【问题标题】:Custom cart item currency symbol based on product category in Woocommerce 3.3+基于 Woocommerce 3.3+ 中产品类别的自定义购物车项目货币符号
【发布时间】:2018-10-09 18:06:48
【问题描述】:

我想根据产品类别显示自定义货币符号。我接受了关于这个问题Change currency symbol based on product category in Woocommerce 的以下 sn-p 建议。

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

这是我的代码:

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

【问题讨论】:

    标签: php wordpress woocommerce currency custom-taxonomy


    【解决方案1】:

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

    1) BY ITEM (最复杂的)

    // 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) 全局(更容易)

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

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 嗨@LoicTheAztec!两者都很棒的解决方案,但它没有完全得到我正在寻找的东西......第一个(最复杂的)在所有页面中显示自定义货币,但在购物车总数中,它仍然显示“€”而不是“ptos”用于“杯子”类别的产品。而对于第二个(全球),当另一个类别(不同于“cupones”)的一个产品额外添加一个“cupones”类别产品时,它们都以“ptos”货币显示......
    • @TrendingConsullting 这对于 Stack OverFlow 来说太宽泛了,而且非常复杂。不能不考虑总数,因为您可以有不同的项目……请注意,Stack OverFlow 不是免费的编码服务,您最好聘请开发人员。
    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 2016-10-18
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多