Advertisement
businessdad

WooCommerce - Keep product prices fixed on manual orders

Jun 28th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.88 KB | None | 0 0
  1. /**
  2.  * Ensures that WooCommerce deducts taxes applicable order's billing or shipping address
  3.  * when adding items to manual orders.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Simply add the code to the bottom of your theme's functions.php file, and it
  7.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  8.  *
  9.  * GPL DISCLAIMER
  10.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  11.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  12.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  13.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  14.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  15.  *
  16.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  17.  *
  18.  * @return string
  19.  */
  20.  
  21. add_action('wp_ajax_woocommerce_add_order_item', 'set_customer_address_for_add_order_item', 5);
  22. add_action('wp_ajax_nopriv_woocommerce_add_order_item', 'set_customer_address_for_add_order_item', 5);
  23.  
  24. /**
  25.  * Intercepts the "add order item" Ajax call, to override the billing and shipping
  26.  * address on the customer entity.
  27.  */
  28. function set_customer_address_for_add_order_item() {
  29.   if(!check_ajax_referer( 'order-item', 'security', false) || !current_user_can('edit_shop_orders')) {
  30.     return;
  31.   }
  32.  
  33.   if(!isset($_POST['order_id'])) {
  34.     return;
  35.   }
  36.   $order = wc_get_order($_POST['order_id']);
  37.   // Set the billing address against the customer entity. This will ensure that WooCommerce
  38.   // will deduct the taxes applicable to that address, rather than shop's base address
  39.   WC()->customer->set_billing_country($order->get_billing_country());
  40.   WC()->customer->set_billing_state($order->get_billing_state());
  41.   WC()->customer->set_billing_postcode($order->get_billing_postcode());
  42.   WC()->customer->set_billing_city($order->get_billing_city());
  43.  
  44.   // Set the billing address against the customer entity. This will ensure that WooCommerce
  45.   // will deduct the taxes applicable to that address, rather than shop's base address
  46.   WC()->customer->set_shipping_country(!empty($order->get_shipping_country()) ? $order->get_shipping_country() : $order->get_billing_country());
  47.   WC()->customer->set_shipping_state(!empty($order->get_shipping_state()) ? $order->get_shipping_state() : $order->get_billing_state());
  48.   WC()->customer->set_shipping_postcode(!empty($order->get_shipping_postcode()) ? $order->get_shipping_postcode() : $order->get_billing_postcode());
  49.   WC()->customer->set_shipping_city(!empty($order->get_shipping_city()) ? $order->get_shipping_city() : $order->get_billing_city());
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement