我在Wordpress中遇到get_option
方法的一些问题。
我正在运行一个WooCommerce网站,而currency symbol position
设置为“正确的空间”。在wp_options表中,option_name
“woocommerce_currency_pos”正确设置为“right_space”。
在普通网站上查看产品时,一切正常,货币代码显示在右侧,选项值将恢复正确。
然而问题是我们使用一个小JS函数在灯箱中显示购物车(mini_cart.php
),返回的代码显示左侧的货币代码。
经过一些调试后,我找到了方法get_woocommerce_price_format
,第一行是:$currency_pos = get_option( 'woocommerce_currency_pos' );
因此,这只是一个默认函数,用于从选项表中获取所述option_name
的值。然而,这在此Ajax调用中返回“left”。
我无法弄清楚究竟是如何返回与数据库中不同的值。
明显的潜在问题:
die($currency_pos)
返回字符串)这个奇怪的问题是什么?
P.S。
通过/wp-admin/admin-ajax.php
这是完整的功能,它不是类或任何东西的一部分
/**
* 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);
}
答案 0 :(得分:1)
嗯,对于将来可能需要这个的人。
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
好的,所以它将货币位置存储在用于AJAX访问的cookie中......我不确定我是否同意这一点,但至少现在我知道了。
希望这会帮助别人。