businessdad icon

Aelia Currency Switcher - Override product prices

businessdad | PRO | 07/20/15 11:41:35 AM UTC | 0 ⭐ | 966 👁️ | Never ⏰ | []
PHP |

2.91 KB

|

None

|

0 👍

/

0 👎

/**
 * Indicates if Aelia Currency Switcher plugin is active.
 *
 * @return bool
 *
 * @author Aelia <[email protected]>
 * @link http://aelia.co/about
 */
function is_currency_switcher_active() {
  return isset($GLOBALS['woocommerce-aelia-currencyswitcher']) && is_object($GLOBALS['woocommerce-aelia-currencyswitcher']);
}
 
/**
 * Replaces the product prices with custom ones.
 *
 * @param array product_prices An array of product prices.
 * @param int product_id The product ID.
 * @param int price_type The price types to be replaced (e.g. regular prices,
 * sale prices, etc).
 * @return array An array of currency => price entries
 *
 * @author Aelia <[email protected]>
 * @link http://aelia.co/about
 */
function my_custom_prices($product_prices, $product_id, $price_type) {
    /* 
    $price_type can have one of the following values:
    - '_regular_currency_prices' -> Product's regular prices
    - '_sale_currency_prices' -> Product's sale prices
    - 'variable_regular_currency_prices' -> Variation's regular prices
    - 'variable_sale_currency_prices' -> Variation's simple prices
 
    Using $price_type, load the appropriate prices (regular or sale), and store 
    them in an array, using the currency as the key. If you don't have one of the 
    prices (e.g. GBP), don't add it to the array. Example:
    */
    
    $my_custom_prices = array(
        'EUR' => 123,
        'USD' => 456,
    );
    
    //You can now merge the original prices with the ones you loaded.
    $product_prices = array_merge($product_prices, $my_custom_prices);
    
    //Finally, return the overridden prices
    return $product_prices;
}
 
// Override currency prices only in the frontend
if(!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
  if(is_currency_switcher_active()) {
    /* When the Currency Switcher is active, there is no need to add filters for
     * woocommerce_get_price, woocommerce_get_regular_price, woocommerce_get_sale_price
     * and so on. Those filters are already run by the Currency Switcher, and we
     * only have to pass the prices to the plugin so that it will give them back
     * to WooCommerce.
     */
    add_filter('wc_aelia_currencyswitcher_product_currency_prices', array($this, 'wc_aelia_currencyswitcher_product_currency_prices'), 20, 3);
  }
  else {
    // When the Currency Switcher is not active, then we can use our own filters
    // to override prices
    add_filter('woocommerce_get_price', array($this, 'woocommerce_get_price'), 10, 2);
    add_filter('woocommerce_get_regular_price', array($this, 'woocommerce_get_regular_price'), 10, 2);
    add_filter('woocommerce_get_sale_price', array($this, 'woocommerce_get_sale_price'), 10, 2);
 
    add_filter('woocommerce_get_variation_regular_price', array($this, 'woocommerce_get_variation_regular_price'), 30, 4);
    add_filter('woocommerce_get_variation_sale_price', array($this, 'woocommerce_get_variation_sale_price'), 30, 4);
    // Other price filters...
  }
}

Comments