Update Magento product / inventory from external source.
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.
|
1
2
3
4
5
6
7
8 |
//
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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
//
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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
<?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.
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
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);
}
} |
No comments:
Post a Comment