【问题标题】:How get configurable by Simple Product Magento 2 rest API如何通过 Simple Product Magento 2 rest API 进行配置
【发布时间】:2017-09-29 00:13:30
【问题描述】:

问题很简单,如何通过 REST API Magento 2 通过 Simple product 获取 Configurable Product?

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

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

谢谢

【问题讨论】:

    标签: rest api magento2 magento-2.0


    【解决方案1】:

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

    注册.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);
    }
    

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

    【讨论】:

    • 你能粘贴registeration.php的完整路径吗?因为 Magento 目录中有很多相同的文件可用。是否有任何 API 可用于获取当前 SKU?
    • registeration.php 应该在新模块里面创建:Vender_Name/Module_Name
    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多