Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

php - Custom decimals in WooCommerce product prices for a product category

Currently we have set up two decimal places for pricing as a default.

The problem: we have a product category which needs to show three decimal points for prices. All other shop products/categories should remain 2 decimal places.

Does anyone have a handy snippet or suggestion how to achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Below you will find a custom function hooked in wc_get_price_decimals filter hook, that will set 3 decimals product prices for a defined product category in product pages, shop page, category and tag archives pages (but not in cart items, or order items).

Here is that code:

add_filter( 'wc_get_price_decimals', 'custom_price_decimals', 10, 1 );
function custom_price_decimals( $decimals ){
    global $product;

    if( is_a( $product, 'WC_Product' ) ){
        // Only for a defined product category
        if( has_term( 'clothing', 'product_cat', $product->get_id() ) )
            $decimals = 3;
    }
    return $decimals;
}

The Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...