Magento: Increase “Qty” upon cancel a shipped order

后端 未结 2 564
耶瑟儿~
耶瑟儿~ 2021-01-15 05:12

I\'m on Magento 1.7.0.2. I\'m using \"Cash On Delivery\" as my payment method.I\'ll tell you exactly the steps That i follow on any order. When an order is placed(Qty decrea

2条回答
  •  生来不讨喜
    2021-01-15 05:43

    Thanks to R.S as he helped me more & more.

    I followed all instructions on R.S's reply https://stackoverflow.com/a/13330543/1794834 and I've only changed the observer code. Here is the observer code that worked with me on Magento 1.7.0.2.

    $curr_date = date('Y-m-d H:i:s');
    $order = $observer->getEvent()->getOrder();
    
    foreach ($order->getItemsCollection() as $item) 
    { 
        $productId  = $item->getProductId();
        $qty = (int)$item->getQtyOrdered();
        $product = Mage::getModel('catalog/product')->load($productId);
        $stock_obj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
        $stockData = $stock_obj->getData();
        $product_qty_before = (int)$stock_obj->getQty();
        $product_qty_after = (int)($product_qty_before + $qty); 
        $stockData['qty'] = $product_qty_after;
        /*
         * it may be case that admin has enabled product add in stock, after product sold,
         * he set is_in_stock = 0 and if order cancelled then we need to update only qty not is_in_stock status.
         * make a note of it
         */
        if($product_qty_after != 0) {
            $stockData['is_in_stock'] = 1;
        }else{
            $stockData['is_in_stock'] = 0;
        }
    
        $productInfoData = $product->getData();
        $productInfoData['updated_at'] = $curr_date;
        $product->setData($productInfoData);
        $product->setStockData($stockData);
        $product->save();
    }
    

提交回复
热议问题