Monday, 8 December 2014

Magento update backorder programatically

To update a products backorder status i wrote a custom script. My script reads the SKU's from the csv file and checks if the product exists and updates its backorder status.

Below is the code.

$products = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
   // get product's stock data such quantity, in_stock etc
   $stockData = Mage::getModel('cataloginventory/stock_item')->loadByProduct($products);
   //to update backorder
   $stockData->setData('use_config_backorders', '0'); //not to use config settings           
   /*0 = No Backorders
   1 = Allow Qty Below 0
   2 = Allow Qty Below 0 and Notify Customer*/
   $stockData->setData('backorders', '1');
   // then set product's stock data to update
   // call save() method to save your product with updated data

You can download the complete code here.  It reads the csv file and checks for the products, If it is found the data are updated. Upon running this file. It will output all the products status (Found, Not found, How many not found) in the webpage. It also Writes the missing sku's in a new CSV file.

No comments:

Post a Comment