PrestaShop:无法使用网络服务创建订单

时间:2018-01-08 12:18:07

标签: php xml web-services prestashop prestashop-1.7

我试图在我拥有的其他网页上使用网络服务创建订单。我正在运行最新版本的PrestaShop 1.7。它非常基本,只需输入产品ID和数量即可。这是我的第一个严肃的任务,我正努力做这项工作,但它仍然没有。我即将展示的代码可以创建一个购物车,但由于某种原因它无法创建订单。

代码:

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();
}

我在Debug中遇到致命错误,这在页面上显示:

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, )), )

1 个答案:

答案 0 :(得分:0)

为我的购物车代码添加了更多字段和安全密钥,然后它运行正常。