Advertisement
businessdad

WooCommerce Prices by Country - Export single product prices

Nov 7th, 2017 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.50 KB | None | 0 0
  1. /**
  2.  * WooCommerce Prices by Country - Export prices by country in separate columns.
  3.  * This example shows how to "explode" the prices by country assigned to each
  4.  * product into separate columns (one column for each combination of region ID,
  5.  * price type and currency).
  6.  *
  7.  * Note
  8.  * This code will export the raw prices by country stored in the database. Prices
  9.  * left as "Auto" won't be calculated on the fly, as calculated data is outside
  10.  * the scope of the export function.
  11.  *
  12.  * HOW TO USE THIS CODE
  13.  * Simply add the code to the bottom of your theme's functions.php file, and it
  14.  * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
  15.  *
  16.  * GPL DISCLAIMER
  17.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  18.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  19.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  20.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  21.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  22.  *
  23.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  24.  */
  25.  
  26. /**
  27.  * Adds one new column to the exported file, for each combination of the following elements:
  28.  * - Region ID
  29.  * - Region Name
  30.  * - Price type (regular price, sale price)
  31.  * - Currency
  32.  *
  33.  * @param array columns
  34.  * @param object exporter
  35.  * @return array
  36.  */
  37. add_filter('woocommerce_product_export_column_names', function($columns, $exporter) {
  38.   // @var An array of price keys (region ID, currency, price type)
  39.   global $price_keys;
  40.   $price_keys = array();
  41.  
  42.   // Safety check. We can skip the logic if the Prices by Country plugin has not
  43.   // been initialised
  44.   if(empty($GLOBALS['wc-aelia-prices-by-country'])) {
  45.     return $columns;
  46.   }
  47.  
  48.   $price_regions = \Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->get_regions();
  49.   $currencies = apply_filters('wc_aelia_cs_enabled_currencies', get_woocommerce_currency());
  50.   $price_types = array('regular_price', 'sale_price');
  51.  
  52.   foreach($price_regions as $region_id => $region) {
  53.     foreach($currencies as $currency) {
  54.       foreach($price_types as $price_type) {
  55.         $meta_key = "_price_by_region_{$region_id}_{$region['region_name']}_{$price_type}_{$currency}";
  56.         $columns['meta:' . $meta_key] = 'Meta: ' . $meta_key;
  57.  
  58.         $price_keys[] = array(
  59.           'region_id' => $region_id,
  60.           'region_name' => $region['region_name'],
  61.           'price_type' => $price_type,
  62.           'currency' => $currency,
  63.         );
  64.       }
  65.     }
  66.   }
  67.  
  68.   return $columns;
  69. }, 99, 2);
  70.  
  71. /**
  72.  * Given a combination of region ID, currency and price type, it returns the
  73.  * matching price from the array of Prices by Country data.
  74.  *
  75.  * @param string price_key
  76.  * @param array pbc_prices An array of prices stored by the Prices by Country
  77.  * plugin.
  78.  * @return float|string The price stored in the database for the key, or an empty
  79.  * string if that price is not set.
  80.  */
  81. function aelia_get_pbc_price_by_key($price_key, $pbc_prices) {
  82.   if(!empty($pbc_prices[$price_key['region_id']][$price_key['currency']][$price_key['price_type']])) {
  83.     return $pbc_prices[$price_key['region_id']][$price_key['currency']][$price_key['price_type']];
  84.   }
  85.   return '';
  86. }
  87.  
  88. /**
  89.  * Adds data from the Prices by Country plugin to the rows generated by the
  90.  * product exported in WooCommerce 3.1 and later.
  91.  *
  92.  * @param array row
  93.  * @param WC_Product product
  94.  * @return array
  95.  */
  96. add_filter('woocommerce_product_export_row_data', function($row, $product) {
  97.   // @var An array of price keys (region ID, currency, price type)
  98.   // This variable is populated within woocommerce_product_export_column_names
  99.   // filter
  100.   global $price_keys;
  101.  
  102.   $pbc_prices = $product->get_meta('_prices_by_country');
  103.  
  104.   if(is_array($pbc_prices)) {
  105.     foreach($price_keys as $price_key) {
  106.       // Generate the price key
  107.       $meta_key = "meta:_price_by_region_{$price_key['region_id']}_{$price_key['region_name']}_{$price_key['price_type']}_{$price_key['currency']}";
  108.       // Get the price for the product, based on the key
  109.       $row[$meta_key] = aelia_get_pbc_price_by_key($price_key, $pbc_prices);
  110.     }
  111.   }
  112.  
  113.   return $row;
  114. }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement