Prestashop webservice创建订单

时间:2013-08-30 23:48:10

标签: php xml web-services prestashop

我正在尝试通过webservices创建与prestashop的API连接

我可以成功创建自定义,地址,购物车 (有关于客户CRUD的众所周知的纯文档, 它按预期工作)

我无法完成新订单:

有源代码片段

require_once('config.php'); // PS_SHOP_PATH etc.
require_once('PSWebServiceLibrary.php');

 try
 {
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$opt = array('resource' => 'orders');
$xml = $webService->get(array('url' => PS_SHOP_PATH.'/api/orders?schema=blank'));

$xml->children()->children()->id_address_delivery = 1; 
$xml->children()->children()->id_address_invoice =  1; 
$xml->children()->children()->id_cart = 25; 
$xml->children()->children()->id_currency = 1;
$xml->children()->children()->current_state = 1;
$xml->children()->children()->id_lang = 1;
$xml->children()->children()->id_customer = 1; 
$xml->children()->children()->id_carrier = 0;
$xml->children()->children()->total_paid = '56973';
$xml->children()->children()->total_paid_real = '56973';
$xml->children()->children()->total_paid_tax_excl = '56973';
$xml->children()->children()->total_paid_tax_incl = '56977';
$xml->children()->children()->total_products = 1338;
$xml->children()->children()->total_products_wt = 1337;
$xml->children()->children()->conversion_rate = '1';
$xml->children()->children()->secure_key = md5('-1');;
$xml->children()->children()->valid = 1;
$xml->children()->children()->module = 'Bankwire';
$xml->children()->children()->payment = 'bankwire';
$xml->children()->children()->current_state = 1;

unset($xml->children()->children()->id);
unset($xml->children()->children()->date_add);
unset($xml->children()->children()->date_upd);
unset($xml->children()->children()->associations);
unset($xml->children()->children()->delivery_date);
unset($xml->children()->children()->invoice_date);
unset($xml->children()->children()->total_discounts_tax_incl);
unset($xml->children()->children()->total_discounts_tax_excl);


$xml = $webService->add(array('resource' => 'orders',
'postXml' => $xml->asXML()
));

$resources = $xml->children()->children();
echo "Successfully added order.".$resources->id;

}
catch (PrestaShopWebserviceException $ex)
{
    // Here we are dealing with errors
    $trace = $ex->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 />'.$ex->getMessage();
}

还有一部分。来自Fiddler的webview日志

     XML SENT

   xml=<?xml version="1.0" encoding="UTF-8"?>
   <prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
   <order>

    <id_address_delivery>1</id_address_delivery>
    <id_address_invoice>1</id_address_invoice>
    <id_cart>25</id_cart>
    <id_currency>1</id_currency>
    <id_lang>1</id_lang>
    <id_customer>1</id_customer>
    <id_carrier>0</id_carrier>
    <current_state>1</current_state>
    <module>Bankwire</module>
    <invoice_number/>

    <delivery_number/>

    <valid>1</valid>


    <id_shop_group/>
    <id_shop/>
    <secure_key>6bb61e3b7bce0931da574d19d1d82c88</secure_key>
    <payment>bankwire</payment>
    <recyclable/>
    <gift/>
    <gift_message/>
    <mobile_theme/>
    <total_discounts/>


    <total_paid>56973</total_paid>
    <total_paid_tax_incl>56977</total_paid_tax_incl>
    <total_paid_tax_excl>56973</total_paid_tax_excl>
    <total_paid_real>56973</total_paid_real>
    <total_products>1338</total_products>
    <total_products_wt>1337</total_products_wt>
    <total_shipping/>
    <total_shipping_tax_incl/>
    <total_shipping_tax_excl/>
    <carrier_tax_rate/>
    <total_wrapping/>
    <total_wrapping_tax_incl/>
    <total_wrapping_tax_excl/>
    <shipping_number/>
    <conversion_rate>1</conversion_rate>
    <reference/>

   </order>
   </prestashop>

返回HTTP BODY    致命错误    其他错误

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

“其他错误”来自:

  else echo 'Other error<br />'.$ex->getMessage();

PSWebServiceLibrary包含

/** @var array compatible versions of PrestaShop Webservice */
const psCompatibleVersionsMin = '1.4.0.17';
const psCompatibleVersionsMax = '1.5.4.1';

PrestaShop是1.5.4.0

有什么建议吗?

5 个答案:

答案 0 :(得分:5)

问题解决了。

1)创建客户(选择。)

2)创建地址(选择。)

3)检查产品的可用性(这真的更好......)

4)使用order_rows和产品ID和数量

创建购物车

5)使用此

创建订单

在所有情况下,所需标签应填写 - 请参阅/ api / 对象 ?schema = synopsis

在创建订单之前,简单购物车不应为空,其声音逻辑 在一步一步的过程中,应该采用相同的顺序 通过webservices在“批处理”处理中完成。

答案 1 :(得分:1)

要通过网络服务下订单,您需要直接向数据库输入一些关于购物车的信息,例如购物车表格中的交货选项,以及ps_cart_cart_rule表格中的折扣添加条目,然后在放置时下订单。 1.添加购物车; 2.添加关于购物车的折扣选项。 3.添加关于购物车的交货选项。 4.下订单具有相同的购物车ID。 代码如下:

foreach ($raw_data['discounts'] as $discount) {
foreach ($raw_data['discounts'] as $discount) {
    $sql = "insert into ps_cart_cart_rule values('" . $id['cart'] . "','" . $discount['id_cart_rule'] . "')";
    $r = mysql_query($sql, $conn)or die(mysql_error($conn));
}

$sql = "update ps_cart set `delivery_option`='a:1:{i:" . $id['address'] . ";s:3:\"" . $id['carrier'] . ",\";}', `id_carrier`='" . $id['carrier'] . "' where id_cart='" . $id['cart'] . "'";
$r = mysql_query($sql, $conn)or die(mysql_error($conn));


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

$xml->order->id_customer = $id['customer'];
$xml->order->id_address_delivery = $id['address'];
$xml->order->id_address_invoice = $id['address'];
$xml->order->id_cart = $id['cart'];
$xml->order->id_currency = $id['currency'];
$xml->order->id_lang = $id['lang'];
$xml->order->id_carrier = $id['carrier'];
$xml->order->current_state = "3";
$xml->order->valid = 0;
$xml->order->total_shipping = $raw_data['total_shipping'];
$xml->order->total_shipping_tax_incl = $raw_data['total_shipping'];
$xml->order->total_shipping_tax_excl = $raw_data['total_shipping'];
$xml->order->total_discounts = $raw_data['total_discounts'];
$xml->order->total_discounts_tax_incl = $raw_data['total_discounts'];
$xml->order->total_discounts_tax_excl = $raw_data['total_discounts'];
$xml->order->payment = 'Cash on delivery';
$xml->order->module = 'cashondelivery';
$xml->order->total_products = $raw_data['total_products'];
$xml->order->total_products_wt = $raw_data['total_products'];
$xml->order->total_paid = $raw_data['total_paid'];
$xml->order->total_paid_tax_incl = $raw_data['total_paid'];
$xml->order->total_paid_tax_excl = $raw_data['total_paid'];
$xml->order->total_paid_real = "0";
$xml->order->conversion_rate = '1';
$opt = array('resource' => 'orders');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
$id['order'] = $xml->order->id;
$id['secure_key'] = $xml->order->secure_key;

答案 2 :(得分:0)

以下错误是由于购物车订单

中缺少必需值
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, )), )

就我而言 id_address_delivery &amp; id_address_invoice 在购物车中丢失的地方,这就是我遇到此错误的原因。

PrestaShop web service create order errors

了解详情

答案 3 :(得分:0)

如果您收到此Start tag expected, \'<\' not found ',错误,请尝试检查您的发送解析的响应 这是由于您按顺序发送的数据不正确或由于xml验证问题而显示的。

我也遇到了同样的问题,我通过检查我在Prestashop库中发送解析方法的响应来解决它。

答案 4 :(得分:0)

define('DEBUG', true);
define('PS_SHOP_PATH', 'http://.....');
define('PS_WS_AUTH_KEY', '...');
define('FILE_NAME', 'file.xlsx');
require_once('./PSWebServiceLibrary.php');
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
require_once __DIR__.'/src/SimpleXLSX.php';

$newArray = array();  // here we will accumulate grouped data
$dataArray=[];
$product_count=0;
if ( $xlsx = SimpleXLSX::parse(FILE_NAME) ) {

    for($i=1; $i < count($xlsx->rows()); $i++){

        $id=$xlsx->rows()[$i][0];
        $product_id=$xlsx->rows()[$i][96];

        if($product_id)
        {
            if(!isset($dataArray[$id]))
            {
                $dataArray[$id]["products"][$product_id]= $xlsx->rows()[$i][103];
                $dataArray[$id]["price"]=$xlsx->rows()[$i][38];
                $dataArray[$id]["product_weight"]=$xlsx->rows()[$i][24];
                $dataArray[$id]["id_address_delivery"]=$xlsx->rows()[$i][170];
                $dataArray[$id]["id_carrier"]=$xlsx->rows()[$i][172];
                $dataArray[$id]["payment_mode"]=$xlsx->rows()[$i][173];
                $dataArray[$id]["product_count"]=$product_count;
                $dataArray[$id]["currency_code"] = $xlsx->rows()[$i][17];
            }
            else
            {
                if(!isset($dataArray[$id]["products"][$product_id]))
                {
                    $dataArray[$id]["products"][$product_id]+= $xlsx->rows()[$i][103];
                }
                else
                {
                    $dataArray[$id]["products"][$product_id]+= $xlsx->rows()[$i][103];
                }
            }
        }
    }

} else {
    echo SimpleXLSX::parseError();
}

try{

    foreach($dataArray as $key => $value){

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

        $xml_currency = $webService->get(array('url' => PS_SHOP_PATH.'/api/currencies/?display=full'));
        $xml_language = $webService->get(array('url' => PS_SHOP_PATH.'/api/languages?display=full'));

        $value['id_language'] = (int)$xml_language->languages->language->id;

        $xml->cart->id_customer = $key;
        $count = 0;
        $value['total_products'] = 0;
        foreach($value['products'] as $product_key => $product_value){

            $xml->cart->associations->cart_rows->cart_row[$count]->id_product = $product_key;
            $xml->cart->associations->cart_rows->cart_row[$count]->quantity = $product_value;
            $value['total_products']+= $product_value; 
            $count++;   
        }

        for($i=0; $i<count($xml_currency->currencies->currency);$i++){
            if($xml_currency->currencies->currency[$i]->iso_code == $value['currency_code'])
                $value['id_currency'] =(int) $xml_currency->currencies->currency[$i]->id;
        }   

        $xml->cart->id_address_delivery = $value['id_address_delivery'];
        $xml->cart->id_address_invoice = $value['id_address_delivery'];
        $xml->cart->id_currency = $value['id_currency'];
        $xml->cart->id_lang = $value['id_language'];
        $xml->cart->id_carrier = $value['id_carrier'];

        $opt = array('resource' => 'carts');
        $opt['postXml'] = $xml->asXML();
        $xml = $webService->add($opt);
        $cart_id = $xml->cart->id;

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

        $xml->order->id_address_delivery = $value['id_address_delivery'];
        $xml->order->id_address_invoice  = $value['id_address_delivery'];
        $xml->order->id_cart = $cart_id;
        $xml->order->id_currency = $value['id_currency'];
        $xml->order->id_lang = $value['id_language'];
        $xml->order->id_customer = $key;
        $xml->order->id_carrier = $value['id_carrier'];
        if($value['payment_mode'] == 'Cash on delivery (COD)')
            $xml->order->module = 'ps_cashondelivery';
        if($value['payment_mode'] == 'Payment by check')
            $xml->order->module = 'ps_checkpayment';
        $xml->order->payment = $value['payment_mode'];
        $xml->order->total_paid = $value['price'];
        $xml->order->total_paid_real = $value['price'];
        $xml->order->total_products = $value['total_products'];
        $xml->order->total_products_wt = $value['product_weight'];
        $xml->order->conversion_rate = '1'; // conversion rate is fixed here

        $opt = array('resource' => 'orders');
        $opt['postXml'] = $xml->asXML();
        $xml = $webService->add($opt);
    }           

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

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