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"
        }
    }
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);
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 – zus Oct 30 '18 at 6:25
0

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/json", "Authorization: Bearer " . json_decode($token2)));

    $result = curl_exec($ch);

    $quote_id = json_decode($result, 1);

// print_r($quote_id); die; 135 $productData = [ 'cart_item' => [ 'quote_id' => $quote_id, "cartId" => '1', 'sku' => 'RB0001', 'qty' => 1, 'customer_id' => 64, 'product_type' => 'configurable',

        ]
    ];  

  $url = $site_url.'rest/V1/carts/mine/items';

  //    print_r($url); die;
    $ch = curl_init($url);
    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($token2)));

    $result = curl_exec($ch);
    print_r($result); die;
    $result = json_decode($result, 1);
    echo '<pre>';print_r($result);

this is Error= {"message":"Property \"CartId\" does not have corresponding setter in class \"Magento\Quote\Api\Data\CartItemInterface\".",

please give me solution

1
  • Please post your query as a question. – Gammer Sep 3 '20 at 15:30

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.