doutuanxiao4619 2012-09-07 15:40
浏览 28
已采纳

为Magento生成数千种测试产品的脚本?

As the title suggests, I'm wondering if there's a way to generate thousands of simple test products (for a local development store) via a PHP script. I have some custom extensions that load different product collections, and they work fine with the standard sample data, but I'd like to test them in a more "real world" setting. So for that I'd like to have a large number of products in the store (as in tens of thousands). They wouldn't need images, and names/SKUs etc could just be incremental values.

Thanks in advance for any suggestions!

  • 写回答

1条回答 默认 最新

  • dongpi9480 2012-09-07 16:58
    关注

    So, here's how, courtesy of this blog post over at Fontis:

    <?php
    $i = 1;
    $numberofprods = 100;
    $numberofprods = range($i,$numberofprods);
    foreach($numberofprods as $product) {
    ++$i; $name = "Product Number ".$i;
    $product = Mage::getModel('catalog/product');
    $product->setSku($i);
    $product->setName($name);
    $product->setDescription("This widget will give you years of trouble-free widgeting.");
    $product->setShortDescription("High-end widget.");
    $product->setPrice(99.99);
    $product->setTypeId('simple');
    $product->setAttributeSetId(4); // need to look this up
    $product->setCategoryIds("3,8"); // need to look these up
    $product->setWeight(1.0);
    $product->setTaxClassId(2); // taxable goods
    $product->setVisibility(4); // catalog, search
    $product->setStatus(1); // enabled
    // assign product to the default website
    $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
    $product->save(); 
    }; ?>
    

    It's best to do this in batches as it seems to be quite a processor heavy task, so I added the amount as a range - so for example you can have $i = 1;$numberofprods = 1000; and then when that's finished $i = 1001;$numberofprods = 2000; and so on.

    To run it, I simply saved it as a template file in app/design/frontend/default/template/createproducts.phtml and then called that from a CMS page like {{block type="core/template" template="createproducts.phtml"}}. Then each time you load that page it'll run and create your products.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器