Magento从Bundle产品中删除Option

时间:2013-10-21 17:50:32

标签: php magento bundle option

我想以编程方式从捆绑产品中删除所有选项,我只知道该产品的SKU。

我有

$bundled = Mage::getModel('catalog/product')->loadByAttribute('sku',THISISMYSKU);

$selectionCollection = $bundled->getTypeInstance(true)->getSelectionsCollection(
            $bundled->getTypeInstance(true)->getOptionsIds($bundled), $bundled);
            foreach($selectionCollection as $option)
            {

                $sku = $option->getSku();
                if($sku !=  "")
                {
                    $optionModel = Mage::getModel('bundle/option');
                    $optionModel->setId($option->option_id);
                    $optionModel->delete();
                    echo 'deleted: '.$option->getName()." optionID(".$option->option_id.")";
                };

但如果我在循环中使用它,则无法正常工作,删除其他产品的选项。

1 个答案:

答案 0 :(得分:2)

试试这个

$productCollection = Mage::getModel('catalog/product')->getCollection()
                     ->addAttributeToFilter('attribute_set_id', 4)
                     ->addAttributeToFilter( 'sku', array( 'in' => array( 'my-sku1', 'my-sku2', 'my-sku3' ) ) )
                     ->addAttributeToFilter('type_id','bundle')
                     ->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED);

    foreach($productCollection as $product)
    {               
        $bundled = Mage::getModel('catalog/product');
        $bundled->load($product->getId());

        $selectionCollection = $bundled->getTypeInstance(true)->getSelectionsCollection(
        $bundled->getTypeInstance(true)->getOptionsIds($bundled), $bundled);

        foreach($selectionCollection as $option)
        {

            $optionModel = Mage::getModel('bundle/option');
            $optionModel->setId($option->option_id);
            $optionModel->delete();

        }

    }

希望这能帮到你