0

I am trying to use plugin to override Magento\Checkout\Model\Cart.php - addOrderItem function

Instead of getting item's quantity, how to check if product is salable using isSalable()? Is there any way to use product id to get if product is isSalable().

public function aroundAddOrderItem($subject, callable $proceed, $orderItem, $qtyFlag = null)
    {
        if ($orderItem->getParentItem() === null) {
            $sku = $orderItem->getSku();
            $productId=$this->product->getIdBySku($sku);
            $productStock = $this->stockItemRepository->get($productId);
            $productQty = $productStock->getQty();
            
            if($productQty < 1)
            {
                return $this;
            }
            else
            {
                return $proceed($orderItem, $qtyFlag);
            }
        }
        return $this;
    }

1 Answer 1

0

You can check product is isSalable by below code using SKU.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();        
    $sku = $orderItem->getSku();
    $salable = $objectManager->create('Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku')->execute($sku);
    foreach ($salable as $key => $value) {
         $salableQty = $value['qty'];
     }

You should not use the ObjectManager directly!

1
  • can I not use isSalable() something like $product->isSalable()? Sep 28, 2020 at 14:07

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.