【问题标题】:How to show product id in product links of a product in Magento 2 REST API Response?如何在 Magento 2 REST API 响应中的产品链接中显示产品 ID?
【发布时间】:2016-12-12 12:20:45
【问题描述】:

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

http://10.201.62.75/e-cart/rest/default/V1/products/iPhone%207

我得到的回应是

{
  "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 与所有详细信息一起显示。

 "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。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: php rest magento


    【解决方案1】:

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

    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'])) {
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-09-29
      相关资源
      最近更新 更多