1

我正在尝试通过 web 服务创建与 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>

RETURN 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

有什么建议吗?

4

5 回答 5

5

问题解决了。

1)创建客户(可选)

2)创建地址(可选)

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

4) 使用 order_rows 以及产品 ID 和数量创建购物车

5)用这个创建订单

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

简单的购物车在创建订单之前不应为空,其声音逻辑在逐步交互过程中,并且应通过 Web 服务在“批处理”中完成相同的顺序。

于 2013-08-31T19:12:04.090 回答
1

对于通过 web 服务下订单,您需要直接在 db 中输入一些关于购物车表中类似购物车的交付选项的条目,对于折扣,请在 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;
于 2016-09-01T09:25:39.800 回答
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();
}       

于 2020-03-03T05:02:28.177 回答
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_deliveryid_address_invoice,这就是我遇到此错误的原因。

阅读更多PrestaShop 网络服务创建订单错误

于 2015-10-15T11:52:39.870 回答
0

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

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

于 2018-04-16T08:52:25.517 回答