1

I am building mobile application using Magento 2, I had an old defined cart which had a product and when checking out it gives an error saying the item is out of stock, that happened after payment and placing the order. I checked this product in backend and found its Quantity was 0 and out of stock. checked with my friend and knew that he bought it in a test case.

Ho can I fix this problem and check all products in cart are in stock OR out of stock Using REST API?

0

I am not sure I understand your question but you can add a new extension attributes that check stock status in rest/V1/carts/mine/items and display the appropriate message.

Create your extension attribute in Vendor/ModuleName/etc/webapi_rest/extension_attributes.xml

<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
    <attribute code="stock" type="string" />
</extension_attributes>

Then Create your plugin to overwrite Magento\Quote\Model\Quote\Item\Repository::getList

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\Repository">
        <plugin name="stock" type="Vendor\ModuleName\Plugin\Stock" disabled="false" sortOrder="70"/>
    </type>
</config>

Then Create Stock.php in Vendor\ModuleName\Plugin\Stock.php

In Stock.php, we overwrite GetList with aroundplugin. We also get StockRegistryInterface to check If item is in stock by sku. Then we pass the message to our extension interface factory.

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\ModuleName\Plugin;
use Magento\Quote\Model\Quote\Item\Repository as ItemRepository;

class Stock
{
    protected $quoteRepository;
    protected $cartItemExtensionFactory;
    protected $stockRegistry;


    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Quote\Api\Data\CartItemExtensionFactory $cartItemExtensionFactory,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->stockRegistry = $stockRegistry;
        $this->cartItemExtensionFactory = $cartItemExtensionFactory;
    }

    public function aroundGetList(ItemRepository $subject, \Closure $proceed, $cartId)
    {
        $cartItems = $proceed($cartId);
        /** @var  \Magento\Quote\Model\Quote $quote */
        $quote = $this->quoteRepository->getActive($cartId);

        /** @var \Magento\Quote\Api\Data\CartItemExtensionInterface $extensionAttributes */
          $i = 0;
        foreach ($quote->getAllVisibleItems() as $item) {
            $extensionAttributes = $cartItems[$i]->getExtensionAttributes();
            if ($extensionAttributes === null) {
                $extensionAttributes = $this->cartItemExtensionFactory->create();
            }
            $sku = $item->getSku();
            $stock = $this->stockRegistry->getStockItemBySku($sku)->getIsInStock();
            if ($stock) {
                $message = "Item In Stock";
            } else {
                $message = "Item Out Of Stock";
            }
            $extensionAttributes->setStock($message);
            $cartItems[$i]->setExtensionAttributes($extensionAttributes);
            $i++;
        }
       // $cartItems->setExtensionAttributes($extensionAttributes);

        return $cartItems;
    }
}

Thanks

  • i think we can use a boolean flag <attribute code="stock" type="boolean" /> if ($stock) { $message = true; } else { $message = false; } – sivakumar Jun 27 '18 at 13:30
  • Thanks @sivakumar. Boolean is obviously better. But this still return a string of "0" and "1". Any reason for this?? – Soladogun Ilyas Ademuyiwa Jun 28 '18 at 5:51
  • This code give error :Fatal error</b>: Uncaught Error: Call to undefined method Magento\Quote\Api\Data\CartItemExtension::setStock() in /Vendor/CheckStock/Plugin/Stock.php:46 @SoladogunIlyasAdemuyiwa – Lovely Setia Dec 5 '18 at 9:50
  • @LovelySetia Make sure you recompile. It should generate that attribute setStock in the generated folder since attribute code is stock – Soladogun Ilyas Ademuyiwa Dec 6 '18 at 11:01
  • I will deleted var/generation/*,var/page_cache/*,var/cache/*.After that run Setup:upgrade,setup:di:compile,setup:static-content:deploy commands.It still showing error @SoladogunIlyasAdemuyiwa – Lovely Setia Dec 6 '18 at 14:24

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.