2
我想通過webservices在prestashop中添加訂單。但我的訂單每次都收到付款錯誤。問題是我的折扣和運輸沒有反映在管理儀表板的訂單面板中。使用webservice在Prestashop中完成訂單
這裏是我的代碼:
$webService = new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$customer = array();
$product = array();
$id['country'] = '165';
$id['lang'] = $raw_data['id_lang'];
$id['currency'] = $raw_data['id_currency'];
$id['carrier'] = $raw_data['id_carrier'];
$product['order_rows'] = $raw_data['order_rows'];
// $product['id'] = $id_product;
// $product['price'] = Product::getPriceStatic($product['id']);
// $product['name'] = Product::getProductName($product['id']);
//$product['total'] = $product['price'] * $product['quantity'];
$customerid = $raw_data['id_customer'];
// ID of created customer
$id['customer'] = $customerid;
// CREATE Address
$addresid = $raw_data['id_address_delivery'];
// ID of created address
$id['address'] = $addresid;
// CREATE Cart
$cartid = $raw_data['id_cart'];
// ID of created cart
$id['cart'] = $cartid;
//$sql = "select id_order from ps_orders order by id_order desc limit 0,1";
//$r = mysql_query($sql, $conn)or die(mysql_error($conn));
//$row = mysql_fetch_assoc($r);
//// CREATE Order
//$id['order'] = $row['id_order'] + 1;
$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 = "0";
$xml->order->total_paid_tax_incl = "0";
$xml->order->total_paid_tax_excl ="0";
$xml->order->total_paid_real = "0";
$xml->order->conversion_rate = '1';
// foreach ($products as $product){
// $xml->order->associations->order_rows->order_row->product_id = $product['id'];
// $xml->order->associations->order_rows->order_row->product_quantity = $product['quantity'];
// }
$opt = array('resource' => 'orders');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
//$xml=$webService->parseXML($xml);
$id['order'] = $xml->order->id;
//var_dump($xml->order);
$id['secure_key'] = $xml->order->secure_key;
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/order_carriers?schema=blank'));
$xml->order_carrier->id_carrier = $id['carrier'];
$xml->order_carrier->id_order = $id['order'];
$xml->order_carrier->shipping_cost_tax_excl = $raw_data['total_shipping'];
$xml->order_carrier->shipping_cost_tax_incl = $raw_data['total_shipping'];
$opt = array('resource' => 'order_carriers');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
foreach ($raw_data['discounts'] as $discount) {
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/order_discounts?schema=blank'));
$xml->order_cart_rule->id_cart_rule = $discount['id_cart_rule'];
$xml->order_cart_rule->id_order = $id['order'];
$xml->order_cart_rule->value = $discount['value'];
$xml->order_cart_rule->name = $discount['name'];
$xml->order_cart_rule->value_tax_excl = $discount['value'];
$opt = array('resource' => 'order_discounts');
$opt['postXml'] = $xml->asXML();
$xml = $webService->add($opt);
}
$sql = "update ps_order_history set id_order_state=3 where id_order=" . $id['order'];
$r = mysql_query($sql, $conn)or die(mysql_error($conn));
$xml = $webService->get(array('url' => PS_SHOP_PATH . '/api/orders/'.$id['order']));
$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',"id"=>$id['order']);
$opt['putXml'] = $xml->asXML();
$xml = $webService->edit($opt);
$id['orderrefer'] = $xml->order->reference;
你能發佈錯誤/錯誤嗎?你正在使用哪種版本的prestashop? – sarcom
hello sarcom,我沒有收到任何錯誤。問題出在管理員儀表板上。如果我使用任何折扣和運費,它說支付錯誤。我添加了管理儀表板的屏幕截圖。和我的prestashop版本是1.6.0.14 –
當你通過webservices進行訂單時,你必須對運輸,折扣等進行正確的計算,因爲當你從網上下訂單時,prestashop會爲我們做這個微積分,但通過web服務,如果你給一個錯誤的數字,你會進入該錯誤。 – sarcom