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

0

Recently I faced the same problem. Here is my code:

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\Model\Product\LinksList">
        <plugin name="get_bundle_product_child_name" type="Chihuo\WechatApp\Plugin\ChildName"/>
    </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="name" type="string" />
    </extension_attributes>
  </config>

Plugin/ChildName.php

<?php
namespace Vender\Module\Plugin;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;

class ChildName
{
    protected $productRepository;
    protected $getSalableQuantityDataBySku;
    protected $getProductSalableQty;
    public function __construct(
        ProductRepositoryInterface $productRepository,
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
        GetProductSalableQtyInterface $getProductSalableQty
    ) {
        $this->productRepository = $productRepository;
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
        $this->getProductSalableQty = $getProductSalableQty;
    }

    public function afterGetItems(
        \Magento\Bundle\Model\Product\LinksList $subject,
       $childrenList
    ) {
        foreach ($childrenList as $child) {
            $linkExtension = $child->getExtensionAttributes();
            $sku = $child->getSku();
            $product = $this->productRepository->get($sku);
            $linkExtension->setName($product->getName());
            $child->setExtensionAttributes($linkExtension);
        }
        return $childrenList;
    }
}

Stock Qty could be set in the same strategy. Hope this helps.

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.