/** * Ensures that WooCommerce deducts taxes applicable order's billing or shipping address * when adding items to manual orders. * * HOW TO USE THIS CODE * Simply add the code to the bottom of your theme's functions.php file, and it * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/ * * GPL DISCLAIMER * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law. * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is" * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. * * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us * * @return string */ add_action('wp_ajax_woocommerce_add_order_item', 'set_customer_address_for_add_order_item', 5); add_action('wp_ajax_nopriv_woocommerce_add_order_item', 'set_customer_address_for_add_order_item', 5); /** * Intercepts the "add order item" Ajax call, to override the billing and shipping * address on the customer entity. */ function set_customer_address_for_add_order_item() { if(!check_ajax_referer( 'order-item', 'security', false) || !current_user_can('edit_shop_orders')) { return; } if(!isset($_POST['order_id'])) { return; } $order = wc_get_order($_POST['order_id']); // Set the billing address against the customer entity. This will ensure that WooCommerce // will deduct the taxes applicable to that address, rather than shop's base address WC()->customer->set_billing_country($order->get_billing_country()); WC()->customer->set_billing_state($order->get_billing_state()); WC()->customer->set_billing_postcode($order->get_billing_postcode()); WC()->customer->set_billing_city($order->get_billing_city()); // Set the billing address against the customer entity. This will ensure that WooCommerce // will deduct the taxes applicable to that address, rather than shop's base address WC()->customer->set_shipping_country(!empty($order->get_shipping_country()) ? $order->get_shipping_country() : $order->get_billing_country()); WC()->customer->set_shipping_state(!empty($order->get_shipping_state()) ? $order->get_shipping_state() : $order->get_billing_state()); WC()->customer->set_shipping_postcode(!empty($order->get_shipping_postcode()) ? $order->get_shipping_postcode() : $order->get_billing_postcode()); WC()->customer->set_shipping_city(!empty($order->get_shipping_city()) ? $order->get_shipping_city() : $order->get_billing_city()); }