Magento: Increase “Qty” upon cancel a shipped order

不打扰是莪最后的温柔 提交于 2020-01-11 06:57:29

问题


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 decreased 1 item), I create a shipment for it and if customer paid the order grand total price. I create an invoice for that order.

My problem, If an order is placed(Qty decreased 1 item), I create a shipment for this order. If the customer refused to pay, I open this order and "Cancel" it and on this case the "Qty" doesn't increase so How can I make it increase?


回答1:


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




回答2:


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();
}


来源:https://stackoverflow.com/questions/13330140/magento-increase-qty-upon-cancel-a-shipped-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!