The following code will help to set shipping and billing information. When you execute this script, in return it will give you available payment methods. Continue reading Magento 2 API set shipping and billing information
Tag Archives: Magento 2
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 Customer Details
The following code will show you the details of 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/customers/me'; $ch = curl_init($requestUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); //decoding result $result= json_decode($result); //printing result echo "<pre>";print_r($result); ?>
Magento 2 API Customer Login
You can use the following code for this tas.
<?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" => "Phpcodez1&"); $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); echo($token); ?>
Magento 2 Module Status
Run the below command to see the status of each module.
php bin/magento module:status
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); ?>
Magento 2 REST API
Magento 2 API framework allows developers to create new services for communicating with Magento 2 stores.
Here I will use this Token based REST API to get all the customers from a Magento 2 store. So before each API call we should get the token from Magento and we should pass this token in the header of every request.
Also to achive this you need to create a new admin user with necessary access to the system.
<?php $magentoURL = "http://127.0.0.1/mage2/"; $apiURL = $magentoURL."index.php/rest/V1/integration/admin/token"; $data = array("username" => "APIUSER", "password" => "APIUSERWD"); $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); //decoding generated token and saving it in a variable $token= json_decode($token); //******************************************// //Using above token into header $headers = array("Authorization: Bearer ".$token); //API URL to get all Magento 2 modules $requestUrl =$magentoURL.'index.php/rest/V1/customers/search?searchCriteria[sortOrders][0][field]=email&searchCriteria[sortOrders][0][direction]=asc'; $ch = curl_init($requestUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); //decoding result $result= json_decode($result); //printing result echo "<pre>";print_r($result); ?>
Magento 2 Enable Cache
To enable all cache types, Issue the following command
php bin/magento cache:enable