我想在 WooCommerce 中的价格标签后显示一段文字.我有一个适用于我的functions.php 的代码,但它显示在所有产品类别中.
I would like to display a piece of text after the price tag in WooCommerce. I have a code that is working for my functions.php, but it shows on all product categories.
我想知道是否有人知道如何使此自定义文本仅显示选定的类别;或者甚至更好,未选择的产品类别,因为会显示文本的产品类别比不显示的产品类别多得多.
I was wondering if someone knows how to make this custom text to show for only selected categories; or even better, unselected product categories as there are a lot more product categories that will display the text than the ones who doesn't.
我的实际代码:
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
$mt = ' per M/T';
return $price . $mt;
}
试试下面的代码,使用 has_term() 条件函数来定义产品类别:
Try the following code that uses has_term() conditional function for defined product categories:
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('t-shirts','hoodies');
if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
$price .= ' ' . __('per M/T');
return $price;
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
要排除定义的产品类别,您将替换:
if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
简单地通过:
if( ! has_term( $product_categories, 'product_cat', $product->get_id() ) )
这篇关于WooCommerce 中选定产品类别的自定义产品价格后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)