【问题标题】:Creation of bundle products in a loop在循环中创建捆绑产品
【发布时间】:2015-06-10 13:01:49
【问题描述】:

我有一个循环以编程方式创建多个捆绑产品。新包的创建工作正常,但如果我有一个现有包并尝试添加/删除选项,它会失败。

如果我第一次运行我的导入代码,就会创建包。第二次,所有选项都将被删除,只有数组的最后一个包有它的选项集。

$ids = [8663,8665,8664];
foreach ($ids as $id) {
    createBundle($id);
}

function createBundle($id)
{
    Mage::unregister('product');

    try {
        $bundleProduct = Mage::getModel('catalog/product')->load($id);
        Mage::register('product', $bundleProduct);

        // try to get already assigned options and remove them all
        $selectionCollection = $bundleProduct->getTypeInstance(true)->getSelectionsCollection(
            $bundleProduct->getTypeInstance(true)->getOptionsIds($bundleProduct), $bundleProduct
        );

        // remove assigned options
        // works fine
        foreach ($selectionCollection as $option) 
        {
            $optionModel = Mage::getModel('bundle/option');
            $optionModel->setId($option->option_id);
            $optionModel->delete();
        }

        $bundleOptions = array(
            '0' => array( //option id (0, 1, 2, etc)
                'title' => 'item01', //option title
                'option_id' => '',
                'delete' => '',
                'type' => 'select', //option type
                'required' => '1', //is option required
                'position' => '0' //option position
            ),
            '1' => array(
                'title' => 'item02',
                'option_id' => '',
                'delete' => '',
                'type' => 'multi',
                'required' => '1',
                'position' => '0'
            )
        );

        $bundleSelections = array();
        $bundleSelections = array(
            '0' => array( //option ID
                '0' => array( //selection ID of the option (first product under this option (option ID) would have ID of 0, second an ID of 1, etc)
                    'product_id' => 8631, //id of a product in selection
                    'delete' => '',
                    'selection_price_value' => '0',
                    'selection_price_type' => 0,
                    'selection_qty' => 10,
                    'selection_can_change_qty' => 0,
                    'position' => 0,
                    'is_default' => 1
                )
            ),
            '1' => array( //option ID
                '0' => array(
                    'product_id' => 8630,
                    'delete' => '',
                    'selection_price_value' => '0',
                    'selection_price_type' => 0,
                    'selection_qty' => 1,
                    'selection_can_change_qty' => 0,
                    'position' => 0,
                    'is_default' => 1
                )
            )
        );

        // flags for saving custom options/selections
        $bundleProduct->setCanSaveCustomOptions(true);
        $bundleProduct->setCanSaveBundleSelections(true);
        $bundleProduct->setAffectBundleProductSelections(true);

        // setting the bundle options and selection data
        $bundleProduct->setBundleOptionsData($bundleOptions);
        $bundleProduct->setBundleSelectionsData($bundleSelections);

        $bundleProduct->save();
        $bundleProduct->setData(array());
        return $bundleProduct->getId();

    } catch (Exception $e) {
        Mage::log($e->getMessage());
        echo $e->getMessage();
    }
}

那么,我怎样才能得到这份工作呢?如果我删除了删除选项的部分,那么每次执行代码时都会创建新的空选项(这是错误的)。

更新:我发现,如果我三次运行脚本,每个捆绑包都具有另一个产品 ID,则这些包已正确创建/更新。所以问题一定是在单个请求中循环执行。

【问题讨论】:

    标签: magento foreach bundle


    【解决方案1】:

    最后,我自己发现了。这部分贴出的代码:

        // try to get already assigned options and remove them all
        $selectionCollection = $bundleProduct->getTypeInstance(true)->getSelectionsCollection(
            $bundleProduct->getTypeInstance(true)->getOptionsIds($bundleProduct), $bundleProduct
        );
    
        // remove assigned options
        // works fine
        foreach ($selectionCollection as $option) 
        {
            $optionModel = Mage::getModel('bundle/option');
            $optionModel->setId($option->option_id);
            $optionModel->delete();
        }
    

    似乎只删除了选项的选择部分。要删除完整选项,我必须这样做:

        // try to get already assigned options and remove them all
        $optionCollection = $_product->getTypeInstance(true)->getOptionsCollection($_product);
    
        // remove assigned options
        foreach ($optionCollection as $option) 
        {
            $option->delete();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 2013-07-19
      • 2013-06-16
      • 1970-01-01
      • 2013-10-27
      • 2019-03-05
      相关资源
      最近更新 更多