class-wc-payments-explicit-price-formatter.php
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* Class WC_Payments_Explicit_Price_Formatter
*
* @package WooCommerce\Payments
*/
use WCPay\MultiCurrency\MultiCurrency;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* 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.
*/
public static function init() {
add_filter( 'woocommerce_cart_total', [ __CLASS__, 'get_explicit_price' ], 100 );
add_filter( 'woocommerce_get_formatted_order_total', [ __CLASS__, 'get_explicit_price' ], 100, 2 );
add_action( 'woocommerce_admin_order_totals_after_tax', [ __CLASS__, 'register_formatted_woocommerce_price_filter' ] );
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
*
* @return bool Whether if it should return explicit price or not
*/
public static function should_output_explicit_price() {
// If customer Multi-Currency is disabled, don't use explicit currencies.
// 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 initialized yet, skip the checks.
if ( ! self::$multi_currency_instance->is_initialized() ) {
return false;
}
// If no additional currencies are enabled, skip it.
if ( ! self::$multi_currency_instance->has_additional_currencies_enabled() ) {
return false;
}
return true;
}
/**
* Registers the get_explicit_price filter for the order details screen.
*
* There are no hooks that enable us to filter the output on the order details screen.
* So, we need to add a filter to formatted_woocommerce_price. We use specific actions
* to register and unregister the filter, so that only the appropriate prices are affected.
*/
public static function register_formatted_woocommerce_price_filter() {
add_filter( 'wc_price_args', [ __CLASS__, 'get_explicit_price_args' ], 100 );
}
/**
* Unregisters the get_explicit_price filter for the order details screen.
*
* There are no hooks that enable us to filter the output on the order details screen.
* So, we need to add a filter to formatted_woocommerce_price. We use specific actions
* to register and unregister the filter, so that only the appropriate prices are affected.
*/
public static function unregister_formatted_woocommerce_price_filter() {
remove_filter( 'wc_price_args', [ __CLASS__, 'get_explicit_price_args' ], 100 );
}
/**
* Returns the price suffixed with the appropriate currency code, if not already.
*
* @param string $price The price.
* @param WC_Abstract_Order|null $order The order.
*
* @return string
*/
public static function get_explicit_price( string $price, WC_Abstract_Order $order = null ) {
if ( false === static::should_output_explicit_price() ) {
return $price;
}
if ( null === $order ) {
$currency_code = get_woocommerce_currency();
} else {
$currency_code = $order->get_currency();
}
if ( ! empty( $currency_code ) ) {
$price_to_check = html_entity_decode( wp_strip_all_tags( $price ) );
if ( false === strpos( $price_to_check, trim( $currency_code ) ) ) {
return $price . ' ' . $currency_code;
}
}
return $price;
}
/**
* Alters the price formatting arguments to include explicit format
*
* @param array $args Price formatting args passed through `wc_price_args` filter.
*
* @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'] );
}
return $args;
}
}