multi_currency)
&& method_exists($woocommerce_wpml->multi_currency, 'get_client_currency')
) {
self::$_currency = $woocommerce_wpml->multi_currency->get_client_currency();
}
if (empty(self::$_currency)) {
self::$_currency = get_option('woocommerce_currency', 'USD');
}
}
$vary_list['wcml_currency'] = self::$_currency;
return $vary_list;
}
/**
* Inject the WCML vary row into the crawler cookie list.
*
* @since 7.9
* @access public
* @param mixed $cookies Crawler cookie list.
* @return array
*/
public static function inject_vary_row( $cookies ) {
if (!is_array($cookies)) {
$cookies = [];
}
$currencies = self::_currencies();
$selected = count($currencies) >= 2 ? self::_selected($currencies) : [];
if (empty($selected)) {
return $cookies;
}
$vals = [];
foreach ($selected as $code) {
$h = self::_vary_hash($code);
if (!empty($h)) {
$vals[] = $h;
}
}
if (empty($vals)) {
return $cookies;
}
$vary_name = \LiteSpeed\Vary::cls()->get_vary_name();
// Merge with admin's same-name rows
$existing_vals = [];
$other_rows = [];
foreach ($cookies as $c) {
if (is_array($c) && isset($c['name']) && $vary_name === $c['name']) {
if (!empty($c['vals']) && is_array($c['vals'])) {
$existing_vals = array_merge($existing_vals, $c['vals']);
}
} else {
$other_rows[] = $c;
}
}
$other_rows[] = [
'name' => $vary_name,
'vals' => array_values(array_unique(array_merge($existing_vals, $vals))),
];
return $other_rows;
}
/**
* Render the WCML multi-currency crawler picker.
*
* @since 7.9
* @access public
* @return void
*/
public static function render_picker() {
$currencies = self::_currencies();
if (count($currencies) < 2) {
return;
}
$selected = self::_selected($currencies);
?>
get_vary_name();
if (empty($_COOKIE[ $vary_name ])) {
return;
}
$currencies = self::_currencies();
$selected = count($currencies) >= 2 ? self::_selected($currencies) : [];
if (empty($selected)) {
return;
}
$val = sanitize_text_field(wp_unslash($_COOKIE[ $vary_name ]));
foreach ($selected as $code) {
if (self::_vary_hash($code) === $val) {
add_filter('wcml_client_currency', function () use ( $code ) {
return $code;
}, 0);
self::apply_client_currency($code);
return;
}
}
}
/**
* User-selected currencies, intersected with what is currently available.
*
* @since 7.9
* @access private
* @param string[] $available Available WCML currencies.
* @return string[]
*/
private static function _selected( $available ) {
$stored = get_option(self::OPT_PICK, []);
if (!is_array($stored)) {
return [];
}
return array_values(array_intersect($available, $stored));
}
/**
* All active WCML currency codes.
*
* @since 7.9
* @access private
* @return string[]
*/
private static function _currencies() {
global $woocommerce_wpml;
if (is_object($woocommerce_wpml)
&& isset($woocommerce_wpml->multi_currency)
&& method_exists($woocommerce_wpml->multi_currency, 'get_currency_codes')
) {
return $woocommerce_wpml->multi_currency->get_currency_codes();
}
$settings = get_option('_wcml_settings', []);
if (!empty($settings['currency_options']) && is_array($settings['currency_options'])) {
return array_keys($settings['currency_options']);
}
return [];
}
/**
* Compute the vary cookie value for a guest under the given currency.
*
* @since 7.9
* @access private
* @param string $currency Currency code.
* @return string|false Vary value, or false if Vary cannot finalize (LITESPEED_GUEST etc.).
*/
private static function _vary_hash( $currency ) {
$prev = self::$_currency;
self::$_currency = $currency;
$hash = \LiteSpeed\Vary::cls()->finalize_default_vary(-1);
self::$_currency = $prev;
return $hash;
}
}