首页 文章

我们需要传递json输入以使用magento 2 rest api将捆绑产品添加到购物车中

提问于
浏览
1

我想使用REST API将捆绑产品添加到购物车中

POSThttp://localhost/magento/rest/default/V1/carts/mine/items

input

{
  "cartItem": {
    "sku": "Bundle Product",
    "qty": 4,
    "quote_id": "29"
  }
}

但我从magento得到一个错误:“警告: Invalid argument supplied for foreach() in /opt/lampp/htdocs/magento/vendor/magento/module-bundle/Model/CartItemProcessor.php on line 87"

加入购物车简单的产品正在工作:

{
  "cartItem": {
    "sku": "Simple Product",
    "qty": 4,
    "quote_id": "29"
  }
}

请帮我解决这个问题 .

1 回答

  • 0

    这是解决方案 . 它基于Magento2(我看到不同的标签,但我认为,根据错误信息,你想要它用于Magento2) .

    假设我们的包由SKU "bundle01"和ID 123标识 .

    首先,让我们从产品中获取一些信息:

    GET /products/bundle01?searchCriteria

    并注意 "extension_attributes" --> "bundle_product_options" 部分,特别是与您的产品相关的 option_id 字段 . 并且不要考虑与产品相关的所有 product_links - > id 值 .

    假设我们有3个产品分别为option_id 643,644,645和id 704,705,706 .

    有了所有这些信息,以下是将捆绑产品添加到购物车的正文:

    {
    "cart_item": {
        "quote_id": <quote_id>,
        "sku": "bundle-01",
        "qty": 1,
        "product_option": {
        "extension_attributes": {
            "bundle_options": [{
                "option_id": 643,
                "option_qty": 1,
                "option_selections": [704]
                }, {
                "option_id": 644,
                "option_qty": 1,
                "option_selections": [705]
                }, {
                "option_id": 645,
                "option_qty": 1,
                "option_selections": [706]
                }]
            }
        }
    }
    }
    

相关问题