3

Addtocart Rest api for Configurable product in magento2 ?

1

Add a configurable product to a cart

To add a configurable product to a cart, you must specify the sku as well as the set of option_id/option_value pairs that make the product configurable.

In this example, we’ll add the Chaz Kangeroo Hoodie (sku: MH01) configurable product to the cart. This product comes in three colors (black, gray, and orange) and five sizes (XS, S, M, L, XL). In the sample data, the option_id values for Size and Color are 141 and 93, respectively. You can use the GET /V1/configurable-products/:sku/options/all call to determine the option_id values for the given SKU.

The GET /V1/configurable-products/:sku/children call returns information about each combination of color and size, 15 in all for MH01. The following sample shows the returned values for size and color for a small gray Chaz Kangeroo Hoodie.

{
  "custom_attributes": [
    {
      "attribute_code": "size",
      "value": "168"
    },
    {
      "attribute_code": "color",
      "value": "52"
    }
  ]
}

We now know the values for option_value for size and color are 168 and 52, so we’re ready to add the product to the cart.

Endpoint

POST /rest//V1/carts/mine/items

Headers

Content-Type application/json

Authorization Bearer

Payload

{
  "cartItem": {
    "sku": "MH01",
    "qty": 1,
    "quote_id": "4",
    "product_option": {
      "extension_attributes": {
        "configurable_item_options": [
          {
            "option_id": "93",
            "option_value": 52
          },
          {
            "option_id": "141",
            "option_value": 168
          }
        ]
      }
    },
    "extension_attributes": {}
  }
}

Response

{
    "item_id": 13,
    "sku": "MH01-S-Gray",
    "qty": 1,
    "name": "Chaz Kangeroo Hoodie",
    "price": 52,
    "product_type": "configurable",
    "quote_id": "4",
    "product_option": {
        "extension_attributes": {
            "configurable_item_options": [
                {
                    "option_id": "93",
                    "option_value": 52
                },
                {
                    "option_id": "141",
                    "option_value": 168
                }
            ]
        }
    }
}

Reference: https://devdocs.magento.com/guides/v2.3/rest/tutorials/orders/order-add-items.html

I hope this will help

| improve this answer | |
  • not working $productData = [ "cartItem"=> [ "sku" => "WH11", "qty" => 1, "quote_id" => $quote_id, "product_option" => [ "extension_attributes"=> [ "configurable_item_options"=> [ [ "option_id"=> "93", "option_value"=> 50 ],[ "option_id" => "141", "option_value" => 166 ] ] ] ] ] ]; – user1500430 Aug 2 '19 at 11:46
  • $ch = curl_init("example.com/rest/V1/carts/mine/items"); curl_setopt($ch, CURL OPT_CUSTOM REQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($productData)); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token))); – user1500430 Aug 2 '19 at 11:47
  • My bad i missed out the option value . Its working fine now – user1500430 Aug 2 '19 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.