Martynas B.
Martynas B.

Reputation: 11

PrestaShop: Can't create order with web service

I'm trying to create an order using the web service on another web page i have. I'm running the latest version of PrestaShop 1.7. It's really basic, just entering ID of product and quantity. This is my first serious task and i'm struggling to make this work, which it still doesn't. The code i will soon show can create a cart, but it can not create the order for some reason.

Code:

try {
/*
 Creating new cart
 */

$xml = $webService->get( array( 'url' => PS_SHOP_PATH .'/api/carts?schema=blank' ) );


// Required
$xml->cart->id_currency         = 1;
$xml->cart->id_lang             = 1;

foreach($_SESSION['vezimas'] as $i => $vezimas) {
    $xml->cart->associations->cart_rows->cart_row[$i]->id_product = $vezimas['product_id'];
    $xml->cart->associations->cart_rows->cart_row[$i]->quantity = $vezimas['qty'];
}

// Adding the new cart
$opt = array( 'resource' => 'carts' );
$opt['postXml'] = $xml->asXML();
$xml = $webService->add( $opt );
$id_cart = $xml->cart->id;

/*
Creating Order
*/

// Getting the structure of an order
$xml = $webService->get(array('url' => PS_SHOP_PATH .'/api/orders/?schema=blank'));

// Required
$xml->order->id_address_delivery    = 6; // Customer address
$xml->order->id_address_invoice     = 6;
$xml->order->id_cart                = $id_cart;
$xml->order->id_currency            = 1;
$xml->order->id_lang                = 1;
$xml->order->id_customer            = 2;
$xml->order->id_carrier             = 1;
$xml->order->module                 = 'ps_checkpayment';
$xml->order->payment                = 'Payments by check';
$xml->order->total_paid             = 0;
$xml->order->total_paid_real        = 0;
$xml->order->total_products         = $_SESSION['prekesNum'];
$xml->order->total_products_wt      = 0;
$xml->order->conversion_rate        = 1;

// Others
$xml->order->valid                      = 1;

// Order Row. Required
foreach($_SESSION['vezimas'] as $i => $vezimas) {
    $xml->order->associations->order_rows->order_row[0]->product_id = $vezimas['product_id'];
    $xml->order->associations->order_rows->order_row[0]->product_quantity = $vezimas['qty'];
}

// Creating the order
$opt = array( 'resource' => 'orders' );
$opt['postXml'] = $xml->asXML();
$xml = $webService->add( $opt );
$id_order = $xml->order->id;
}  catch (PrestaShopWebserviceException $e) {

// Here we are dealing with errors
$trace = $e->getTrace();
if ($trace[0]['args'][0] == 404) echo 'Bad ID';
else if ($trace[0]['args'][0] == 401) echo 'Bad auth key';
else echo 'Other error<br />'.$e->getMessage();
}

I get a fatal error in Debug, and this on the page:

Other error
HTTP XML response is not parsable: array ( 0 => LibXMLError::__set_state(array( 'level' => 3, 'code' => 4, 'column' => 1, 'message' => 'Start tag expected, \'<\' not found ', 'file' => '', 'line' => 1, )), )

Upvotes: 0

Views: 1252

Answers (1)

Martynas B.
Martynas B.

Reputation: 11

Added some more field and a secure key to my cart code then it worked fine.

Upvotes: 1

Related Questions