根据Woocommerce中的产品类别更改货币符号

时间:2018-01-24 10:48:38

标签: php wordpress if-statement woocommerce currency

我正在尝试根据产品类别更改默认的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;      
}

但同样的事情。它会针对“清除”类别中的所有产品显示'$$$',但会删除任何其他产品的'$'

我做错了什么?

2 个答案:

答案 0 :(得分:2)

  

相关:Custom cart item currency symbol based on product category in Woocommerce 3.3+

您需要将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文件。

现在应该可以了。

答案 1 :(得分:0)

为当前主题创建子主题可能是解决方案,但是我认为,如果您只想更改符号,则不必做那么多。

只需转到woocommerce后端文件..

wp-content/plugins/woocommerce/includes/wc-core-functions.php

如果您的Wordpress主题中没有后端文件过多的下载文件管理器,则您会在主站点中看到这些文件夹。

wp-content>>plugins>>woocommerce>>includes>>wc-core-functions.php

wc-core-function.php文件中,您将看到符号

enter image description here

相关问题