Magento 2 REST API - 通过 slug 获取产品,或在 REST 搜索中获取媒体



我有一个余烬应用程序,我正在使用它作为前端。我需要从 REST API 获取产品,但我需要使用 slug,而不是使用 SKU。所以我访问以下工作正常的端点:http://*.com/index.php/rest/V1/products?searchCriteria[filter_groups][0][filters][0][field]=url_key&searchCriteria[filter_groups][0][filters][0][value]=daniels-icecream-slug

但是,结果显然是一个产品列表,而不是产品端点,因此省略了一些数据。即media_gallery_entries字段。那么,无论如何,我可以在/products?searchCriteria端点中返回此数据,或者有没有办法获取/products/:slug而不是为产品端点/products/:sku

你需要用API调用来定义conditionType,如下所示

V1/products/?searchCriteria[filterGroups][0][filters][0][field]=url_key&searchCriteria[filterGroups][0][filters][0][value]=%shirt%&searchCriteria[filterGroups][0][filters][0][condition_type]=like

参数:

searchCriteria[filterGroups][0][filters][0][field]=url_key
searchCriteria[filterGroups][0][filters][0][value]=%shirt%
searchCriteria[filterGroups][0][filters][0][condition_type]=like

注意:确保根据您的要求在值中前缀和后缀%

我在我的 api 调用中使用相同的,它可以工作

我使用的是Magento v2.2,当我进行搜索时,每个项目都有一个image属性(在custom_attributes列表中),mangento在向上添加图像时会自动将其添加到产品中:

{
  "items": [{
    "sku": "MH07-XS-Black",
    "name": "Hero Hoodie-XS-Black",
    "custom_attributes": [{
        "attribute_code": "description",
        "value": "<p>Gray and black color blocking sets you apart as the Hero Hoodie keeps you warm on the bus, campus or cold mean streets. Slanted outsize front pockets keep your style real . . . convenient.</p>n<p>&bull; Full-zip gray and black hoodie.<br />&bull; Ribbed hem.<br />&bull; Standard fit.<br />&bull; Drawcord hood cinch.<br />&bull; Water-resistant coating.</p>"
      },
      {
        "attribute_code": "image",
        "value": "/m/h/mh07-black_main.jpg"
      },
      {
        "attribute_code": "small_image",
        "value": "/m/h/mh07-black_main.jpg"
      },
      {
        "attribute_code": "thumbnail",
        "value": "/m/h/mh07-black_main.jpg"
      },
      {
        "attribute_code": "color",
        "value": "49"
      },
      {
        "attribute_code": "minimal_price",
        "value": "54.0000"
      },
      {
        "attribute_code": "category_ids",
        "value": [
          "15"
        ]
      },
      {
        "attribute_code": "options_container",
        "value": "container2"
      },
      {
        "attribute_code": "required_options",
        "value": "0"
      },
      {
        "attribute_code": "has_options",
        "value": "0"
      },
      {
        "attribute_code": "url_key",
        "value": "hero-hoodie-xs-black"
      },
      {
        "attribute_code": "msrp_display_actual_price_type",
        "value": "0"
      },
      {
        "attribute_code": "tax_class_id",
        "value": "2"
      },
      {
        "attribute_code": "size",
        "value": "167"
      }
    ]
  }]
}

最新更新