Skip to content

Commit

Permalink
Remove explicit prices where a single currency is active (#2673)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpaksu authored Aug 6, 2021
1 parent 99925e4 commit a19aafb
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 5 deletions.
76 changes: 76 additions & 0 deletions includes/class-wc-payments-explicit-price-formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package WooCommerce\Payments
*/

use WCPay\MultiCurrency\MultiCurrency;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
Expand All @@ -13,6 +15,14 @@
* Class for displaying the explicit prices on total amounts.
*/
class WC_Payments_Explicit_Price_Formatter {

/**
* The multi currency instance for checking the number of enabled currencies
*
* @var MultiCurrency
*/
private static $multi_currency_instance = null;

/**
* Inits the formatter, registering the necessary hooks.
*/
Expand All @@ -23,6 +33,66 @@ public static function init() {
add_action( 'woocommerce_admin_order_totals_after_total', [ __CLASS__, 'unregister_formatted_woocommerce_price_filter' ] );
}

/**
* Overrides the MultiCurrency instance that the class uses.
* Mostly for testing purposes.
*
* @param MultiCurrency $multi_currency MultCurrency class.
*
* @return void
*/
public static function set_multi_currency_instance( MultiCurrency $multi_currency ) {
self::$multi_currency_instance = $multi_currency;
}

/**
* Checks if the method should output explicit price on frontend
*
* @return bool Whether if it should return explicit price or not
*/
private static function should_output_explicit_price() {
// As is_admin() returns false for REST requests, we need to skip those checks for REST requests for backend too.
$is_backend_request = (
// Current URL is an admın URL.
( 0 === strpos( strtolower( wp_get_referer() ), strtolower( admin_url() ) ) )
// The current request is a REST request.
&& WC()->is_rest_api_request()
);

// Only apply this for frontend.
if ( ! is_admin() && ! defined( 'DOING_CRON' ) && ! $is_backend_request ) {
// If customer multi currency is disabled, don't use explicit currencies on frontend.
// Because it'll have only the store currency active, same as count == 1.
if ( ! WC_Payments_Features::is_customer_multi_currency_enabled() ) {
return false;
}

// If the MultiCurrency instance hasn't been defined yet, fetch the instance.
if ( null === self::$multi_currency_instance ) {
self::$multi_currency_instance = WC_Payments_Multi_Currency();
}

// If the instance isn't initalized yet, skip the checks.
if ( false === self::$multi_currency_instance->is_initialized() ) {
return false;
}

$enabled_currencies = self::$multi_currency_instance->get_enabled_currencies();

// If there isn't any enabled currency, skip it.
if ( empty( $enabled_currencies ) ) {
return false;
}

// Don't attach explicit price filters on frontend with a single currency setup.
if ( is_array( $enabled_currencies ) && 1 === count( $enabled_currencies ) ) {
return false;
}
}

return true;
}

/**
* Registers the get_explicit_price filter for the order details screen.
*
Expand Down Expand Up @@ -54,6 +124,9 @@ public static function unregister_formatted_woocommerce_price_filter() {
* @return string
*/
public static function get_explicit_price( string $price, WC_Order $order = null ) {
if ( false === static::should_output_explicit_price() ) {
return $price;
}
if ( null === $order ) {
$currency_code = get_woocommerce_currency();
} else {
Expand All @@ -75,6 +148,9 @@ public static function get_explicit_price( string $price, WC_Order $order = null
* @return array The modified arguments
*/
public static function get_explicit_price_args( $args ) {
if ( false === static::should_output_explicit_price() ) {
return $args;
}
if ( false === strpos( $args['price_format'], $args['currency'] ) ) {
$args['price_format'] = sprintf( '%s %s', $args['price_format'], $args['currency'] );
}
Expand Down
22 changes: 20 additions & 2 deletions includes/multi-currency/MultiCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class MultiCurrency {
*/
protected static $instance = null;

/**
* Static flag to show if the currencies initialization has been completed
*
* @var bool
*/
protected static $is_initialized = false;

/**
* Compatibility instance.
*
Expand Down Expand Up @@ -108,7 +115,7 @@ class MultiCurrency {
*
* @var array
*/
protected $available_currencies;
protected $available_currencies = [];

/**
* The default currency.
Expand All @@ -122,7 +129,7 @@ class MultiCurrency {
*
* @var array
*/
protected $enabled_currencies;
protected $enabled_currencies = [];

/**
* Client for making requests to the WooCommerce Payments API
Expand Down Expand Up @@ -240,6 +247,8 @@ public function init() {
if ( is_admin() ) {
add_action( 'admin_init', [ __CLASS__, 'add_woo_admin_notes' ] );
}

static::$is_initialized = true;
}

/**
Expand Down Expand Up @@ -1021,4 +1030,13 @@ private function register_admin_scripts() {
\WC_Payments::get_file_version( 'dist/multi-currency.css' )
);
}

/**
* Returns if the currency initialization are completed
*
* @return bool If the initializations have been completedÎ
*/
public static function is_initialized() : bool {
return static::$is_initialized;
}
}
Loading

0 comments on commit a19aafb

Please sign in to comment.