0

I want step by step Magento 2 REST API add to cart I am trying this post/rest/V1/guest-carts it generate cart Id , then post/rest/V1/guest-carts/:cart Id/items and I am getting error message like this

{
 "message": "Consumer is not authorized to access %resources",
 "parameters": {
 "resources": "Magento_Cart::manage"
 }
 }
Abhishek Tripathi
2,9152 gold badges21 silver badges38 bronze badges
asked Jul 13, 2017 at 6:59
1
  • i am using this code but not working add to cart . if($_REQUEST['action'] == 'Addtocart') { $username =$_POST['username']; $password = $_POST['password']; $token2 = $Cstm_api->custom_auth($username,$password); $token = $Cstm_api->custom_login($username,$password); $customerData = [ 'customer_id' => 64 ]; $url = $site_url.'rest/V1/carts/mine'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData)); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application Commented Feb 12, 2018 at 6:38

1 Answer 1

5

Try following way:

Step 1: Create auth token


$userData = array("username" => "admin", "password" => "admin123");
$ch = curl_init("http://magento.local.com/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);

Step 2: Load/Create Customer Quote


$customerData = [
 'customer_id' => 2
];
$ch = curl_init("http://magento.local.com/index.php/rest/V1/carts/mine");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$quote_id = json_decode($result, 1);
echo '<pre>';print_r($quote_id);

Step 3: Add product to cart


$productData = [
 'cart_item' => [
 'quote_id' => $quote_id,
 'sku' => '24-MB01',
 'qty' => 1
 ]
];
$ch = curl_init("http://magento.local.com/index.php/rest//V1/carts/mine/items");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "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)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result);

Step 4: Check current quote/cart status


$ch = curl_init("http://magento.local.com/index.php/rest//V1/carts/".$quote_id);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
$result = json_decode($result, 1);
echo '<pre>';print_r($result);
answered Jul 13, 2017 at 7:33
1
  • magento ver 1.9.2.3,I am work with REST API in POSTMAN, i have all oAuth 1.0 details like Consumer Key,Consumer Secret,Access Token,Token Secret. Now i moved to oAuth 2.0, how can i get my all oAuth 2.0 details? @Sohel Rana Commented Oct 30, 2018 at 6:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.