0

I want to add products in cart in Magento 2 using Rest API

I have created a controller and using this code.

<?php

    $userData = array("username" => "admin", "password" => "password");
    $ch = curl_init("http://127.0.0.1/mag/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);

    $customerData = [
        'customer_id' => 12
    ];
    $ch = curl_init("http://127.0.0.1/mag/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>';var_dump($quote_id);


    $productData = [
        'cart_item' => [
            'quote_id' => $quote_id,
            'sku' => 'test',
            'qty' => 1
        ]
    ];
    $ch = curl_init("http://127.0.0.1/mag/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>';var_dump($result);

    $ch = curl_init("http://127.0.0.1/mag/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>';var_dump($result);

It doesn't show any error but it doesn't add that product in cart i have checked customer id

$customerData = [
        'customer_id' => 12
    ];

for my current logged in user from Database 'customer_entity' table and 'entity_id' row. Any one know how to add that and view it from fronted. Any help is appreciated.

1 Answer 1

0

You should follow the official docs here: https://devdocs.magento.com/guides/v2.4/rest/tutorials/orders/order-intro.html

First, do one by one step in Postman or any tool that fit you. The reason for this is, you know what should input, what the type of it.

After everything is succeeded in tool, you just need to convert them to curl command.

Your code doesn't work because the required token is customer's token, you are using admin token (I stop looking at your code from here so you should continue to follow the tutorial above)

4
  • Thanks for your response @jimmy it will perfectly working for logged in user. Will you please tell me how it will work for guest user.
    – Prits
    Mar 19, 2021 at 7:45
  • Guest is also documented Use the V1/guest-carts endpoint to create a cart on behalf of a guest. Do not include an authorization token. The quoteId for the guest customer quote will be masked.
    – Jimmy
    Mar 19, 2021 at 7:47
  • What that mean is you cannot use quoteId as plain text such as 1,2,3. You should use marked Id, which is linked in the quote_id_mask table
    – Jimmy
    Mar 19, 2021 at 7:48
  • Thanks for the update @jimmy I am using postman to get the data but on 127.0.0.1/mag/rest/V1/guest-carts/81/items this API hit with that parameter { "cart_item": { "quoteId": 81, "sku": "test", "qty": 1, "quote_id": "HWgMbh8XAVWzV9t5Bu5GQ6HeVjUYCcA6" } } but still the error comes up is "message": "\"%fieldName\" is required. Enter and try again.", "parameters": { "fieldName": "quoteId" } any idea regarding this.
    – Prits
    Mar 19, 2021 at 11:20

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.