Custom decimals in WooCommerce product prices for a product category

雨燕双飞 提交于 2019-12-11 00:36:24

问题


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?


回答1:


Bellow you will ind 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.



来源:https://stackoverflow.com/questions/45424141/custom-decimals-in-woocommerce-product-prices-for-a-product-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!