Magento 以编程方式创建 bundle 产品

标签 magento import bundle

我在哪里可以找到在 PHP 中创建 BundleProducts 的完整且有效的示例? 我正在使用 Magento 1.7

Google 只给我碎片

编辑:

直到现在我在这段代码,但保存抛出

Fatal error: Call to a member function getStoreId() on a non-object in \app\code\core\Mage\Bundle\Model\Selection.php on line 73

-

---删除旧代码--

编辑 2 好的,通过注册产品异常已经消失,我的 bundle 出现在后端,但 bundle 项目没有显示在管理面板中

$items = array();
    $selections = array ();

    $items[] = array(
                'title' => '',
                'option_id' => '',
                'delete' => '',
                'type' => 'radio',
                'required' => 'true',
                'position' => 0,
            );

    $items[] = array(
                'title' => '',
                'option_id' => '',
                'delete' => '',
                'type' => 'radio',
                'required' => 'true',
                'position' => 0,
            );

    $selection = array();
    $selection[] = array(
                'selection_id' => '',
                'option_id' => '',
                'product_id' => '2',
                'delete' => '',
                'selection_price_value' => '',
                'selection_price_type' => '0',
                'selection_qty' => '1',
                'selection_can_change_qty' => '0',
                'position' => '0',
                'is_default' => '1',
            );
    $selections[] = $selection;

    $selection = array();
    $selection[] = array(
                'selection_id' => '',
                'option_id' => '',
                'product_id' => '3',
                'delete' => '',
                'selection_price_value' => '',
                'selection_price_type' => '0',
                'selection_qty' => '1',
                'selection_can_change_qty' => '0',
                'position' => '0',
                'is_default' => '1',
            );
    $selections[] = $selection;

    $storeID = 1;
    $websiteIDs = array(1);
    $product = Mage::getModel('catalog/product');

    $p = array(
            'sku_type' => 0,
            'sku' => '123321',
            'name' => "BarProduct",
            'description' => 'Foo',
            'short_description' => 'Bar',
            'type_id' => 'bundle',
            'attribute_set_id' => 4,
            'weight_type' => 0,
            'visibility' => 4,
            'price_type' => 0,
            'price_view' => 0,
            'status' => 1,
            'created_at' => strtotime('now'),
            'category_ids' => $cats,
            'store_id' => $storeID,
            'website_ids' => $websiteIDs
    );
    $product->setData($p);

    Mage::register('product', $product);
    Mage::register('current_product', $product);

    $product->setBundleOptionsData($items);
    $product->setBundleSelectionsData($selections);
    $product->setCanSaveBundleSelections(true);
    $product->setCanSaveConfigurableAttributes(false);
    $product->setAffectBundleProductSelections(true);
    $product->setCanSaveCustomOptions(true);

    $product->save();

最佳答案

好的,这段代码现在对我有用了:

$processes = Mage::getSingleton('index/indexer')->getProcessesCollection();
    $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_MANUAL));
    $processes->walk('save');


    $items = array();
    $selections = array ();

    $items[] = array(
            'title' => 'o1',
            'option_id' => '',
            'delete' => '',
            'type' => 'radio',
            'required' => '1',
            'position' => '0',
    );

    $items[] = array(
            'title' => 'o2',
            'option_id' => '',
            'delete' => '',
            'type' => 'radio',
            'required' => '1',
            'position' => '0',
    );

    $selection = array();
    $selection[] = array(
            'selection_id' => '',
            'option_id' => '',
            'product_id' => '2',
            'delete' => '',
            'selection_price_value' => '',
            'selection_price_type' => '0',
            'selection_qty' => '1',
            'selection_can_change_qty' => '0',
            'position' => '0',
            'is_default' => '1',
    );
    $selections[] = $selection;

    $selection = array();
    $selection[] = array(
            'selection_id' => '',
            'option_id' => '',
            'product_id' => '3',
            'delete' => '',
            'selection_price_value' => '',
            'selection_price_type' => '0',
            'selection_qty' => '1',
            'selection_can_change_qty' => '0',
            'position' => '0',
            'is_default' => '1',
    );
    $selections[] = $selection;



    $storeID = 1;
    $websiteIDs = array(1);
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $product = Mage::getModel('catalog/product');

    $cats = array(3);

    $p = array(
            'sku_type' => 1,
            'sku' => '123321',
            'name' => "BarProduct",
            'description' => 'Foo',
            'short_description' => 'Bar',
            'type_id' => 'bundle',
            'attribute_set_id' => 4,
            'weight_type' => 0,
            'visibility' => 4,
            'price_type' => 1,
            'price_view' => 0,
            'price' => 1.99,
            'has_options' => 1,
            'required_options' => 1,
            'status' => 1,
            'created_at' => strtotime('now'),
            'category_ids' => $cats,
            'store_id' => 0,
            'website_ids' => $websiteIDs,
            'weight' => 0,
            'weight_type' => 1,
            'delivery_time' => '',
            'generate_meta' => 1,
            'tax_class_id' => 1, //19%
    );
    $product->setData($p);

    Mage::register('product', $product);
    Mage::register('current_product', $product);
    $product->setCanSaveConfigurableAttributes(false);
    $product->setCanSaveCustomOptions(true);


    // Set the Bundle Options & Selection Data
    $product->setBundleOptionsData($items);
    $product->setBundleSelectionsData($selections);
    $product->setCanSaveBundleSelections(true);
    $product->setAffectBundleProductSelections(true);

    $product->save();

    $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME));
    $processes->walk('save');
    $processes->walk('reindexEverything');

关于Magento 以编程方式创建 bundle 产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13584034/

相关文章:

php - 我无法弄清楚语法错误

asp.net-mvc - 缩小的脚本仅在MVC4 BundleConfig中

swift - 读取 macOS 命令行工具项目中的文件

sql - magento 锁定等待超时超出 sales_flat_order_grid

.htaccess - Magento 中的 SSL 错误(404 未找到)

php - 如何在 Magento 中编写触发器?

go - 无法在 Golang 中导入本地模块

magento - 法师日志(magento)中的 null 是什么意思?

python - 关于 "import"的基本问题

Android - 导出加密 key 以注册已发布的应用程序