Magento: Increase “Qty” upon cancel a shipped order

后端 未结 2 565
耶瑟儿~
耶瑟儿~ 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:40

    If order status is Processing

    Create a custom module with observer for 'order_cancel_before' (see example @ Change Magento default status for duplicated products change <catalog_model_product_duplicate> to <order_cancel_before>

    since <order_cancel_before> is not defined in app/code/core/Mage/Sales/Model/Order.php

    You could override/rewrite order model class see e.g http://phprelated.myworks.ro/how-to-override-rewrite-model-class-in-magento/

    In your local module do

    public function cancel()
    {
        if ($this->canCancel()) {
            Mage::dispatchEvent('order_cancel_before', array('order' => $this));
            $this->getPayment()->cancel();
            $this->registerCancellation();
    
            Mage::dispatchEvent('order_cancel_after', array('order' => $this));
        }
    
        return $this;
    }
    

    Or you could create a new method increaseProductQty() in your model and copy the code below into it (this way you would not need an observer). Then replace the line Mage::dispatchEvent('order_cancel_before'... with $this->increaseProductQty()

    In your observer method (pseudo code)

    $curr_date = date('Y-m-d H:i:s');
    $order = $observer->getEvent()->getOrder();
    
    foreach ($order->getItemsCollection() as $item) 
    { 
        $productId  = $item->getProductId();
        $qty = $item->getQty();
    
        // you need to check order status to make sure it processing
        //$order->getStatus() (assuming you are canceling entire order)
        //$order->getPayment();
    
        $product = Mage::getModel('catalog/product')->load($product_id);
        $stock_obj = Mage::getModel('cataloginventory/stock_item')->load($product_id);
        $stockData = $stock_obj->getData();
        $product_qty_before = (int)$stock_obj->getQty();
        $product_qty_after = (int)($product_qty_before + $qty); 
        $stockData['qty'] = $product_qty_after;
    
        $productInfoData = $product->getData();
        $productInfoData['updated_at'] = $curr_date;
        $product->setData($productInfoData);
        $product->setStockData($stockData);
        $product->save();
    }
    

    If you have issue with updating stock see Set default product values when adding new product in Magento 1.7

    Reference http://pragneshkaria.com/programatically-change-products-quantity-after-order-cancelled-magento/

    If order status is Pending

    Take a look at System > Configuration > Inventory

    Set Items’ Status to be In Stock When Order is Cancelled — Controls whether products in pending orders automatically return to the stock if orders are cancelled. Scope: STORE VIEW.

    Read more @

    How to Manage Magento Store Inventory?

    ADMIN: System → Configuration → Inventory Tab

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题