产品类别的 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.