i am trying to implement an simple observer in a custom module to update product stock when it is loaded.
This is the content of the xml file:
<?xml version="1.0"?>
<config>
<modules>
<Foo_Bar>
<version>0.1</version>
</Foo_Bar>
</modules>
<global>
<models>
<updatestock>
<class>Foo_Bar_Model</class>
</updatestock>
</models>
<events>
<catalog_product_load_before>
<observers>
<Foo_Bar>
<type>model</type>
<class>updatestock/observer</class>
<method>updatestock</method>
</Foo_Bar>
</observers>
</catalog_product_load_before>
</events>
</global>
</config>
And here is the content of the observer model:
class Foo_Bar_Model_Observer extends Mage_Core_Model_Abstract {
public function updatestock($observer) {
$product = $observer->getProduct();
$product->setQty(555);
$product->save();
}
}
The problem is that the Stock is not saved; What can be the solution for this?
Thanks for help.
Edit:
I think that the problem comes from :
<catalog_product_load_before>
The product is not yet loaded and I get a 404 Not found page, but when I replace it with:
<catalog_product_load_after>
The product stock is properly updated, but when the product Stock Availability is "Out of Stock" and Stock is 0; the "Availability" in Frontend is always "Out of Stock" and "Add to cart Button" is hidden (this means that the loaded inventory is 0).
I have tried with "<catalog_product_load_before>
" but in this case I can't get the product Id nor SKU.
Thanks for help