2

So I am trying to go for a headless magento approach.

I am using the following API to get product details:

V1/products/:sku

If the product is a bundle product, it shows bundle_product_options in the response as follows:

"bundle_product_options": [
        {
            "option_id": 6,
            "title": "Option Title",
            "required": true,
            "type": "select",
            "position": 1,
            "sku": "Option SKU",
            "product_links": [
                {
                    "id": "52",
                    "sku": "Child Product SKU",
                    "option_id": 6,
                    "qty": 1,
                    "position": 1,
                    "is_default": false,
                    "price": 0,
                    "price_type": 0,
                    "can_change_quantity": 1
                },

I want to add the following information in the product_links section:

Child Product Name and Child Product quantity left in stocks

I have created a plugin as follows:

etc/di.xml

<?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\Bundle\Api\ProductLinkManagementInterface">
         <plugin name="get_linked_product_metrics" type="Vendor\Module\Plugin\LinkedProductGet"/>
      </type>
   </config>

etc/extension_attribute.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
      <extension_attributes for="Magento\Bundle\Api\Data\LinkInterface">
          <attribute code="stockQty" type="string"/>
      </extension_attributes>

      <extension_attributes for="Magento\Bundle\Api\Data\LinkInterface">
          <attribute code="name" type="string"/>
       </extension_attributes>
  </config>

Vendor/Module/Plugin/LinkedProductGet.php

<?php

   namespace Vendor\Module\Plugin;

   use Magento\Bundle\Api\Data\LinkInterface;

   class LinkedProductGet{
      protected $productExtensionFactory;
      protected $productFactory;

      public function __construct(
         \Magento\Catalog\Api\Data\ProductExtensionFactory $productExtensionFactory,
         \Magento\Catalog\Model\ProductFactory $productFactory,
         \Magento\CatalogInventory\Api\StockStateInterface $stockItem,
         \Magento\Store\Model\StoreManagerInterface $storeManager,
         \Magento\Eav\Model\Config $eavConfig
      )
     {
        $this->productFactory = $productFactory;
        $this->storeManager = $storeManager;
        $this->stockItem = $stockItem;
        $this->productExtensionFactory = $productExtensionFactory;
        $this->eavConfig = $eavConfig;
     }

     /**
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $subject
     * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $products
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
     public function afterGetProductLinks(
         \Magento\Bundle\Api\ProductOptionRepositoryInterface $subject,
         $products
     )
     {
         /** @var  $product */
         foreach ($products->getItems() as $key => $product) {

                 $title = $this->getName();
                 $extensionattributes = $product->getExtensionAttributes();
                 $extensionattributes->setName($title);
                 $product->setExtensionAttributes($extensionattributes);
                 $qty = $this->stockItem->getStockQty($product->getId(), $product->getStore()->getWebsiteId());
                 $extensionattributes = $product->getExtensionAttributes();
                 $extensionattributes->setStockQty($qty);
                 $product->setExtensionAttributes($extensionattributes);
         }
         return $products;
      }
   }

But I am getting no extra field in the desired section. What could I be doing wrong? Any help will be appreciated!

I used this guide: How can we get stock data in product listing via afterGetList plugin in Magento 2.2.3

Your Answer

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

Browse other questions tagged or ask your own question.