Wordpress get_option() 关于 AJAX 问题

Wordpress get_option() on AJAX issue

我在使用 Wordpress 中的 get_option 方法时遇到一些问题。

我是 运行 WooCommerce 网站,currency symbol position 设置为 "Right with space"。在 wp_options table 中 option_name "woocommerce_currency_pos" 被正确设置为 "right_space"。

在正常网站上查看产品时,一切正常,货币代码显示在右侧,选项值恢复正确。

然而,问题是我们使用了一点 JS 函数在灯箱中显示购物车 (mini_cart.php),返回的代码在左侧显示了货币代码。

经过一些调试,我找到了方法 get_woocommerce_price_format,这里的第一行是:$currency_pos = get_option( 'woocommerce_currency_pos' );

所以这只是一个默认函数,用于从选项 table 中获取规定的 option_name 的值。然而,这 returns "left" 在这个 Ajax 调用中。

我不知道到底是怎么回事,这返回的值与数据库中的值不同。

明显的潜在问题:

这个奇怪的问题是什么?

p.s。 AJAX 通过 /wp-admin/admin-ajax.php

调用

这是完整的功能,它不是 class 或任何

的一部分
/**
 * Get the price format depending on the currency position
 *
 * @return string
 */
function get_woocommerce_price_format ()
{
    $currency_pos = get_option('woocommerce_currency_pos');

    switch ($currency_pos) {
    case 'left' :
        $format = '%1$s%2$s';
        break;
    case 'right' :
        $format = '%2$s%1$s';
        break;
    case 'left_space' :
        $format = '%1$s %2$s';
        break;
    case 'right_space' :
        $format = '%2$s %1$s';
        break;
    }

    //$format = '%2$s %1$s';

    return apply_filters('woocommerce_price_format', $format, $currency_pos);
}

好吧,对于将来可能需要它的任何人。

Woocommerce 使用此行覆盖 get_option() 方法: add_filter('option_woocommerce_currency_pos', array($this, 'filter_currency_position_option'));

然后调用 filter_currency_position_option

该方法的前几行是:

function filter_currency_position_option($value){
    global $pagenow;

    if( ( is_ajax() || ( $pagenow == 'post-new.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'shop_order' ) ) && isset( $_COOKIE[ '_wcml_order_currency' ] ) ){
        $currency_code = $_COOKIE[ '_wcml_order_currency' ];

W-T-F

好的,所以它将货币位置存储在 cookie 中以供 AJAX 访问……我不确定我是否同意这一点,但至少现在我知道了。

希望这对其他人有所帮助。