Question

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

No correct solution

OTHER TIPS

If you want to $productobj after saving product from backend side so you can easily use catalog_product_save_after event.

I am assuming that you already know how to create a module in M2.

Right now you have to need develop new module for M2

Then Create this events.xml file in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And Create your observer file Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
        $id = $product->getId(); //Get Product Id

        //Get Quantity
        $stockItem = $product->getExtensionAttributes()->getStockItem();
        $stockData = $stockItem->getQty();
        // Get new Qty
        $_vendor_qty = $product->getVendorQty();
        $_on_hand_qty = $product->getOnHandQty();
        $totalQty = $_vendor_qty+$_on_hand_qty; //Add New Qty


        $stockItem->setQty($totalQty); //Set New Qty to Main Qty
        $stockItem->save();

    }   
}
$product->getStockItem()
    ->setData('qty', $qty)
    ->save();

Product stock is handled through the CatalogInventory Module.

Try this:

class Foo_Bar_Model_Observer  {

    public function updatestock($observer) {
        $product = $observer->getProduct();
        $stock = $product->getStockItem();

        $stock->setQty(555);      
        $stock->save();

        $product->save();
    } 
}

Also, you don't need to extend Mage_Core_Model_Abstract on an observer. Just trying to save a little memory in your system ;)

Try

$product = productId = $observer->getProduct();
$stockItem = Mage::getModel('cataloginventory/stock_item');
$stockItem->assignProduct($product);
$stockItem->setData('is_in_stock', 1);
$stockItem->setData('qty', 5555);

$product->setStockItem($stockItem);
$product->save();

See Magento: Increase "Qty" upon cancel a shipped order

Use the event catalog_product_load_before and in the observer use the following code:

$productId = $observer->getValue();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItem->setData('qty',555);
$stockItem->save();

You can load the product using:

$productId = $observer->getProduct()->getId();
$stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItem->setData('qty', (integer)$stockAmount);
$stockItem->save();

And that's it :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top