如何通过Simple Product Magento 2 rest API进行配置

时间:2017-09-29 00:13:31

标签: rest api magento2 magento-2.0

问题很简单,如何通过REST API Magento 2通过Simple产品获取可配置产品?

我正在使用以下调用来获取简单的产品:

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

谢谢

1 个答案:

答案 0 :(得分:2)

我创建了一个新模块,它接受子产品ID作为参数,并返回父产品ID和其他属性,如name,thumbnail ....

registeration.php

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

等/ 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>

等/ 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>

等/ 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>

阿比/ ChildThumbnailManagementInterface.php

<?php

namespace WebAPI\GetParentProductThumbnail\Api;

interface ChildThumbnailManagementInterface
{


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

型号/ 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;
    }
}