SHOW:
|
|
- or go back to the newest paste.
1 | /** | |
2 | - | * WooCommerce Prices by Country - Export prices by country in separate columns. |
2 | + | * Prices by Country - Use billing or shipping country from manual orders |
3 | - | * This example shows how to "explode" the prices by country assigned to each |
3 | + | * The following code snippet takes the billing or shipping country from manual orders, |
4 | - | * product into separate columns (one column for each combination of region ID, |
4 | + | * so that the prices for such country are taken when adding items to the order. |
5 | - | * price type and currency). |
5 | + | * |
6 | * IMPORTANT | |
7 | - | * Note |
7 | + | * For this code snippet to work, the order must have the billing and/or shipping address |
8 | - | * This code will export the raw prices by country stored in the database. Prices |
8 | + | * entered and saved into the database. To do so, please enter the address(es), then |
9 | - | * left as "Auto" won't be calculated on the fly, as calculated data is outside |
9 | + | * save the order before adding products to it. |
10 | - | * the scope of the export function. |
10 | + | * |
11 | * GPL DISCLAIMER | |
12 | - | * HOW TO USE THIS CODE |
12 | + | |
13 | - | * Simply add the code to the bottom of your theme's functions.php file, and it |
13 | + | |
14 | - | * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/ |
14 | + | |
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 | - | * Adds one new column to the exported file, for each combination of the following elements: |
27 | + | |
28 | - | * - Region ID |
28 | + | add_filter('wc_aelia_pbc_customer_country', function($country) { |
29 | - | * - Region Name |
29 | + | // Intercept the "add order item" action. That's the action used to add products |
30 | - | * - Price type (regular price, sale price) |
30 | + | // to manual orders |
31 | - | * - Currency |
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 | - | * @param array columns |
33 | + | // Safeguard to prevent dealing with invalid orders |
34 | - | * @param object exporter |
34 | + | if($order instanceof \WC_Order) { |
35 | - | * @return array |
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 | - | add_filter('woocommerce_product_export_column_names', function($columns, $exporter) { |
37 | + | $order_country = $order->get_shipping_country(); |
38 | - | // @var An array of price keys (region ID, currency, price type) |
38 | + | |
39 | - | global $price_keys; |
39 | + | |
40 | - | $price_keys = array(); |
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 | - | // Safety check. We can skip the logic if the Prices by Country plugin has not |
42 | + | if(empty($order_country) || (Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->customer_country_to_use() === 'billing_country')) { |
43 | - | // been initialised |
43 | + | $order_country = $order->get_billing_country(); |
44 | - | if(empty($GLOBALS['wc-aelia-prices-by-country'])) { |
44 | + | |
45 | - | return $columns; |
45 | + | |
46 | } | |
47 | ||
48 | - | $price_regions = \Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->get_regions(); |
48 | + | // Replace the active country with the order country, if set |
49 | - | $currencies = apply_filters('wc_aelia_cs_enabled_currencies', get_woocommerce_currency()); |
49 | + | return !empty($order_country) ? $order_country : $country; |
50 | - | $price_types = array('regular_price', 'sale_price'); |
50 | + | }); |