Programatically change products quantity after order cancelled Magento


include $_SERVER['DOCUMENT_ROOT'].'/app/Mage.php';
Mage::app('default');

/**
 * 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
*/
function change_product_qty_after_order_cancel($product_id,$qty){
    $curr_date = date('Y-m-d H:i:s');
    $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); //annet-pk just added qty update here (+)
 
    $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();
 }
change_product_qty_after_order_cancel (1,12);

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools