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

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

本文介绍了在Woocommerce中根据产品类别更改货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据产品类别更改默认的Woocommerce货币符号。

I am trying to change the default Woocommerce currency symbol based on the product category.

我的默认WC货币设置为USD,并且我的所有产品均显示<$价格前的c $ c>'$'前缀。但是,我只想显示'$'而不是'$$$' 清仓 类别。

My default WC currency is set to USD and all my products display with '$' prefix before the price. But instead of '$', I would like to show '$$$' only for the products that are in 'clearance' category.

这是我的代码:

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;
    }
}

它有效,并显示 $$$仅适用于清仓类别中的产品,但是会从其余类别的所有产品中删除 $。

It works, and '$$$' is displayed only for the products within the 'clearance' category, however it removes the '$' from all products in the remaining categories.

我需要if语句不执行以下操作:

I need that if statement to do nothing if the condition is not met.

我也尝试过使用 endif 结束标记,如下所示:

I've also tried with endif closing tag like this:

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;
}

但这里也是一样。它为清仓类别中的所有产品显示'$$$',但为任何产品删除了'$'其他产品。

but same thing here. It shows '$$$' for all products within 'clearance' category, but removes the '$' for any other product.

我在做什么错了?

推荐答案

您需要输入返回$ currency_symbol; if 语句之外:

You need to put return $currency_symbol; outside the if statement this way:

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

现在它应该可以工作了。

Now it should work.

这篇关于在Woocommerce中根据产品类别更改货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:10