I've got a problem when adding a grouped product to cart.
I need to set a custom option for all products that are added to cart while adding a grouped product to cart.
What I have tried last (with a little bit of success):
<checkout_cart_product_add_after>
<observers>
<customoptions>
<type>singleton</type>
<class>Company_CustomOptions_Model_Observer</class>
<method>addCustomOptionGroupSku</method>
</customoptions>
</observers>
</checkout_cart_product_add_after>
and
public function addCustomOptionGroupSku(Varien_Event_Observer $observer) {
$product = $observer->getProduct ();
if ($product->isGrouped ()) {
$quoteItem = $observer->getQuoteItem ();
$additionalOptions = array (
'options' => array (
'label' => 'GROUPSKU',
'value' => $product->getSku ()
)
);
$quoteItem->addOption ( new Varien_Object ( array (
'product' => $quoteItem->getProduct (),
'code' => 'additional_options',
'value' => serialize ( $additionalOptions )
) ) );
}
}
I have created one grouped product, containing two products.
But that code only adds the custom option "GROUPSKU" to one of the items in the cart. The other one is untouched.
How do I get all the QuoteItems that are about to be added to the cart?
I have also added this question to the StackOverflow part of StackExchange: https://stackoverflow.com/questions/27905415/add-custom-options-while-adding-grouped-product-to-cart
Best Answer
I found a solution which does not need an observer.
I had to do a rewrite to
Mage_Sales_Model_Quote
. More specific the methodaddProductAdvanced()
Bad news: You can not simply use
parent::addProductAdvanced()
and then do your own stuff. You have to copy the original code and fill it up with your code.Here is what I did (look out for the comment starting with
/********
):This way all the products that are added to the cart using a grouped product now have the custom option.