使用REST API下单时,订单上未显示税款

时间:2019-04-25 作者:Parth Shah

我正在尝试订购REST API,该API将用于android应用程序。我用过wc_create_order() 但我并没有像我在网络视图中得到的那个样得到纳税价值。

我想要什么-我需要在产品旁边显示tax,因为我已经分享了屏幕截图。

Order page while order with api.enter image description here

Order page while order with website(woocommerce checkout page).enter image description here

以下是我使用的代码:

function wc_place_orders($request){

global $woocommerce;

$order_data = array(
    \'status\' => apply_filters(\'woocommerce_default_order_status\', \'processing\'),
    \'customer_id\' => $request[\'user_id\'],
    \'created_via\' => "Created From Andriod App",
);

$address = array(
    \'first_name\' => $request[\'first_name\'],
    \'last_name\'  => $request[\'last_name\'],
    \'company\'    => \'\',
    \'email\'      => $request[\'email\'],
    \'phone\'      => $request[\'phone\'],
    \'address_1\'  => $request[\'address_1\'],
    \'address_2\'  => $request[\'address_2\'],
    \'city\'       => $request[\'city\'],
    \'state\'      => $request[\'state\'],
    \'postcode\'   => $request[\'postcode\'],
    \'country\'    => $request[\'country\'],
);

$order = wc_create_order($order_data);
$order_id = $order->get_id();

$saved_cart_meta = get_user_meta( $request[\'user_id\'], \'_woocommerce_persistent_cart_\' . get_current_blog_id(), true );

foreach ($saved_cart_meta[\'cart\'] as $key => $value) {
    $product = wc_get_product( $value[\'product_id\'] );
    $order->add_product( $product, $value[\'quantity\']);
}
$order->set_address( $address, \'billing\' );
$order->set_created_via($order_data[\'created_via\']);

// Set payment gateway
$payment_gateways = WC()->payment_gateways->payment_gateways();
$order->set_payment_method( $payment_gateways[$request[\'payment\']] );

//Shipping Fees
$shipping_taxes = WC_Tax::calc_shipping_tax($request[\'shipping_value\'], WC_Tax::get_shipping_tax_rates());
$order->add_shipping(new WC_Shipping_Rate(\'flat_rate_shipping\', $request[\'shipping_method_title\'], $request[\'shipping_value\'], $shipping_taxes, \'flat_rate\'));


//Pickup&dropoff time
add_post_meta($order_id, "pickup_date", $request[\'pickup_date\']);
add_post_meta($order_id, "drop_date", $request[\'drop_date\']);


$order->set_total($request[\'discount\'], \'cart_discount\');
$order->save();

$order->calculate_taxes();
$order->calculate_totals();

$order->update_status("Completed", \'Created With Android App\', TRUE);

return new WP_REST_Response($order, 123);}

1 个回复
SO网友:mcswizz

这篇文章很旧,但我在以编程方式创建订单时遇到了同样的问题。税款未计算。。。

我们已将客户帐单地址配置为默认税基,国家/地区值为“德国”,应为“DE”。。。。我们已经将其更改为ISO代码,现在它运行良好。