Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * WooCommerce Prices by Country - Export prices by country in separate columns.
- * This example shows how to "explode" the prices by country assigned to each
- * product into separate columns (one column for each combination of region ID,
- * price type and currency).
- *
- * Note
- * This code will export the raw prices by country stored in the database. Prices
- * left as "Auto" won't be calculated on the fly, as calculated data is outside
- * the scope of the export function.
- *
- * HOW TO USE THIS CODE
- * Simply add the code to the bottom of your theme's functions.php file, and it
- * will run automatically. For more information: https://www.skyverge.com/blog/add-custom-code-to-wordpress/
- *
- * 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
- */
- /**
- * Adds one new column to the exported file, for each combination of the following elements:
- * - Region ID
- * - Region Name
- * - Price type (regular price, sale price)
- * - Currency
- *
- * @param array columns
- * @param object exporter
- * @return array
- */
- add_filter('woocommerce_product_export_column_names', function($columns, $exporter) {
- // @var An array of price keys (region ID, currency, price type)
- global $price_keys;
- $price_keys = array();
- // Safety check. We can skip the logic if the Prices by Country plugin has not
- // been initialised
- if(empty($GLOBALS['wc-aelia-prices-by-country'])) {
- return $columns;
- }
- $price_regions = \Aelia\WC\PricesByCountry\WC_Aelia_Prices_By_Country::settings()->get_regions();
- $currencies = apply_filters('wc_aelia_cs_enabled_currencies', get_woocommerce_currency());
- $price_types = array('regular_price', 'sale_price');
- foreach($price_regions as $region_id => $region) {
- foreach($currencies as $currency) {
- foreach($price_types as $price_type) {
- $meta_key = "_price_by_region_{$region_id}_{$region['region_name']}_{$price_type}_{$currency}";
- $columns['meta:' . $meta_key] = 'Meta: ' . $meta_key;
- $price_keys[] = array(
- 'region_id' => $region_id,
- 'region_name' => $region['region_name'],
- 'price_type' => $price_type,
- 'currency' => $currency,
- );
- }
- }
- }
- return $columns;
- }, 99, 2);
- /**
- * Given a combination of region ID, currency and price type, it returns the
- * matching price from the array of Prices by Country data.
- *
- * @param string price_key
- * @param array pbc_prices An array of prices stored by the Prices by Country
- * plugin.
- * @return float|string The price stored in the database for the key, or an empty
- * string if that price is not set.
- */
- function aelia_get_pbc_price_by_key($price_key, $pbc_prices) {
- if(!empty($pbc_prices[$price_key['region_id']][$price_key['currency']][$price_key['price_type']])) {
- return $pbc_prices[$price_key['region_id']][$price_key['currency']][$price_key['price_type']];
- }
- return '';
- }
- /**
- * Adds data from the Prices by Country plugin to the rows generated by the
- * product exported in WooCommerce 3.1 and later.
- *
- * @param array row
- * @param WC_Product product
- * @return array
- */
- add_filter('woocommerce_product_export_row_data', function($row, $product) {
- // @var An array of price keys (region ID, currency, price type)
- // This variable is populated within woocommerce_product_export_column_names
- // filter
- global $price_keys;
- $pbc_prices = $product->get_meta('_prices_by_country');
- if(is_array($pbc_prices)) {
- foreach($price_keys as $price_key) {
- // Generate the price key
- $meta_key = "meta:_price_by_region_{$price_key['region_id']}_{$price_key['region_name']}_{$price_key['price_type']}_{$price_key['currency']}";
- // Get the price for the product, based on the key
- $row[$meta_key] = aelia_get_pbc_price_by_key($price_key, $pbc_prices);
- }
- }
- return $row;
- }, 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement