문제

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;
    }
도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top