首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Magento 2 REST API响应中显示产品链接中的产品id?

如何在Magento 2 REST API响应中显示产品链接中的产品id?
EN

Stack Overflow用户
提问于 2016-12-12 20:20:46
回答 1查看 657关注 0票数 2

当我在magento 2中点击下面的REST API时,我得到了以下响应

代码语言:javascript
复制
http://10.201.62.75/e-cart/rest/default/V1/products/iPhone%207

我得到的回应是

代码语言:javascript
复制
{
  "id": 40,
  "sku": "iPhone 7",
  "name": "iPhone 7",
  "attribute_set_id": 11,
  "price": 999,
  "status": 1,
  "visibility": 4,
  "type_id": "simple",
  "created_at": "2016-12-08 12:16:20",
  "updated_at": "2016-12-08 12:16:20",
  "weight": 0.2,
  "extension_attributes": [],
  "product_links": [
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "Gold Leather cases",
      "linked_product_type": "simple",
      "position": 4,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "Silver Leather cases",
      "linked_product_type": "simple",
      "position": 3,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "Platinum Leather cases",
      "linked_product_type": "simple",
      "position": 2,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "Black Leather cases",
      "linked_product_type": "simple",
      "position": 1,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "iPhone 7S Wireless Charger",
      "linked_product_type": "simple",
      "position": 6,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "iPhone 7S Earphones White",
      "linked_product_type": "simple",
      "position": 5,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "upsell",
      "linked_product_sku": "iPhone 6",
      "linked_product_type": "simple",
      "position": 1,
      "extension_attributes": []
    },
    {
      "sku": "iPhone 7",
      "link_type": "upsell",
      "linked_product_sku": "iPhone 6 Plus",
      "linked_product_type": "simple",
      "position": 2,
      "extension_attributes": []
    }
  ],
  "options": [],
  "media_gallery_entries": [
    {
      "id": 43,
      "media_type": "image",
      "label": "",
      "position": 1,
      "disabled": false,
      "types": [
        "image",
        "small_image",
        "thumbnail",
        "swatch_image"
      ],
      "file": "/i/p/iphone6.jpg"
    }
  ],
  "tier_prices": [],
  "custom_attributes": [
    {
      "attribute_code": "meta_title",
      "value": "iPhone 7"
    },
    {
      "attribute_code": "meta_keyword",
      "value": "iPhone 7"
    },
    {
      "attribute_code": "meta_description",
      "value": "iPhone 7 "
    },
    {
      "attribute_code": "image",
      "value": "/i/p/iphone6.jpg"
    },
    {
      "attribute_code": "small_image",
      "value": "/i/p/iphone6.jpg"
    },
    {
      "attribute_code": "thumbnail",
      "value": "/i/p/iphone6.jpg"
    },
    {
      "attribute_code": "news_from_date",
      "value": "2016-07-27 00:00:00"
    },
    {
      "attribute_code": "news_to_date",
      "value": "2021-07-22 00:00:00"
    },
    {
      "attribute_code": "custom_design",
      "value": "2"
    },
    {
      "attribute_code": "category_ids",
      "value": [
        "2",
        "3",
        "4",
        "5"
      ]
    },
    {
      "attribute_code": "options_container",
      "value": "container2"
    },
    {
      "attribute_code": "required_options",
      "value": "0"
    },
    {
      "attribute_code": "has_options",
      "value": "0"
    },
    {
      "attribute_code": "country_of_manufacture",
      "value": "IN"
    },
    {
      "attribute_code": "url_key",
      "value": "iphone-7"
    },
    {
      "attribute_code": "swatch_image",
      "value": "/i/p/iphone6.jpg"
    },
    {
      "attribute_code": "tax_class_id",
      "value": "2"
    },
    {
      "attribute_code": "gift_message_available",
      "value": "2"
    },
    {
      "attribute_code": "model",
      "value": "iphone"
    },
    {
      "attribute_code": "color",
      "value": "17"
    },
    {
      "attribute_code": "device_memory",
      "value": "25"
    },
    {
      "attribute_code": "brand",
      "value": "26"
    },
    {
      "attribute_code": "operating_system_mobile",
      "value": "32"
    },
    {
      "attribute_code": "processor_speed",
      "value": "35"
    },
    {
      "attribute_code": "screen_size",
      "value": "36"
    },
    {
      "attribute_code": "ram",
      "value": "44"
    },
    {
      "attribute_code": "camera",
      "value": "47"
    },
    {
      "attribute_code": "product_id",
      "value": "iPhone7"
    }
  ]
}

在下面显示产品链接的部分中,我需要另一个参数产品id来显示所有详细信息。

代码语言:javascript
复制
 "product_links": [
    {
      /////////need product id here////////////
      "sku": "iPhone 7",
      "link_type": "related",
      "linked_product_sku": "Gold Leather cases",
      "linked_product_type": "simple",
      "position": 4,
      "extension_attributes": []
    },
    {

在上面的描述中,我需要在sku之前使用产品id。有人能帮我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-04-20 18:01:14

您需要在\Magento\Catalog\Model\ProductLink\Repository::getList更改逻辑

代码语言:javascript
复制
foreach (array_keys($linkTypes) as $linkTypeName) {
        $collection = $this->entityCollectionProvider->getCollection($product, $linkTypeName);
        foreach ($collection as $item) {
            /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $productLink */
            $productLink = $this->productLinkFactory->create();
            $productLink->setSku($product->getSku())
                ->setLinkType($linkTypeName)
                ->setLinkedProductSku($item['sku'])
                ->setLinkedProductType($item['type'])
                ->setPosition($item['position']);
            if (isset($item['custom_attributes'])) {
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41100828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档