Advertisement
businessdad

Aelia Currency Switcher - Override product prices

Jul 20th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. /**
  2.  * Indicates if Aelia Currency Switcher plugin is active.
  3.  *
  4.  * @return bool
  5.  *
  6.  * @author Aelia <support@aelia.co>
  7.  * @link http://aelia.co/about
  8.  */
  9. function is_currency_switcher_active() {
  10.   return isset($GLOBALS['woocommerce-aelia-currencyswitcher']) && is_object($GLOBALS['woocommerce-aelia-currencyswitcher']);
  11. }
  12.  
  13. /**
  14.  * Replaces the product prices with custom ones.
  15.  *
  16.  * @param array product_prices An array of product prices.
  17.  * @param int product_id The product ID.
  18.  * @param int price_type The price types to be replaced (e.g. regular prices,
  19.  * sale prices, etc).
  20.  * @return array An array of currency => price entries
  21.  *
  22.  * @author Aelia <support@aelia.co>
  23.  * @link http://aelia.co/about
  24.  */
  25. function my_custom_prices($product_prices, $product_id, $price_type) {
  26.     /*
  27.     $price_type can have one of the following values:
  28.     - '_regular_currency_prices' -> Product's regular prices
  29.     - '_sale_currency_prices' -> Product's sale prices
  30.     - 'variable_regular_currency_prices' -> Variation's regular prices
  31.     - 'variable_sale_currency_prices' -> Variation's simple prices
  32.  
  33.     Using $price_type, load the appropriate prices (regular or sale), and store
  34.     them in an array, using the currency as the key. If you don't have one of the
  35.     prices (e.g. GBP), don't add it to the array. Example:
  36.     */
  37.    
  38.     $my_custom_prices = array(
  39.         'EUR' => 123,
  40.         'USD' => 456,
  41.     );
  42.    
  43.     //You can now merge the original prices with the ones you loaded.
  44.     $product_prices = array_merge($product_prices, $my_custom_prices);
  45.    
  46.     //Finally, return the overridden prices
  47.     return $product_prices;
  48. }
  49.  
  50. // Override currency prices only in the frontend
  51. if(!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
  52.   if(is_currency_switcher_active()) {
  53.     /* When the Currency Switcher is active, there is no need to add filters for
  54.      * woocommerce_get_price, woocommerce_get_regular_price, woocommerce_get_sale_price
  55.      * and so on. Those filters are already run by the Currency Switcher, and we
  56.      * only have to pass the prices to the plugin so that it will give them back
  57.      * to WooCommerce.
  58.      */
  59.     add_filter('wc_aelia_currencyswitcher_product_currency_prices', array($this, 'wc_aelia_currencyswitcher_product_currency_prices'), 20, 3);
  60.   }
  61.   else {
  62.     // When the Currency Switcher is not active, then we can use our own filters
  63.     // to override prices
  64.     add_filter('woocommerce_get_price', array($this, 'woocommerce_get_price'), 10, 2);
  65.     add_filter('woocommerce_get_regular_price', array($this, 'woocommerce_get_regular_price'), 10, 2);
  66.     add_filter('woocommerce_get_sale_price', array($this, 'woocommerce_get_sale_price'), 10, 2);
  67.  
  68.     add_filter('woocommerce_get_variation_regular_price', array($this, 'woocommerce_get_variation_regular_price'), 30, 4);
  69.     add_filter('woocommerce_get_variation_sale_price', array($this, 'woocommerce_get_variation_sale_price'), 30, 4);
  70.     // Other price filters...
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement