PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How to add an Item to a quote/cart via API on Magento2

 August 22, 2022     api, magento2, rest     No comments   

Issue

I have tried calling [POST] /carts/mine/items, headers with correct bearer, and body:

{
    "cart_item": 1,
    "sku": "MY_SKU",
    "qty": 1
}

and I get the folowing response:

{
   "message": "Invalid value of \"%value\" provided for the %fieldName field.",
   "parameters": {
      "fieldName": "qty",
      "value": null
   }
}

Two things, I do not understand what to put in cart_item (but it is required) and I do not why it keeps telling me qty is null?


Solution

First of all empty cart should be created using request with empty body:

[POST] {base URL}/rest/V1/carts/mine

In response you will get ID of your quote.

Now you can add items to your cart using:

[POST] {base URL}/rest/V1/carts/mine/items
{
  "cart_item": {
    "quote_id": <cart ID received from previous call>,
    "sku": "product_sku",
    "qty": 10
  }
}

In response you should get your cart item data:

{
  "item_id": 1,
  "sku": "product_sku",
  "qty": 10,
  "name": "Simple Product",
  "price": 123,
  "product_type": "simple",
  "quote_id": "1"
}

Be careful since you may accidentally update existing cart item quantity with POST request, if execute the same request several times.



Answered By - Alex Paliarush
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing