Question

I need to transform the string of a custom option of a product when it is added to the cart, but I'm having difficulty doing so..

On the checkout_cart_product_add_after I have the following method being fired:

public function transformStringExample(Varien_Event_Observer $observer)
{

    $quoteitem = $observer->getQuoteItem();

    // $optionId is the ID of the custom product option I want to change
    $option = $quoteitem->getOptionByCode('option_' .  $optionId); 
    $option->setValue($newValue);

}

It seems to work as the quote item option get's successfully saved in the database (sales_flat_quote_item_option). However, when I look at it in the cart, it seems to still be showing the values from the info_buyrequest.

Am I misunderstanding how this works? Or do I have to get the buyrequest, delete it from the quote item, change it, and then "re-set" it using Mage_Sales_Model_Quote_Item::setOptions because there doesn't seem to be a way to easily editing the buyrequest...

Thanks in advance.

Edit:

I've tried what @Vinai suggested here https://stackoverflow.com/a/9344336/896657, which is grabbing and editing the info buy request, but I still can't get it to work..

if ($info = $quoteitem->getProduct()->getCustomOption('info_buyRequest')) {
    $infoArr = unserialize($info->getValue());

    $infoArr['options'][$optionId] = $newValue;

    $info->setValue(serialize($infoArr));
    $quoteitem->addOption($info);
}
Was it helpful?

Solution

Turns out, I was adding a configurable product to the cart and I had to do $quoteitem->getParentItem() and get the custom options of the parent quote item instead of the child.

It still doesn't make sense to me why both the parent and child quote item have the same custom options, even though the custom options are only set on the configurable product and not the child products.

If I run select * from sales_flat_quote_item_option; I get two sets of the same custom options, but for different quoteitems (child and parent). Why is that? Should the custom options only set for the parent quote item (because it relates to the configurable product, where the custom options are set?) The custom options on the child quote item don't seem to have any purpose...

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top