Update Magento product / inventory from external source.

Update Magento product / inventory from external source.

Magento is newly coming great e-commerce. So if you wish you can move your existing store in Magento or if you interested then you can create another new store in Magento. In this case you may want to synchronize your existing inventory with new magento store. Here I am going to explain how we can update magento product/inventory from outside of Magento admin. We can use corn job to process inventory synchronization in automatic way. Magento also have corn job feature.

In two ways we can update magento’s inventory.
1. By creating new module or
2. Using product API/web service.

Here I will discuss update through module system.

Update inventory creating new module:

At first create new module in local pool to handle inventory update work. And create all of your necessary files. Then follow below steps to update your inventory.

Load product: load product using product id. If you have product sku then retrieve product id for corresponding sku.

 
// retrieve product id using sku
 $product_id = Mage::getModel('catalog/product')
                    ->getIdBySku($sku);
 
// call product model and create product object
$product    = Mage::getModel('catalog/product');
// Load product using product id
 $product ->load($product_id);

Set updated inventory data: set all of your updated data in product object.

 
// get product's general info such price, status, description
$productInfoData = $product->getData();
 
// update general info using new data
$productInfoData['price'] = 11;
$productInfoData['description'] = 'Testing product update';
$productInfoData['status'] = 1;
 
// then set product's general info to update
$product->setData($productInfoData);
 
// get product's stock data such quantity, in_stock etc
$stockData = $product->getStockData();
 
// update stock data using new data
$stockData['qty'] = 356;
$stockData['is_in_stock'] = 1;
 
// then set product's stock data to update
$product->setStockData($stockData);
 
// call save() method to save your product with updated data
$product->save();

Basically our product update process is completed but in this situation updated product will not be save. Magento want origData to save product object. Without admin session magento cannot create origData for product. This is a magento security issue. So if you want to update magento product outside of admin or without creating admin session then you need to override Mage_Catalog_Model_Product class.

Configure new module and create app/local/M4U/catalog/etc/c onfig.xml file and write below code.

 
<?xml version="1.0"?>
<config>
    <global>
	<modules>
           <m4u_catalog>
               <version>0.1.0</version>
           </m4u_catalog>
        </modules>
	<models>
	  <catalog>
            <rewrite>
              <product>M4U_Catalog_Model_Product</product>
            </rewrite>
          </catalog>
       </models>
   </global>
</config>

Override Mage_Catalog_Model_Product class and write setOrigData method as below.

 
class M4U_Catalog_Model_Product extends Mage_Catalog_Model_Product
{
    /**
     * Set original loaded data if needed
     * @param string $key
     * @param mixed $data
     * @return Varien_Object
     */
    public function setOrigData($key=null, $data=null)
    {
       return Mage_Catalog_Model_Abstract::setOrigData($key, $data);
    }
}

If you expert in Magento module system or in override system only then you can follow this process to update magento inventory otherwise you should proceed with api. For alternative solution, you can see “update product through api / web service” post.

Have any query or question? Feel free to post.

DOWNLOAD SOLUTION HERE