简体   繁体   中英

How get configurable by Simple Product Magento 2 rest API

The question is simple, how can I get the Configurable Product through a Simple product through the REST API Magento 2?

I'm using the following call to get the simple product:

http://127.0.0.1/magento2/index.php/rest/V1/products/prdConfig-RED

Thank you

I have created a new module which accepts child product id as parameter and returns the parent product id and other attributes like name , thumbnail....

registeration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'WebAPI_GetParentProductThumbnail',
    __DIR__
);

etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="WebAPI_GetParentProductThumbnail" setup_version="1.0.0"/>
</config>

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">
    <preference for="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" type="WebAPI\GetParentProductThumbnail\Model\ChildThumbnailManagement"/>
</config>

etc/webapi.xml

<?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/webapi-getparentproductthumbnail/childthumbnail">
        <service class="WebAPI\GetParentProductThumbnail\Api\ChildThumbnailManagementInterface" method="getChildThumbnail"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Api/ChildThumbnailManagementInterface.php

<?php

namespace WebAPI\GetParentProductThumbnail\Api;

interface ChildThumbnailManagementInterface
{


    /**
     * GET for ChildThumbnail api
     * @param string $product_id
     * @return string
     */
    public function getChildThumbnail($product_id);
}

Model/ChildThumbnailManagement.php

<?php


namespace WebAPI\GetParentProductThumbnail\Model;

class ChildThumbnailManagement
{


    /**
     * {@inheritdoc}
     */
    public function getChildThumbnail($product_id)
    {
        $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        

        if($product_id != ""){
            //This method getParentIdsByChild($child_id) get the parent id of a configurable product.
            $parent_product = $objectManager->create('Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable')->getParentIdsByChild($product_id);
            if(isset($parent_product[0]))
            {
                $parent_id = $parent_product[0];

                //Parent object where you can get Thumbnail, name.... etc
                //$parent_object = $objectManager->create('Magento\Catalog\Model\Product')->load($parent_product[0]);

                echo parent_id;
            }
        }
        return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM