3

I am trying to create order using REST API I have got an error message ordered: {"message":"%fieldName is a required field.","parameters":{"fieldName":"entity"}}"

What is entity field in order of magento2?.

Api.php

<?php
class MagentoClient {
 public $bearer_token = '';
 public $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) {
 $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');
 }
 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 setShipping($cart_id, $shipping) {
 return $this->request('/guest-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 getPaymentMethods($cart_id) {
 return $this->request('/guest-carts/' . $cart_id . '/payment-information', 'GET');
 }
}

place-order.php

<?php
require_once 'api.php';
// The URL to your Magento 2 instance (ending with /index.php/rest/V1)
$api_url = 'http://192.168.1.100/magento24/index.php/rest/V1/orders';
// Set the integrations access token.
$token = '00qlti27cwtsj2inmjg9w02yve991kib';
// Fill in the SKU of the product which should be ordered.
$sku = 'car';
$magento = new MagentoClient($token, $api_url);
$product = $magento->getProduct($sku);
$cart = $magento->createCart();
$cart = str_replace('"', '', $cart);
$order_filled = $magento->addToCart($cart, $sku, 1);
//var_dump($order_filled);
$ship_to = array (
 'addressInformation' =>
 array (
 'shippingAddress' =>
 array (
 'region' => 'Wien',
 'region_id' => 95,
 'country_id' => 'AT',
 'street' =>'aaa',
 array (
 0 => 'Fillgradergasse 12-14/1a',
 ),
 'company' => 'acolono GmbH',
 'telephone' => '1111111',
 'postcode' => '1060',
 'city' => 'Vienna',
 'firstname' => 'Martin',
 'lastname' => 'Testman',
 'email' => '[email protected]',
 'prefix' => 'address_',
 'region_code' => 'W',
 'sameAsBilling' => 1,
 ),
 'billingAddress' =>
 array (
 'region' => 'Wien',
 'region_id' => 95,
 'country_id' => 'AT',
 'street' =>
 array (
 0 => 'Fillgradergasse 12-14/1a',
 ),
 'company' => 'acolono GmbH',
 'telephone' => '1111111',
 'postcode' => '1060',
 'city' => 'Vienna',
 'firstname' => 'Martin',
 'lastname' => 'Testman',
 'email' => '[email protected]',
 'prefix' => 'address_',
 'region_code' => 'W',
 ),
 'shipping_method_code' => 'flatrate',
 'shipping_carrier_code' => 'flatrate',
 ),
);
$order_shipment = $magento->setShipping($cart, $ship_to);
// var_dump($order_shipment);
//$payment = $magento->getPaymentMethods($cart);
//var_dump($payment);
$ordered = $magento->placeOrder($cart, 'cashondelivery');
echo "\nordered:\n";
var_dump($ordered)
;

Can any one help me on this problem ? Any references or suggestions are highly appreciated. Thanks in advance.

asked Mar 2, 2017 at 14:35
1
  • You should probably give some code example how do you call the API Commented Mar 2, 2017 at 17:23

1 Answer 1

0

All seems good with your code I have tried it and it's working .Check with your instance custom modules and access token.

answered Mar 3, 2017 at 12:43
2
  • Need more information ...What is entity field in order of magento2?. Commented Mar 3, 2017 at 13:10
  • @KamleshJha you find your answer for sales order entity here [link]magento.stackexchange.com/questions/162801/… Commented Mar 3, 2017 at 14:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.