我正在尝试根据产品类别更改默认的Woocommerce货币符号。
我的默认WC货币设置为USD,并且我的所有产品'$'
在价格之前显示前缀。但是'$'
,我只想显示类别中'$$$'
的产品,而不是'clearance'
。
这是我的代码:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product, $woocommerce;
if ( has_term( 'clearance', 'product_cat' ) ) {
switch( $currency ) {
case 'USD': $currency_symbol = '$$$'; break;
}
return $currency_symbol;
}
}
它起作用,并且仅针对“清除”类别中的产品显示“ $$$”,但是会从其余类别中的所有产品中删除“ $”。
如果不满足条件,我需要if语句不执行任何操作。
我也尝试过使用endif
关闭标记,例如:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product, $woocommerce;
if ( has_term( 'clearance', 'product_cat' ) ) :
switch( $currency ) {
case 'USD': $currency_symbol = '$$$'; break;
}
return $currency_symbol;
endif;
}
但这里也是一样 它'$$$'
为“清除”类别中的所有产品显示,但'$'
为其他任何产品删除。
我究竟做错了什么?
您需要以这种方式放在语句return $currency_symbol;
之外if
:
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
global $post, $product;
if ( has_term( 'clearance', 'product_cat' ) ) {
switch( $currency ) {
case 'USD': $currency_symbol = '$$$';
break;
}
}
return $currency_symbol; // <== HERE
}
代码进入您的活动子主题(或活动主题)的function.php文件中。
现在应该可以了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句