Advertisement
businessdad

Prices by Country - Use country from manual orders

May 29th, 2020
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Prices by Country - Use billing or shipping country from manual orders
  3.  * The following code snippet takes the  billing or shipping country from manual orders,
  4.  * so that the prices for such country are taken when adding items to the order.
  5.  *
  6.  * IMPORTANT
  7.  * For this code snippet to work, the order must have the billing and/or shipping address
  8.  * entered and saved into the database. To do so, please enter the address(es), then
  9.  * save the order before adding products to it.
  10.  *
  11.  * GPL DISCLAIMER
  12.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  13.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  14.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  15.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  16.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  17.  *
  18.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  19.  *
  20.  */
  21.  
  22. /**
  23.  * During the creation of manual orders, this function replaces the active country used by the
  24.  * Prices by Country plugin with the billing or shipping country from the order.
  25.  *
  26.  * @param string $country
  27.  */
  28. add_filter('wc_aelia_pbc_customer_country', function($country) {
  29.   // Intercept the "add order item" action. That's the action used to add products
  30.   // to manual orders
  31.   if(defined('DOING_AJAX') && isset($_REQUEST['action']) && isset($_REQUEST['order_id']) && ($_REQUEST['action'] === 'woocommerce_add_order_item')) {
  32.     $order = wc_get_order($_REQUEST['order_id']);
  33.     // Safeguard to prevent dealing with invalid orders
  34.     if($order instanceof \WC_Order) {
  35.       // Take the shipping country, if the Prices by Country plugin is configured to do so
  36.       if(Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->customer_country_to_use()  === 'shipping_country') {
  37.         $order_country = $order->get_shipping_country();
  38.       }
  39.  
  40.       // If the shipping country is empty, or the Prices by Country plugin is configured to use
  41.       // the billing country, take that one instead
  42.       if(empty($order_country) || (Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->customer_country_to_use()  === 'billing_country')) {
  43.         $order_country = $order->get_billing_country();
  44.       }
  45.     }
  46.   }
  47.  
  48.   // Replace the active country with the order country, if set
  49.   return !empty($order_country) ? $order_country : $country;
  50. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement