Magento bundle products items – selection_id cannot be null?

In magento bundle product, when you save bundle item in website scope. It’s throw an error that is “SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘selection_id’ cannot be null”. To generate this error go to System -> Configuration -> Catalog -> Price . Select “Catalog Price Scope” to “Website”. Then Create a bundle product, Yes you can see this error. Any Idea? What’s wrong here? I think this is known issue from magento, this type of error comes from all version of magento.

How comes?
In magento, bundle items are save “catalog_product_bundle_selection” table. When you save this item as a scope of website label, then this will be related to another table called “catalog_product_bundle_selection_price”. Where selection_id column is a foreign key of “catalog_product_bundle_selection” table selection_id column. When you save this item as website label, it’s try to save “catalog_product_bundle_selection_price” first, That’s why this error comes.

What’s Solution
Solution is simple πŸ™‚ . Overwrite Mage_Bundle_Model_Selection model class and codes are look like

class Package_MyModule_Model_Selection extends Mage_Bundle_Model_Selection
{
    /**
     * Processing object before save data
     *
     * @return Mage_Bundle_Model_Selection
     */
    protected function _beforeSave()
    {
        // No code please
    }

    /**
     * Processing object after save data
     *
     * @return Mage_Bundle_Model_Selection
     */
    protected function _afterSave()
    {
        $storeId = Mage::registry('product')->getStoreId();
        if (!Mage::helper('catalog')->isPriceGlobal() && $storeId) {
            $this->setWebsiteId(Mage::app()->getStore($storeId)->getWebsiteId());

            $this->getResource()->saveSelectionPrice($this);

            if (!$this->getDefaultPriceScope()) {
                $this->unsSelectionPriceValue();
                $this->unsSelectionPriceType();
            }
        }
        parent::_afterSave();
    }
}

Happy Coding πŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s