I'm trying to placing order with REST API which will use for android app. I've used wc_create_order()
for placing order but I didn't getting tax values as I got in web view.
What I want - I need to display tax also beside products as I have shared screenshots.
Order page while order with api.
Order page while order with website(woocommerce checkout page).
Here is code what I have used:
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);}
I'm trying to placing order with REST API which will use for android app. I've used wc_create_order()
for placing order but I didn't getting tax values as I got in web view.
What I want - I need to display tax also beside products as I have shared screenshots.
Order page while order with api.
Order page while order with website(woocommerce checkout page).
Here is code what I have used:
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);}
Share
Improve this question
edited Apr 25, 2019 at 7:05
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Apr 25, 2019 at 6:00
Parth ShahParth Shah
881 silver badge11 bronze badges
2
- WooCommerce already has an API for creating orders? Why not just use that? – Jacob Peattie Commented Apr 25, 2019 at 6:15
- Problem is same with Woocommerce API as well can't extend it well. – Parth Shah Commented Apr 25, 2019 at 7:04
1 Answer
Reset to default 2This post is old, but I had the same problem with creating an order programmatically. The tax was not calculated ...
We had configured the customer billing address as the default tax base and the country value was "Germany" and it should be "DE" .... We have changed it to the ISO code and now it works fine.