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

Php 如何在Magento 2 REST API响应中的产品链接中显示产品id?,php,rest,magento,Php,Rest,Magento,当我在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

当我在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"
    }
  ]
}
在下面显示产品链接的部分中,我需要显示另一个参数Product 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。有人能帮我解决这个问题吗?

您需要在
\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'])) {
...