Magento 2 API Create Cart Customer

The following code will create a cart/quote for a logged-in customer.

 $magentoURL = "";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "", "password" => "phpcodez&");
 $data_string = json_encode($data);

$ch = curl_init($apiURL);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Content-Length: ".strlen($data_string)));
 $token = curl_exec($ch);
 $headers = array("Authorization: Bearer ".json_decode($token));
 $requestUrl =$magentoURL.'index.php/rest/V1/carts/mine';
 $ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 $result = curl_exec($ch);
 $result= json_decode($result);
 echo "<pre>";print_r($result);

Leave a Reply

Your email address will not be published. Required fields are marked *