4

I need to add product in cart using magento 2 api.

I am using below api

"rest/V1/guest-carts/{cart_id}/items

I am passing parameters like below,

    {
    "cartItem": {
        "sku": "paneer_chilli-1",
        "qty": "1",
        "name": "Paneer Chilli",
        "price": 100,
        "product_type": "simple",
        "quote_id": "3a43e2fc1ed26db195a8ceee00929bcb",
        "product_option": {
            "extension_attributes": {
                "custom_options": [{
                    "option_id": 6,
                    "option_value": 2
                }]
            }
        }
    }
}

dosent work, it is giving error like below,

Property "CustomOptions" does not have corresponding setter in class "Magento\Quote\Api\Data\CartItemExtensionInterface".

What is option_id and option_value in my case when I have to pass custom option with multiple option with its price (checkbox)

It will be great if someone can help. Thanks

2 Answers 2

0

Go to http://devdocs.magento.com/swagger/ > quoteGuestCartItemRepositoryV1

In your case, should change to underscore format

{
    "cartItem": {
        "quote_id": {cart id},
        "sku": {sku},
        "qty": 1,
        "product_option":{
            "extension_attributes":{
                "custom_options":[
                    {
                        "option_id":"1",
                        "option_value":"2"
                    }
                    ]
            }
        }
    }
}
2
  • 1
    Hey, Thanks for you reply, but now it is giving, "Please specify product's required option(s)." i am updating my latest json object in question, it will be great of you can suggest something.
    – Anand
    Jan 21, 2017 at 15:17
  • The values and option id should match exactly. Please check the options present to the product by using SKU than you can place exact values which makes move forward from above error Sep 16, 2018 at 3:34
0

If anyone is running into the same issue and looking for a solution, to add a product with customized options selected through the guest-carts rest api in the original question the custom_options attribute was incorrect at least in one way.

Adding the product to the cart will only work when the key is set to the option's ID and the value to the option's value ID

Example

   "cartItem": {
        "sku": "paneer_chilli-1",
        "qty": "1",
        "name": "Paneer Chilli",
        "price": 100,
        "product_type": "simple",
        "quote_id": "3a43e2fc1ed26db195a8ceee00929bcb",
        "product_option": {
            "extension_attributes": {
                "custom_options": ["522465": 403826]
            }
        }
    }

To verify you are using the absolute correct ID's for example when you keep receiving the error Property "CustomOptions" does not have corresponding setter in class "Magento\Quote\Api\Data\CartItemExtensionInterface". it's worth doublechecking through this query using product's ID which will output list of correct possbile combinations of custom options:

SELECT opt.option_id, val.option_type_id, val.sku
FROM catalog_product_option AS opt
         LEFT JOIN catalog_product_option_type_value AS val ON val.option_id = opt.option_id
WHERE opt.product_id = 1234; # <<== YOUR_PRODUCT_ID

# OUTPUT:
# | option_id | option_type_id | sku          |
# | :---      | :---           | :---         |
# | 522465    | 403826         | mild-spicy   |
# | 522465    | 403827         | extra-spicy  |
# | 522465    | 403828         | black-pepper |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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