使用webservice在Prestashop中完成订单
问题描述:
我想通过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;
答
对于通过webservice下订单,您需要直接在数据库中输入与购物车表中的购物车类似的配送选项的某些条目,并在ps_cart_cart_rule表中添加条目,然后按照您放置时的顺序和顺序添加条目。
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;
答
我已经通过直接输入解决我的问题,以分贝运费和折扣表。
你能发布错误/错误吗?你正在使用哪种版本的prestashop? – sarcom
hello sarcom,我没有收到任何错误。问题出在管理员仪表板上。如果我使用任何折扣和运费,它说支付错误。我添加了管理仪表板的屏幕截图。和我的prestashop版本是1.6.0.14 –
当你通过webservices进行订单时,你必须对运输,折扣等进行正确的计算,因为当你从网上下订单时,prestashop会为我们做这个微积分,但通过web服务,如果你给一个错误的数字,你会进入该错误。 – sarcom