1

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

Ricardo Martins
  • 5,702
  • 3
  • 40
  • 59
Bizboss
  • 7,792
  • 27
  • 109
  • 174

6 Answers6

3

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();

    }   
}
Baharuni Asif
  • 325
  • 1
  • 17
1
$product->getStockItem()
    ->setData('qty', $qty)
    ->save();
Drew Hunter
  • 10,136
  • 2
  • 40
  • 49
0

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();
mpaepper
  • 3,952
  • 3
  • 21
  • 28
  • Actually, I think you can even pass in the whole $product instead of the $productId, should both work. – mpaepper Dec 20 '12 at 16:20
  • @Bizboss I edited my answer responding to your edit. This code should now work correctly in your observer. It changes the stock qty before the product is loaded, so afterwards the product should show the wanted behavior. – mpaepper Dec 21 '12 at 14:09
  • Thank you a lot, I have tested it, but `$productId = $observer->getValue();` son't gives anything. – Bizboss Dec 21 '12 at 15:11
  • @Bizboss Which Magento version? I tested it in community 1.7, getValue() should work, b/c in Mage_Core_Model_Abstract where the event ..._load_before is defined, the $id called in load is passed in to the observer as "value" – mpaepper Dec 21 '12 at 18:15
0

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

boruch
  • 453
  • 3
  • 16
  • There are a few errors with you code. Casting to integer in php is simply (int). should productId be $productId? and the second last line has a missing closing parenthesis. I downvoted – Marty Wallace Dec 21 '12 at 06:47
  • @Marty Wallace You could typecast using integer in PHP Look http://php.net/manual/en/language.types.type-juggling.php Also no missing parenthesis on that line. The only valid argument is the missing $. Instead of downvoting why not just point out or edit? – boruch Dec 21 '12 at 14:34
  • because there were a few mistakes in the code that is why a downvoted. i try code and it failed – Marty Wallace Dec 21 '12 at 14:56
  • Did you use the proper observer event? – boruch Dec 21 '12 at 15:09
0

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

Ryan Street
  • 157
  • 7
0

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

Community
  • 1
  • 1
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62