我正在尝试订购REST API,该API将用于android应用程序。我用过wc_create_order()
但我并没有像我在网络视图中得到的那个样得到纳税价值。
我想要什么-我需要在产品旁边显示tax,因为我已经分享了屏幕截图。
Order page while order with api.
Order page while order with website(woocommerce checkout page).
以下是我使用的代码:
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);}