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')

// 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

// 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

// call save() method to save your product with updated data

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"?>

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.


9 responses to “Update Magento product / inventory from external source.

  1. I was able to get the same achieved result by using Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    • Scott, Yes you can do same thing using Mage::app(). But I think we should follow Magento module system and you may directly update product data in database without calling $product->save();

  2. Thank you for this post, it saved me a lot of time!

  3. I’m a bit confusing (i’m studying magento):
    are you using two different modules? If so, how are they related?

  4. Could you please post the actual XML which is generated by your php code. I do not know PHP but I have a tool called SOAPUI which I am using to learn about web services and their interactions. This allows me to send actual XML examples and I cannot see what tags i need around the new qty value so a complete, single product update of stock level as XML would be great.

    Thanks in Advance,

  5. dipankar mukherjee

    this is a wonder ful post,thaks boss , your code helped me a lot ,thanks

  6. Hi Mage Folks
    If you want to update product prices in Magento in easier & faster way by using csv file with just two fields: sku & new price then you can follow this great article:

    This saves you a lot of your time and hence $$$

    Happy E-Commerce!!

  7. Magento Agentur

    Does this work with Magento 1.3.2?

  8. We just purchased stocker update from stockupdater.com – makes updating inventory during the check in / check out process really easy. Can even use a barcode scanner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s