soldier99
soldier99

Reputation: 123

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?

Upvotes: 2

Views: 4666

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254373

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.

Upvotes: 3

Related Questions