businessdad icon

Prices by Country - Use country from manual orders

businessdad | PRO | 05/29/20 11:22:49 AM UTC | 0 ⭐ | 1738 👁️ | Never ⏰ | []
PHP |

2.68 KB

|

None

|

0 👍

/

0 👎

/**
 * Prices by Country - Use billing or shipping country from manual orders
 * The following code snippet takes the  billing or shipping country from manual orders,
 * so that the prices for such country are taken when adding items to the order.
 * 
 * IMPORTANT
 * For this code snippet to work, the order must have the billing and/or shipping address
 * entered and saved into the database. To do so, please enter the address(es), then 
 * save the order before adding products to it.
 * 
 * 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
 * 
 */
 
/**
 * During the creation of manual orders, this function replaces the active country used by the
 * Prices by Country plugin with the billing or shipping country from the order.
 * 
 * @param string $country
 */
add_filter('wc_aelia_pbc_customer_country', function($country) {
  // Intercept the "add order item" action. That's the action used to add products
  // to manual orders
  if(defined('DOING_AJAX') && isset($_REQUEST['action']) && isset($_REQUEST['order_id']) && ($_REQUEST['action'] === 'woocommerce_add_order_item')) {
    $order = wc_get_order($_REQUEST['order_id']);
    // Safeguard to prevent dealing with invalid orders
    if($order instanceof \WC_Order) {
      // Take the shipping country, if the Prices by Country plugin is configured to do so
      if(Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->customer_country_to_use()  === 'shipping_country') {
        $order_country = $order->get_shipping_country();
      }
 
      // If the shipping country is empty, or the Prices by Country plugin is configured to use
      // the billing country, take that one instead
      if(empty($order_country) || (Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->customer_country_to_use()  === 'billing_country')) {
        $order_country = $order->get_billing_country();
      }
    }
  }
 
  // Replace the active country with the order country, if set
  return !empty($order_country) ? $order_country : $country;
});

Comments