1

So I have a module, which I am using to set a custom field in bundle options. I have implimented my module successfully. However I came to a situation to which I can't move on without get an answer for this problem.

In my module, in order to save data to my custom fields, I am observing to <catalog_product_save_after />. In my custom table,along with my custom field id and its content, I want to save option_id and store_id. Things work fine, for product that have already bundle options. For such products, I can get option_id and store_id. But for newly created options for product, option_id field seems to empty. I don't know how to get option_id.

Method I am using to get option details : $product->getBundleOpitonsData()

When echo this, I am getting a result like this.

Array
(
[7] => Array
    (
        [description_id] => 
        [description_new] => 
        [title] => bla bla title
        [description] => bla bla description
        [option_id] => 
        [delete] => 
        [type] => select
        [required] => 1
        [position] => 
    )
 [1] => Array
    (
        [description_id] => 1
        [description_new] => no
        [title] => test
        [description] => new description
        [option_id] => 30
        [delete] => 
        [type] => select
        [required] => 1
        [position] => 0
    )

 ---------- 
)

As you can see, for newly created option(array[7]), option_id field is empty. But for an already existing option (array[1]), option_id DOES exist.

Note : In Mage_Bundle module, there is an observer for the event <catalog_product_prepare_save />. In the mentioned obsever, we can see that options are SETing.

public function prepareProductSave($observer)
{
    $request = $observer->getEvent()->getRequest();
    $product = $observer->getEvent()->getProduct();

    if (($items = $request->getPost('bundle_options')) && !$product->getCompositeReadonly()) {
        $product->setBundleOptionsData($items);
    }
}

If that is correct, before invoking my observer, option is setting through the bundle observer. Then Why option_id field is empty for new option ?

Please help me friends. I can't move on without your help. Eagerly waiting for your reponses

1
+50

Your option_id is coming from the database table catalog_product_bundle_option. The id is not set by Magento but is an AUTOINCREMENT assigned by Mysql when the object is first saved. You can see this happen a lot throughout Magento, the id is not available until after the first time the object is saved.

For your scenario your issue is likely coming from
`app/code/core/Mage/Bundle/Model/Product/Type.php

public function save($product = null)
{
    parent::save($product);

which saves the bundle options after the product has been saved. Your event is triggered with the parent::save so the options have not hit the database yet.

To solve your issue try to find an event which is triggered later than catalog_product_save_after, for example catalog_product_save_commit_after.

  • Thanks for quick reply. Can I know what is then setting in prepareSave() method ? If I use catalog_product_save_commit_after, do I get all bundle options by using $product->getBundleOpitonsData() method ? – Rajeev K Tomy Jun 16 '14 at 1:32
  • ` [7] => Array ( [description_id] => [description_new] => [title] => BLUM BLUM [description] => BLUM BLUM [option_id] => [delete] => [type] => select [required] => 1 [position] => )` I used described event. But as you can see, option_id is empty now also . – Rajeev K Tomy Jun 16 '14 at 1:55
  • just had a quick browse through the code for you setBundleOptionsData() is only ever used in the prepareProductSave observer reading from POSTed data. Which will never get the id until an edit of the product. The above still applies so I would suggest getting your options from the bundle product. For code example see /app/code/core/Mage/Bundle/Block/Catalog/Product/View/Type/Bundle.php in the getOptions method. – Kristof at Fooman Jun 16 '14 at 2:10
  • It helped me. It gave me some idea that I am lack with. Thanks for your support. You owe my bounty – Rajeev K Tomy Jun 16 '14 at 4:52
  • Great to hear you solved it - feel free to post what you ended up doing for others to learn from. – Kristof at Fooman Jun 16 '14 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.