1
votes

I have to create an extension in which when Minimum Qty Allowed in Shopping Cart > Inventory then its set Minimum Qty Allowed in Shopping Cart = Quantity.

Event is worked after save an order.

My code is given below:

public function updateminqtynew(Varien_Event_Observer $observer)
            {
            $quote = $observer->getEvent()->getOrder();
            $orderIncrementId = $quote->getIncrementId();
            $order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
             $items = $order->getAllItems(); 
              $itemcount=count($items);

                foreach ($items as $itemId => $item)
                {
                                    $sku = $item->getSku();
                                    $_Pdetails = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
                                    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_Pdetails->getId());
                                    $minqty = $stock->getMinSaleQty();
                                    $allqty = $stock->getQty();         
                                //  Mage::log("minqty".$_Pdetails->getId().round($minqty), null, 'logfileif.log');
                                    //Mage::log("allqty".round($allqty), null, 'logfileif.log');

                                if(intval($minqty)>intval($allqty))
                                    {
                                        Mage::log("if working", null, 'logfileif.log');

                                        $stock->setMinSaleQty($allqty);
                                        $stock->setQtyIncrements($stock->getQty());
                                        $stock->save();


                                            Mage::log("save", null, 'logfileif.log');
                                    }
                                    else
                                    {
                                            //Mage::log("NO", null, 'logfileif.log');
                                    }


                        }
            }

Values are print in log file but not update in database.

1

1 Answers

0
votes

Please try this and let me know its work for you .

 if(intval($minqty)>intval($allqty))
                                    {
                                        Mage::log("if working", null, 'logfileif.log');
///--- another pattern to save data if logically  all are correct as you mentioned. 
                                    $stock->setData('min_sale_qty',$allqty);
                                    $stock->setData('qty_increments',$stock->getQty());
                                    $stock->save();


                                        Mage::log("save", null, 'logfileif.log');
                                }