Tag Archives: Quote

Magento 2 API Create Cart Customer

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

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/customer/token";
 $data = array("username" => "Test.User1521623008@phpcodez.com", "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);
 ?>