class Basic_CurrencySwitcher_Integration {
/**
* Basic integration with WooCommerce Currency Switcher, developed by Aelia
* (http://aelia.co). This method can be used by any 3rd party plugin to
* return prices converted to the active currency.
*
* Need a consultation? Find us on Codeable: https://aelia.co/hire_us
*
* @param double price The source price.
* @param string to_currency The target currency. If empty, the active currency
* will be taken.
* @param string from_currency The source currency. If empty, WooCommerce base
* currency will be taken.
* @return double The price converted from source to destination currency.
* @author Aelia <[email protected]>
* @link https://aelia.co
*/
protected function get_price_in_currency($price, $to_currency = null, $from_currency = null) {
// If source currency is not specified, take the shop's base currency as a default
if(empty($from_currency)) {
$from_currency = get_option('woocommerce_currency');
}
// If target currency is not specified, take the active currency as a default.
// The Currency Switcher sets this currency automatically, based on the context. Other
// plugins can also override it, based on their own custom criteria, by implementing
// a filter for the "woocommerce_currency" hook.
//
// For example, a subscription plugin may decide that the active currency is the one
// taken from a previous subscription, because it's processing a renewal, and such
// renewal should keep the original prices, in the original currency.
if(empty($to_currency)) {
$to_currency = get_woocommerce_currency();
}
// Call the currency conversion filter. Using a filter allows for loose coupling. If the
// Aelia Currency Switcher is not installed, the filter call will return the original
// amount, without any conversion being performed. Your plugin won't even need to know if
// the multi-currency plugin is installed or active
return apply_filters('wc_aelia_cs_convert', $price, $from_currency, $to_currency);
}
/**
* Performs a simple test conversion.
*/
public function test_conversion() {
$price = 123;
// Just call the get_price_in_currency() method, no further action required
return $this->get_price_in_currency($price);
}
public function __construct() {
add_filter('wc_aelia_currencyswitcher_product_convert_callback', array($this, 'wc_aelia_currencyswitcher_product_convert_callback'), 10, 2);
}
/**
* Alter the callbacks used by the Currency Switcher, replacing the callback
* for a "my_custom_product" type with a custon one.
*
* @param callable convert_callback The original callback.
* @param WC_Product product The callback being converted.
* @return callable The callback that will convert the product's prices.
*/
public function wc_aelia_currencyswitcher_product_convert_callback($convert_callback, $product) {
if($product->product_type === 'my_custom_product') {
$convert_callback = array($this, 'convert_my_custom_product');
}
return $convert_callback;
}
/**
* Converts the prices of a custom product to the specified currency.
*
* @param WC_Product product A variable product.
* @param string currency A currency code.
* @return WC_Product The product with converted prices.
*/
public function convert_my_custom_product($product, $currency) {
/* Here you can implement your custom logic to convert the prices.
* The two lines below are just an example. Here you can do anything you
* want with the product, to ensure that its prices are converted correctly.
*/
$product->my_custom_price = $this->get_price_in_currency($product->my_custom_price);
$product->my_other_custom_price = $this->get_price_in_currency($product->my_other_custom_price);
return $product;
}
}
Comments