I would like to change some options of the items present in the cart.
On the checkout_cart_add_product_complete
.
I launch the following function:
public function TreatShipConflicts($observer)
{
//Analyse du nouveau produit ajouté au panier
$_cart = Mage::getModel('checkout/cart')->getQuote();
$_items = $_cart->getAllItems();
foreach ($_items as $_item) {
$_options = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
$Flag = 0;
$optionid = 0;
Mage::log("CART to be changed");
foreach($_options["options"] as $_option)
{
if ($_option['label'] == 'ShipMth')
{
$Flag = 1;
$optionid = $_option['option_id'];
}
}
Mage::log($optionid);
if ($Flag == 1) {
$optionnb = "option_".$optionid;
$item22 = Mage::getSingleton('checkout/session')->getQuote()->getItemById($_item->getId());
$options = $item22->getOptions();
foreach ($options as $option) {
switch (true) {
case ($option->getCode() == 'info_buyrequest') :
$unserialized = unserialize($option->getValue());
$unserialized['options'][$optionid] = 'TNT';
$option->setValue(serialize($unserialized));
break;
case ($option->getCode() == $optionnb) :
$option->setValue('TNT');
break;
}
}
$item22->setOptions($options)->save();
Mage::getSingleton('checkout/cart')->save();
}
}
}
All my items option are changed EXCEPT the last one (the last added in cart i.e. the one that triggered the event)…
(I was before triggering on the checkout_cart_product_add_after
event, but read on a so post that the item was not yet saved, and then it was impossible to customize its options at this step. I then changed to checkout_cart_add_product_complete, (where at this point it was said to be really an Item and really in cart, but got the same result..)
Thank you for your help,
EDIT: An info that maybe can help. When I add my first item in cart (cart empty) (item that then will be the unsaved one), I have an error on $options = $item22->getOptions();
which means that it is clearly not the same item type as the others…
EDIT 2: still searching… and found a new strange thing : when my cart has items in it and that I remove the last added item (the one with unsaved options), when I add a new item in cart, I have got the same error as the first EDIT !
If I remove another item (not the last one), I can add an time without the $options = $item22->getOptions();
error but the option modification is still unsaved…
EDIT3: I checked the database using phpmyadmin and the "sales_flat_quote_item_option" table still have the unmodified options values for the last added item