João Pedro
João Pedro

Reputation: 29

Changing the number of decimal places for a specific category in WooCommerce

I've been trying everything to make a change in my website and I'm not succeeding.

I set up WooCommerce to put 3 decimal places on all my products but right now I have a category called 'gas' that I need to have only 2 decimal places.

This is the code I tried but something is not working (I have very little experience with PHP):

add_filter( 'wc_get_price_decimals', 'custom_price_decimals', 10, 1 );
function custom_price_decimals( $decimals ){
    global $product;
    if( is_a( $product, 'WC_Product') ){
       
        if( has_term( 'gas', $product->get_id() ) )
            $decimals = 2;
    }
    return $decimals;
}

someone can help me? Best regards, João Pedro

Upvotes: 0

Views: 431

Answers (1)

mujuonly
mujuonly

Reputation: 11841

add_filter('wc_get_price_decimals', 'custom_price_decimals', 10, 1);

function custom_price_decimals($decimals) {
    global $product;
    if (is_a($product, 'WC_Product') && has_term('gas', 'product_cat', $product->get_id())) {
        $decimals = 2;
    }
    return $decimals;
}

Upvotes: 1

Related Questions