0

How to get parent product(Configurable) by child product(Simple) sku or id using REST API?

I can't find in this list: https://devdocs.magento.com/swagger/index_22.html

plz help me..

  • can't touch server any files, just using REST API – JHY Jun 20 at 8:56
0

Magento2 does not have any APi.

So you have to create a new API point for this requirement.

Create below files:

 1. app/code/Devbera/Configurable/etc/webapi.xml

 2. app/code/Devbera/Configurable/etc/di.xml

 3. app/code/Devbera/Configurable/Api/ConfigurableManagementInterface.php

 4. app/code/Devbera/Configurable/Api/ConfigurableManagementInterface.php

Define api point at webapi.xml.

And code is below:

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
  <route method="GET" url="/V1/devbera-configurable/:childId">
      <service class="Devbera\Configurable\Api\ConfigurableManagementInterface"
               method="getParentIdsByChild"/>
      <resources>
          <resource ref="anonymous"/>
      </resources>
  </route>
</routes>

And WebApi interface ConfigurableManagementInterface.php

And code is :

<?xml version="1.0" ?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
  <route method="GET" url="/V1/devbera-configurable/:childId">
      <service class="Devbera\Configurable\Api\ConfigurableManagementInterface"
               method="getParentIdsByChild"/>
      <resources>
          <resource ref="anonymous"/>
      </resources>
  </route>
</routes>

ConfigurableManagement.php for getting parent product

<?php
/**
 * A Magento 2 module named Devbera/Configurable
 * Copyright (C) 2019 amitbera.com
 *
 * This file included in Devbera/Configurable is licensed under OSL 3.0
 *
 * http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * Please see LICENSE.txt for the full text of the OSL 3.0 license
 */

namespace Devbera\Configurable\Model;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class ConfigurableManagement implements \Devbera\Configurable\Api\ConfigurableManagementInterface
{
    /**
     * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
     */
    private $configurable;
    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    private $productRepository;
    /**
     * @var \Magento\Catalog\Api\Data\ProductInterfaceFactory
     */
    private $productFactory;

    public function __construct(
       Configurable $configurable,
       \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
       \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory
    ) {
        $this->configurable = $configurable;
        $this->productRepository = $productRepository;
        $this->productFactory = $productFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function getParentIdsByChild($childId)
    {
        $parentIds = $this->configurable->getParentIdsByChild($childId);

        $parentList = [];
        if (!empty($parentIds)) {
            foreach ($parentIds as $parentId) {
                $parentList[] = $this->productRepository->getById($parentId);
            }
        }

        return $parentList;
    }
}

And di.xml for rewrite interface with Model management class

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  <preference for="Devbera\Configurable\Api\ConfigurableManagementInterface"
              type="Devbera\Configurable\Model\ConfigurableManagement"/>
</config>

Full module avaliable at Github

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.