0

I'm trying to put qty to 0 if qty < 0, but it's not working for me i all time have qty on minus

my code don't working for me

public function salesOrderSaveAfter($event)
    {
 $order = $event->getOrder();
            $orderIncrementId = $order->getIncrementId();

            foreach ($order->getAllVisibleItems() as $item) {
                $oldQty = (int)$item->getProduct()->getStockItem()->getQty();
                $qtyOrdered = (int)$item->getQtyOrdered();

                $differenceQty = $oldQty - $qtyOrdered;
            }

            //increase the price by 25% AND check for fires twice
            if ($differenceQty < 0) {
                $product_model = Mage::getModel('catalog/product')->load($item->getProductId());
                $price = $product_model->getPrice();
                $newPrice = $price + (($price * 25) / 100);
                $product_model->setPrice($newPrice);
                if ($product_model->getStockItem()->getBackorders() == "2") {
                    $product_model->setStockData(array(
                            'use_config_manage_stock' => 1,
                            'manage_stock' => 1,
                            'is_in_stock' => 1,
                            //'qty' => 0 don't working too
                        )
                    );
                    $product_model->setQty(0);
                }

                $product_model->save();
            }
}

I need when the quantity is less 0, for example -3, set quantity to 0

enter image description here

0

Use below code to update product stock data in magento 1

$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product_model->getId());
if ($stockItem->getId() > 0 and $stockItem->getManageStock()) {
    $qty = 0;
    $stockItem->setQty($qty);
    $stockItem->setIsInStock((int)($qty > 0));
    $stockItem->save();
}
3
  • your answer concerns magento2, i have problem on magento 1.9 Jan 30 '20 at 11:13
  • please check my updated answer I have update for magento 1 Jan 30 '20 at 19:11
  • it's not working for my code, qty the amount still goes to minus Feb 3 '20 at 11:25

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.