1

When an order is placed, I would like to modify some of the products options.

I then made an observer on "sales_order_place_after" calling a function :

       public function sitreatmth($observer)
   {
        $_order = $observer->getEvent()->getOrder();
        $_order_id = $_order->getId();

        $_items = $_order->getAllItems();


            $shipmentItems = array();
            $shipmentItemsMth = array();

            foreach($_items as $_item) 
            {
                $_options = $_item->getProductOptions();            

                foreach($_options["options"] as $option) 
                {
                    if($option['label'] == 'FabTime') 
                    {
                        if ($option['value']==2)
                        {
                            $shipmentItems [$_item->getId()] = $_item->getQtyOrdered();

                            foreach($_options["options"] as $option) 
                            {
                                if($option['label'] == 'ShipMth') 
                                {
                                    $shipmentItemsMth [$_item->getId()] = $option['value'];
                                }
                            }

                        }
                    }       
                }

            $CptTNT=0;
            $CptSTD=0;

            foreach ($shipmentItemsMth as $Mth)
            {
                if ($Mth=='TNT') {$CptTNT++;}
                else if ($Mth=='STD') {$CptSTD++;}
            }

            if (($CptTNT!=0)&&($CptSTD!=0))     // CONFLIT tout TNT
            {
                foreach ($shipmentItems as $itemshp)
                {
                    $_item=$_order->getItemById($itemshp);

                    $_options = $_item->getProductOptions();  
                    $row_id_cnt = 0;
                    $row_id = 0;        
                    $Flag = 0;

                    foreach($_options["options"] as $option) 
                    {
                        if($option['label'] == 'ShipMth') 
                        {

                            $row_id = $row_id_cnt;
                            $Flag = 1;
                        }
                        $row_id_cnt++;
                    }       
                    if ($Flag == 1)
                    {   
                        $_options["options"][$row_id]['value'] = "TNT";
                        $_options["options"][$row_id]['print_value'] = "TNT";
                        $_options["options"][$row_id]['option_value'] = "TNT";
                    }
                    $_item->setProductOptions($_options);
                    $_order->setSomeNonExistentProperty(true); 
                    $_order->save();    

                    ///DEBUG
                    $_order = $observer->getEvent()->getOrder();
                    $_item=$_order->getItemById($itemshp);

                    $_options = $_item->getProductOptions();  

                    foreach($_options["options"] as $option) 
                    {
                        if($option['label'] == 'ShipMth') 
                        {
                            Mage::log($option['value']);
                        }
                    }           

                }   
            }


            if ($_order->getId() && !empty($shipmentItems) && $_order->canShip()) 
            {
                $shipment = Mage::getModel('sales/service_order', $_order)->prepareShipment($shipmentItems);
                $shipment->save();
            }
        }
   }

The Debug section returns the good values, but, in backend, when i access my order details, the product options are not changed….

Thank you for your help,

1

try to set up as a admin store when you are saving the product. So from the beginning of the code save the current store id in a $current variable. Then set as admin store. Right after you save your product back the store using the $current value. I'm from the mobile sorry if I don't add more details.

Let me know if this works.

Best, GrinGo.

[UPDATE] Now from my laptop I review much better your code and what you need. Let do this simple change and let me know if is works. Call "sales_order_place_before" not "sales_order_place_after"

Best, GrinGo.

  • thank you for your help. I did not find how to do that... – Alexglvr Feb 16 '15 at 17:56
  • I'll send you a example. Give a moment. – AleGrinGo Feb 16 '15 at 18:11
  • sales_order_place_before does not change anything…my options are still unchanged – Alexglvr Feb 17 '15 at 7:56
  • Something is missing in the code for sure. If I have time I'll try to add this pace of code in ones of my project and find out what going on. Why you want to change this values? Is business porpoises? Best. – AleGrinGo Feb 17 '15 at 12:31
  • I need to change this value because I have several manufacturing times, and conflicts can be generated on shipments by clients. – Alexglvr Feb 21 '15 at 10:18
0

Here is the solution to modify the options of the items already in cart. DOES NOT WORK FOR THE NEW ADDED ITEM :

$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() == "option_67") :
                $option->setValue('TNT');
                break;
        }
    }
    $item22->setOptions($options)->save(); 
    Mage::getSingleton('checkout/cart')->save();

I continue searching to change the just added Item options…

EDIT: after having read several post, I learned that on "sales_order_place_after" event, the new item was not really an item yet, therefore it seems to be impossible to change/save its options… I then tried a new solution, trigger on "checkout_cart_add_product_complete" but same result…

The upper code is working for all items in cart, but not for the last one (the last added)…

I tried re-reading the item options after having applied the modifications, and they are well changed. Last product is just NOT SAVED…

Thank you for your help,

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.