Tag Archives: Customer

Magento 2 API Order Details

The following code will return the the complete list of orders.

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $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);


 $requestUrl =$magentoURL.'index.php/rest/V1/orders?searchCriteria';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Magento 2 API Order Details

The following code will return the order details.All we have to do is that we have to pass the order id

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "apiuser", "password" => "apiuserpwd");
 $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);

$requestUrl =$magentoURL.'index.php/rest/V1/orders/3';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Magento 2 API Create an order

The following code will convert the quote to order and will return the order id.

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

$customerData =[
 'paymentMethod' => [
 "method" => 'quickpay'
 ],
 'billing_address' => [
 "region" => 'New York',
 "region_id" => "43",
 "region_code" => "NY",
 "country_id" => "US",
 "street" => array("123 Oak Ave"),
 "postcode" => "10577",
 "city" => "Purchase",
 "firstname" => "Pramod",
 "lastname" => "Prasad",
 "customer_id" => "16",
 "email" => "Test.User1521623008@brandmuscle.com",
 "telephone" => "(512) 555-1111",
 ],
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/payment-information';

$ch = curl_init($requestUrl);
 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 = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);
?>

Magento 2 API Estimate shipping costs

The following code will give you the shipping method available for a given address.

<?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" => "phpcodezpwd");
 $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);

$customerData = [
 'address' => [
 "region" => 'New York',
 "region_id" => "43",
 "region_code" => "NY",
 "country_id" => "US",
 "street" => array("123 Oak Ave"),
 "postcode" => "10577",
 "city" => "Purchase",
 "firstname" => "Pramod",
 "lastname" => "Prasad",
 "customer_id" => "43",
 "email" => "Test.User1521623008@brandmuscle.com",
 "telephone" => "(512) 555-1111",
 "same_as_billing" => "1"
 ]
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/estimate-shipping-methods';

$ch = curl_init($requestUrl);
 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 = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);

?>

Magento 2 API Add Product To Cart

The following code will add product to 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" => "phpcodezpwd");
 $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);

$quoteUrl =$magentoURL.'index.php/rest/V1/carts/mine';

$chQuote = curl_init($quoteUrl);
 curl_setopt($chQuote, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 curl_setopt($chQuote, CURLOPT_RETURNTRANSFER, true);
 $quote = json_decode(curl_exec($chQuote));
 
 $cartData = [
 'cartItem' => [
 "quote_id" => $quote->id,
 "sku" => "152110360696",
 "qty" => 2
 ]
 ];

$requestUrl =$magentoURL.'index.php/rest/V1/carts/mine/items';

$ch = curl_init($requestUrl);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($cartData));
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 $result = json_decode(curl_exec($ch));
 echo "<pre>";print_r($result);

?>

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);
 ?>

Magento 2 API Create Customer

Here I am creating a customer using REST API

<?php
 $magentoURL = "http://127.0.0.1/mage2/";
 $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token";
 $data = array("username" => "APIUSER", "password" => "APIUSERPWD");
 $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);

$customerData = [
 'customer' => [
 "email" => 'Test.User'.time().'@phpcodez.com',
 "firstname" => "Brand",
 "lastname" => "Muscle",
 "storeId" => 1,
 "websiteId" => 1
 ],
 "password" => "PHPCodez1&"
 ];

$ch = curl_init($magentoURL."index.php/rest/V1/customers");
 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);

$result = json_decode($result, 1);
 echo '<pre>';print_r($result);
?>