我试图根据产品类别更改默认的Woocommerce货币符号。
我的默认WC货币被设置为美元,我的所有产品在价格之前都会显示'$'前缀。但是,与'$'不同,我只想显示属于'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;
}但这里也是一样的。它显示了“许可”类别中所有产品的'$$$',但删除了任何其他产品的'$'。
我做错了什么?
发布于 2018-01-24 11:27:46
您需要这样将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文件中。
现在该起作用了。
发布于 2020-03-04 08:44:21
为当前主题创建子主题可能是解决方案,但我认为,如果您只想更改符号,则不必做那么多工作。
只需转到woocommerce后端文件。
wp-content/plugins/woocommerce/includes/wc-core-functions.php
如果在Wordpress主题中没有后端文件多余的下载文件管理器,您将在主站点中看到这些文件夹。
wp-content>>plugins>>woocommerce>>includes>>wc-core-functions.php
在wc-core-function.php文件中,您将看到符号
https://stackoverflow.com/questions/48420586
复制相似问题