programatically change product quantity and lastupdate time in magento


<?php
include $_SERVER['DOCUMENT_ROOT'].'/app/Mage.php';
Mage::app('default');
echo "<br />Started";


 function change_product_qty_after_order($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);

    $stockData['qty'] = $product_qty_after;
    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 (1,8);
echo "<br />Completed";

?>

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