1

Web サービス経由で prestashop への API 接続を作成しようとしています

Custommers、Addresses、Carts を正常に作成できます (顧客の 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 サービスを介して注文するには、cart テーブルのカートのような配送オプションに関していくつかのエントリをデータベースに直接作成する必要があります。ディスカウントの場合は、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 Web サービスで注文エラーを作成してください

于 2015-10-15T11:52:39.870 に答える