1

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..

1
  • can't touch server any files, just using REST API Commented Jun 20, 2019 at 8:56

1 Answer 1

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

2
  • 1
    Wow, seems like an oversight for the API. I can think of plenty of use cases for want to grab the parent product based on the child. Is this still true today? Commented Sep 1, 2023 at 19:15
  • @DanielBlack as far as I know, yes. Commented May 16, 2024 at 15:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.