0

I am using Magento 2.1.6 CE and also am using default rest api's for create mobile app. In that I follow below steps to create an order, But order is not created.

Step 1:- Create an empty cart for customer using POST /rest/V1/carts/mine/

enter image description here

Step 2:- Add product to cart using POST /rest/V1/carts/mine/items

enter image description here

Step 3:- Get cart totals using GET /rest/V1/carts/mine/totals/

enter image description here

Step 4:- Get Shipping methods by customer address id using POST /rest/V1/carts/mine/estimate-shipping-methods-by-address-id

enter image description here

Step 5:- Add billing and shipping address using POST /rest/V1/carts/mine/billing-address

enter image description here

Step 6:- Set shipping information using POST /rest/V1/carts/mine/shipping-information

enter image description here

Step 7:- Set payment information and create order using POST /rest/V1/carts/mine/set-payment-information

enter image description here

Note:

Am tried PUT /rest/V1/carts/mine/order method also but it returns following error.

enter image description here

How can I get this done?

Please suggest me is there any default options available to done this.

Thanks in advance!

0

You can use following method to create order using api.

Order Place:

<?php
require_once 'api.php';

require dirname(__FILE__) . '/../../app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
require dirname(__FILE__) . '/../abstract.php';
$obj = $bootstrap->getObjectManager();
$resource = $obj->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

// The URL to your Magento 2 instance (ending with /index.php/rest/V1)
$api_url = 'site_url/index.php/rest/V1';
// Set the integrations access token.
$token = 'auth_token';

$magento = new MagentoClient($token, $api_url);
$json = $_POST['json_obj'];
$json_cart_array =array();
$json_cart_array = json_decode($json,true);
$cart_id = $json_cart_array['cart_id'];
$payment_method = $json_cart_array['payment_method'];
//$payment_method = $json_cart_array['payment_method'];

// Create customer cart using customer id.
$ordered = $magento->placeOrderCust($cart_id, $payment_method);
$result1 = json_decode($ordered, 1);
//print_r($result1);exit();

API Class:

<?php
class MagentoClient {
  public $bearer_token = 'auth_token';
  public $base_url = 'magento_base_url';

  public function __construct($token, $base_url) {
    $this->base_url = $base_url;
    $this->bearer_token = $token;
  }

  public function request($endpoint, $method = 'GET', $body = FALSE) {

    // Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $this->base_url . $endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

      $headers = array();
    $headers[] = "Authorization: Bearer " . $this->bearer_token;
    if ($body) {
      $headers[] = "Content-Type: application/json";
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
      echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);
    return $result;
  }

  public function getProduct($product_id) {
    return $this->request('/products/' . $product_id . '/', 'GET');
  }

  public function createCart() {
    return $this->request('/guest-carts/', 'POST');
    // var_dump($this->request('/guest-carts/', 'POST'));
    // die;
  }

  public function addToCart($cart_id, $product_sku, $quantity = 1) {
    $order = array(
      'cartItem' => array(
        'quote_id' => $cart_id,
        'sku' => $product_sku,
        'qty' => $quantity,
      )
    );
    return $this->request('/guest-carts/' . $cart_id . '/items',
      'POST',
      json_encode($order)
      );
  }

  public function addToCartCust($cart_id, $product_sku, $quantity = 1,$options) {
    if(isset($options['extension_attributes']['configurable_item_options']) && $options != '' && count($options['extension_attributes']['configurable_item_options']) > 0){
      $order = array(
      'cartItem' => array(
        'quote_id' => $cart_id,
        'sku' => $product_sku,
        'qty' => $quantity,
        'product_option' => $options
      )
    );
    }else{
      $order = array(
      'cartItem' => array(
        'quote_id' => $cart_id,
        'sku' => $product_sku,
        'qty' => $quantity
      )
    );

    }
    //json_encode($order);

    return $this->request('/carts/' . $cart_id . '/items',
      'POST',
      json_encode($order)
      );
  }

  public function setShipping($cart_id, $shipping) {
    return $this->request('/guest-carts/' . $cart_id . '/shipping-information',
      'POST',
      json_encode($shipping)
    );
  }

  public function setShippingCust($cart_id, $shipping) {
    return $this->request('/carts/' . $cart_id . '/shipping-information',
      'POST',
      json_encode($shipping)
    );
  }

  public function placeOrder($cart_id, $payment_method = 'cashondelivery') {
    $payment = array(
      'paymentMethod' => array('method' => $payment_method)
    );

    return $this->request('/guest-carts/' . $cart_id . '/order',
      'PUT',
      json_encode($payment)
    );
  }

   public function placeOrderCust($cart_id, $payment_method = 'cashondelivery') {
    $payment = array(
      'paymentMethod' => array('method' => $payment_method)
    );

    return $this->request('/carts/' . $cart_id . '/order',
      'PUT',
      json_encode($payment)
    );
  }

  public function getPaymentMethods($cart_id) {
    return $this->request('/guest-carts/' . $cart_id . '/payment-information', 'GET');
  }

 public function createCustCart($cid)
 {
     return $this->request('/customers/' . $cid . '/carts', 'POST');
 }

}

This code is available on github but now i forgot the link. Hope it helps.

  • How can I use the above code in api call please explain in detail – Vignesh Bala Aug 31 '17 at 12:02
  • You can create write this script in magento root directory and check this is working or not in browser by accessing first script. Or you can check api using individual function from the class for creating cart, adding shipping address etc.. – Nitin Pawar Aug 31 '17 at 12:18
  • I am got following error Warning: require(C:\xampp\htdocs\mage5/../abstract.php): failed to open stream: No such file or directory in C:\xampp\htdocs\mage5\place-order.php on line 6 – Vignesh Bala Aug 31 '17 at 13:17
  • Below error also Fatal error: require(): Failed opening required 'C:\xampp\htdocs\mage5/../abstract.php' – Vignesh Bala Aug 31 '17 at 13:21
  • create scripts/orderapi in magento root and put above two scripts orderapi folder. – Nitin Pawar Sep 1 '17 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.