WooCommerce 中选定产品类别的自定义产品价格后缀

Custom product price suffix for selected product categories in WooCommerce(WooCommerce 中选定产品类别的自定义产品价格后缀)
本文介绍了WooCommerce 中选定产品类别的自定义产品价格后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想在 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 中选定产品类别的自定义产品价格后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)