Magento – Stock qty is not updating after order place with custom qty

event-observerproduct

I am trying to set stock qty of products with my custom stock qty after placing order. So I used following code in my config.xml file-

<sales_order_place_after>
                    <observers>
                        <po>
                            <type>singleton</type>
                            <class>po/observer</class>
                            <method>setQty</method>
                        </po>
                    </observers>
</sales_order_place_after> 

Obeserver.php file

public function setQty($product_id)
{ 
  $product = Mage::getModel("catalog/product")->load(12345); //assume product_id=1234 just placed hardcore for a particular product
  $product->setStockData(array(
                        'use_config_manage_stock' => 0, 
                        'manage_stock'=>1, 
                        'min_sale_qty'=>1, 
                        'is_in_stock' => 1, 
                        'qty' => 5
                        )
  );

 $product->save();
}

now if product with id 12345 has current stock qty 4 and an order with qty 1 of this product is placed then I want its qty should be 5 or whatever I want. but currently it is decreasing and showing qty 3 instead of qty 5. I have set "Yes" for option Decrease Stock When Order is Placed in System>Configuration>Inventory. I don't want to set it NO. Although setting it NO, it worked and qty is set to 5

Not sure which function is overriding qty. Perhaps it is core functionality. I want to call my observer function after all core function has been called. but i don't know how to do this. Please help me.

Best Answer

It must be coming from the module Mage/CatalogInventory. I didn't try, but here are some tips for your searches :

There are two observers that seems to be related to the stock qty update :

app/code/core/Mage/CatalogInventory/etc/config.xml :145

        <sales_model_service_quote_submit_before>
            <observers>
                <inventory>
                    <class>cataloginventory/observer</class>
                    <method>subtractQuoteInventory</method>
                </inventory>
            </observers>
        </sales_model_service_quote_submit_before>
        <sales_model_service_quote_submit_success>
            <observers>
                <inventory>
                    <class>cataloginventory/observer</class>
                    <method>reindexQuoteInventory</method>
                </inventory>
            </observers>
        </sales_model_service_quote_submit_success>

There is also this function, where the decreasing of the stock quantity is made, if the option is set yo YES.

app/code/core/Mage/CatalogInventory/Model/Stock/Item.php :204

public function subtractQty($qty)
{
    if ($this->canSubtractQty()) {
        $this->setQty($this->getQty()-$qty);
    }
    return $this;
}

This function is called only in two functions, registerProductsSale, and registerItemSale. What is interested is that those functions themselves are used only in the observer. One of them is deprecated. And the other one is... subtractQuoteInventory !

So if I was you, I would check when is this event sent exactly, and maybe try to either run your observer method after, or to override the Mage/CatalogInventory observer, and change the functionality of this function.

You can override the observer by adding an observer with the same name in your own module, e.g. :

etc/config.xml

    <sales_model_service_quote_submit_before>
        <observers>
            <inventory>
                <class>my_module/observer</class>
                <method>subtractQuoteInventory</method>
            </inventory>
        </observers>
    </sales_model_service_quote_submit_before>

Also set a dependency in your module declaration file, so your configuration will be loaded after the original configuration and replace it.

app/etc/module/my_module.xml

<config>
    <modules>
        <My_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_CatalogInventory />
            </depends>
        </My_Module>
    </modules>
</config>