Search code examples
phpmagentobundle

Magento remove Option from Bundle product


I want to remove all options from bundle product programatically, I know only SKU of the product.

I have

$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.")";
                };

But if i have it in loop, doesn't work properly, deleting option of other products.


Solution

  • try this

    $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();
    
            }
    
        }
    

    hope this help you