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
-
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: applicationvinod kushwaha– vinod kushwaha2018年02月12日 06:38:20 +00:00Commented Feb 12, 2018 at 6:38
1 Answer 1
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
Sohel Rana
36.2k3 gold badges74 silver badges94 bronze badges
-
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 Ranazus– zus2018年10月30日 06:25:33 +00:00Commented Oct 30, 2018 at 6:25
default