基于Woocommerce 3.3+中产品类别的自定义购物车商品货币符号 [英] Custom cart item currency symbol based on product category in Woocommerce 3.3+

查看:80
本文介绍了基于Woocommerce 3.3+中产品类别的自定义购物车商品货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据产品类别显示自定义货币符号。我采纳了关于此问题的以下摘要建议更改Woocommerce中基于产品类别的货币符号

I want to display a custom currency symbol based on product category. I took the following snippet suggested on this question Change currency symbol based on product category in Woocommerce.

但是我实际上需要在所有Woocommerce页面(包括购物车和结帐页面)中也显示自定义货币。

However I actually need the custom currency to be displayed also in all Woocommerce pages (including cart and checkout page).

这是我的代码:

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( 'cupones', 'product_cat' ) ) :

        switch( $currency ) {
             case 'EUR': $currency_symbol = 'ptos'; break;
        }
        return $currency_symbol;

    elseif ( has_term( 'cupones', 'product_cat' ) && is_cart() ): 

        switch( $currency ) {
             case 'EUR': $currency_symbol = 'ptos'; break;
        }
        return $currency_symbol;

    elseif ( has_term( 'cupones', 'product_cat' ) && is_checkout() ): 

        switch( $currency ) {
             case 'EUR': $currency_symbol = 'ptos'; break;
        }
        return $currency_symbol;  
    endif;      
}


推荐答案

已更新:对于购物车和结帐页面,有两种方法:按项目或全局。

Updated: For cart and checkout pages, there is 2 ways: By item or globally.

1)按项目

// HERE below your settings
function custom_currency_symbol_settings(){
    return array(
        'curr_symbol' => 'ptos', // <=== HERE define your currency symbol replacement
        'currency'    => 'EUR', // <=== HERE define the targeted currency code
        'category'    => array('cupones'), // <=== HERE define your product category(ies)
        'taxonomy'    => 'product_cat',
    );
}

// On product pages - Custom currency symbol
add_filter( 'woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2 );
function change_existing_currency_symbol( $currency_symbol, $currency ) {
    global $post, $product;

    // Loading your settings
    $data = custom_currency_symbol_settings();

    // Others Woocommerce product pages
    if ( has_term( $data['category'], $data['taxonomy'] ) && $currency == $data['currency'] ) {
        return $data['curr_symbol'];
    }

    return $currency_symbol;
}

// On cart item product price (Cart page) - Custom currency symbol
add_filter( 'woocommerce_cart_product_price', 'change_cart_item_price_currency_symbol', 10, 2 );
function change_cart_item_price_currency_symbol( $product_price_html, $product ) {

    // Loading your settings
    $data = custom_currency_symbol_settings();

    // Get the correct product ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for the defined product category and the defined currency
    if ( ! has_term( $data['category'], $data['taxonomy'], $product_id  ) || get_woocommerce_currency() != $data['currency'] ) {
        return $product_price_html; // Exit
    }

    if ( WC()->cart->display_prices_including_tax() ) {
        $price = wc_get_price_including_tax( $product );
    } else {
        $price = wc_get_price_excluding_tax( $product );
    }

    return wc_price_custom( $price, $data['curr_symbol'] );
}

//  On cart item line subtotal (Cart and Checkout pages) - Custom currency symbol
add_filter( 'woocommerce_cart_product_subtotal', 'change_cart_item_subtotal_currency_symbol', 10, 8 );
function change_cart_item_subtotal_currency_symbol( $product_subtotal, $product, $quantity, $cart ) {

    // Loading your settings
    $data = custom_currency_symbol_settings();

    // Get the correct product ID
    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    // Only for the defined product category and the defined currency
    if ( ! has_term( $data['category'], $data['taxonomy'], $product_id  ) || get_woocommerce_currency() != $data['currency'] ) {
        return $product_price_html; // Exit
    }

    if ( $product->is_taxable() ) {

        if ( $cart->display_prices_including_tax() ) {
            $row_price        = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
            $product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );

            if ( ! wc_prices_include_tax() && $cart->get_subtotal_tax() > 0 ) {
                $product_subtotal .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
            }
        } else {
            $row_price        = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
            $product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );

            if ( wc_prices_include_tax() && $cart->get_subtotal_tax() > 0 ) {
                $product_subtotal .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
            }
        }
    } else {
        $row_price        = $product->get_price() * $quantity;
        $product_subtotal = wc_price_custom( $row_price, $data['curr_symbol'] );
    }
    return $product_subtotal;
}

// Custom formatting price function replacement
function wc_price_custom( $price, $curr_symbol, $args = array() ){
    extract( apply_filters( 'wc_price_args', wp_parse_args( $args, array(
        'ex_tax_label'       => false,
        'currency'           => '',
        'decimal_separator'  => wc_get_price_decimal_separator(),
        'thousand_separator' => wc_get_price_thousand_separator(),
        'decimals'           => wc_get_price_decimals(),
        'price_format'       => get_woocommerce_price_format(),
    ) ) ) );

    $unformatted_price = $price;
    $negative          = $price < 0;
    $price             = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
    $price             = apply_filters( 'formatted_woocommerce_price', number_format( $price, $decimals, $decimal_separator, $thousand_separator ), $price, $decimals, $decimal_separator, $thousand_separator );

    if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 ) {
        $price = wc_trim_zeros( $price );
    }

    $formatted_price = ( $negative ? '-' : '' ) .
        sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . $curr_symbol . '</span>', $price );
    $return          = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';

    if ( $ex_tax_label && wc_tax_enabled() ) {
        $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
    }
    return apply_filters( 'wc_price', $return, $price, $args, $unformatted_price );
}

2)全球(容易得多)

2) GLOBALLY (Much more easier):

以下基于您的代码设置的代码也将全局更改购物车和结帐页面上的货币符号,当'cupones'产品类别时可以在任何购物车商品中找到:

The following code based on your code settings will change the currency symbol globally on cart and on checkout pages too, When 'cupones' product category is found in any cart item:

add_filter( 'woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2 );
function change_existing_currency_symbol( $currency_symbol, $currency ) {
    global $post, $product;

    $custom_sym = 'ptos'; // <=== HERE define your currency symbol replacement
    $custom_cur = 'EUR'; // <=== HERE define the targeted currency code
    $category   = array('cupones'); // <=== HERE define your product category(ies)
    $taxonomy   = 'product_cat';

    // Cart and checkout
    if( ( is_cart() || is_checkout() ) && $currency == $custom_cur ){
        foreach( WC()->cart->get_cart() as $cart_item ){
            if ( has_term( $category, $taxonomy, $cart_item['product_id'] ) ){
                return $custom_sym; // Found! ==> we return the custom currency symbol
            }
        }
    }

    // Others Woocommerce product pages
    if ( has_term( $category, $taxonomy ) && $currency == $custom_cur ) {
        return $custom_sym;
    }

    return $currency_symbol;
}

代码包含在您活动的子主题的function.php文件中(或活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于基于Woocommerce 3.3+中产品类别的自定义购物车商品货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆