产品类别的 WooCommerce 产品价格中的自定义小数 [英] Custom decimals in WooCommerce product prices for a product category

查看:70
本文介绍了产品类别的 WooCommerce 产品价格中的自定义小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们默认为定价设置了两位小数.

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

问题:我们有一个产品类别需要显示三个小数点的价格.所有其他商店产品/类别应保留 2 位小数.

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?

推荐答案

下面你会发现一个自定义函数挂在 wc_get_price_decimals 过滤钩子中,它会设置 3 位小数 产品页面、商店页面、类别和标签档案页面中定义的产品类别的产品价格(但不在购物车项目或订单项目中).

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).

这是代码:

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

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已经过测试且有效.

This code is tested and works.

这篇关于产品类别的 WooCommerce 产品价格中的自定义小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆