Use Magento 2 APIs to Create an Order
['quote_id' => $cartId, 'qty' => 1, 'sku' => '24-MB04']]); $url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/items'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token)); $result = curl_exec($ch); $obj = json_decode($result); var_dump($obj); $body = json_encode([ 'addressInformation' =>[ 'shippingAddress' => [ "country_id" => 'US', "street" =>[ "123 Main Str" ], "company" => "Company", "telephone" => "123456", "postcode" => "91780", "city" => "El Monten", "firstname" => "Guo", "lastname" => "Jing", "region_id" => "12", "region" => "California", "email" => "dummy@example.com", "sameAsBilling" => 1 ], 'billingAddress' => [ "country_id" => 'US', "street" =>[ "123 Main Str" ], "company" => "Company", "telephone" => "123456", "postcode" => "91780", "city" => "El Monten", "firstname" => "Guo", "lastname" => "Jing", "region_id" => "12", "region" => "California", "email" => "dummy@example.com", ], "shipping_method_code" => "flatrate", "shipping_carrier_code" => "flatrate" ] ]); $url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/shipping-information'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token)); $result = curl_exec($ch); var_dump($result); $body = json_encode([ "paymentMethod" => [ "method" => "checkmo" ] ]); $url = 'http://magento2.local/index.php/rest/V1/guest-carts/'. $cartId . '/order'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token)); $result = curl_exec($ch); var_dump($result); curl_close($ch);
Create Or Update Source Items
[ ['source_code' => 'default', 'sku' => '24-MB01', 'quantity'=>555, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB01', 'quantity' => 222, 'status' => 1], ['source_code' => 'default', 'sku' => '24-MB04', 'quantity'=>444, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB04', 'quantity' => 111, 'status' => 1], ['source_code' => 'default', 'sku' => '24-MB03', 'quantity'=>333, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB03', 'quantity' => 111, 'status' => 1], ['source_code' => 'default', 'sku' => '24-MB02', 'quantity'=>222, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB02', 'quantity' => 100, 'status' => 1], ['source_code' => 'default', 'sku' => '24-MB06', 'quantity'=>666, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB06', 'quantity' => 110, 'status' => 1], ['source_code' => 'default', 'sku' => '24-MB05', 'quantity'=>555, 'status'=>1], ['source_code' => 'la', 'sku' => '24-MB05', 'quantity' => 101, 'status' => 1], ]]); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer ' . $access_token)); $result = curl_exec($ch); var_dump($result); $obj = json_decode($result); var_dump($obj); curl_close($ch);
Get the Access Token
In the previous examples, I have used the $access_token to make API’s call. You may ask how to get this value. Here is it:
$url = 'http://magento2.local/index.php/rest/V1/integration/admin/token'; $ch = curl_init($url); $body = json_encode(['username' => 'your username', 'password' => 'your password']); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Content-Lenght: " . strlen($body))); $access_token = curl_exec($ch); echo $access_token . "\n\n\n";
I hope these examples will help you in your future project. You can view all Magento 2 APIs with: