3

I have created an event which checks that if the backorder quantity of an item is greater than zero than set the ordered product quantity to zero instead of keeping it in negative and also change its status to out of stock but nothing works.

config.xml

<sales_order_place_after>
                <observers>
                    <some_random>
                        <class>My_Model_Observer</class>
                        <method>checkBackOrder</method>
                    </some_random>
                </observers>
            </sales_order_place_after>

Observer.php

public function checkBackOrder($observer)
    {
        $order = $observer->getOrder();
        $customer = $order->getCustomer();
            foreach ($order->getAllItems() as $item)
            {
                if ($item->getProductType() == 'simple') {
                    $product = Mage::getModel("catalog/product")->load($item->getProductId());

                    if ($item->getQtyBackordered() > 0) {
                        $webQty = $product->getStockItem()->getQty();
                    if ($webQty <= 0) {
                        $this->_changeStockStatus($product);
                      }
                    }
                }
            }
    }

protected function _changeStockStatus($product)
    {
        $webStockId = Mage::app()->getWebsite()->getStockId();
        $store = Mage::app()->getStore()->getStoreId();

        $stockItem = Mage::getModel('cataloginventory/stock_item')->setStoreId($store)->loadByProduct($product->getId());
        $stockItem->setData('qty', 0);
        $stockItem->setIsInStock(false)->setStockId($webStockId)->save();
    }

As you can see in the code that I am not doing something complex except trying to change the item quantity and setting it status to "Out of stock" but when I execute this code it does not change the quantity or the status of an item.

Can someone please tell me what I am doing wrong here?

1

Try the following.

       // Check if there is a stock item object
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
    $stockItemData = $stockItem->getData();
    if (empty($stockItemData)) {
    // Create the initial stock item object
    $stockItem->setData('manage_stock',1);
    $stockItem->setData('is_in_stock',$qty ? 1 : 0);
    $stockItem->setData('use_config_manage_stock', 0);
    $stockItem->setData('stock_id',1);
    $stockItem->setData('product_id',$product->getId());
    $stockItem->setData('qty',0);
    $stockItem->save();

    // Init the object again after it has been saved so we get the full object
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
}

// Set the quantity
$stockItem->setData('qty',$qty);
$stockItem->save();
$product->save();

And the just save the product. Also you can refer few links here,
How can i quick update of qty and stock and all inventory fields in Magento
How to set a product to be in/out of stock programatically

  • I do not see the reason to create a stock item here and reload the object again. After placing the order it is not like the item is going to be delete from stock. Secondly I do not understand why to save the product? Saving the stock item itself should be enough and do the trick so what is the reason of saving the product here? @RJ07 – Capri82 Jul 4 '16 at 10:37
0

After spending a lot of time and digging I solved the problem myself. Instead of using sales_order_place_after observer I used sales_model_service_quote_submit_success which is the last observer running by magento after placing an order.

And this where I placed my code and got my desired result(s).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.