简体   繁体   English

如果产品属于 WooCommerce 中的某个类别,则在存档页面的价格下方添加自定义文本

[英]Add custom text below price on archive pages if product is in a certain category in WooCommerce

I'm trying to add a custom message below the price (not on single product pages) on any product that is inside a certain category (or two.)我正在尝试在某个类别(或两个类别)内的任何产品的价格下方(不是在单个产品页面上)添加自定义消息。

This is how far I've gotten:这是我已经走了多远:

add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;

if ( is_product_category( 'swim-trunks' ) ) {  

echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}

But this is only working on the category slug meaning it's only working on that specific category and isn't taking into consideration if that product is in another category as well.但这仅适用于类别 slug,这意味着它仅适用于该特定类别,并且不考虑该产品是否也属于另一个类别。 And finally, it's not working in product sliders on the homepage or related products.最后,它不适用于主页或相关产品的产品滑块。

Edit:编辑:

So, I've updated the code here and this seems to work:所以,我在这里更新了代码,这似乎有效:

/* Begin Custom Text below price on shop page */
add_action( 'woocommerce_after_shop_loop_item', 'custom_text', 5 );
function custom_text() {
global $product;

if ( has_term( 76, 'product_cat' ) || has_term( 71, 'product_cat' )) { 

    echo '<span class="custom-text">Buy Any 3 Swim Trunks For $99</span>';
}
}
/* End Custom Text below price on shop page */

If anyone has a better solution, I'd love to hear it!如果有人有更好的解决方案,我很乐意听到!

You can use acf field or metabox or custom field woocommerce.您可以使用 acf 字段或 metabox 或自定义字段 woocommerce。 All you need to do is hook it into place.您需要做的就是将其挂到位。 For example woocommerce_after_shop_loop_item例如woocommerce_after_shop_loop_item

You can try this one for custom test bellow product price on product single page -您可以在产品单页上尝试这个用于自定义测试的产品价格 -

add_filter( 'woocommerce_get_price_html', 'woo_add_text_after_price_single_product', 20, 1 );

function woo_add_text_after_price_single_product($price){

  // Your custom text
  $custom_text = '<span style="color: #ff0000;">My Custom Text Here...</span>';

   return $price.'<br>'.$custom_text;
}

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 WooCommerce 商店和存档页面上的产品类别在产品价格下方显示图像 - Display image below product price based on product category on WooCommerce shop and archive pages 在 Woocommerce 存档页面上的产品价格后添加图标 - Add an icon after product price on Woocommerce archive pages 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages 为 WooCommerce 类别存档页面添加产品标题标签 - Add product title heading tag for WooCommerce category archive pages 在 woocommerce 存档中添加产品类别 - Add Product category in woocommerce archive 在特定的 Woocommerce 产品类别存档页面下添加文本 - Add a text under specific Woocommerce product category archive page 在 Woocommerce 中为特定类别的产品价格前添加文本 - Add text before product price for specific category in Woocommerce 在 WooCommerce 单个产品页面中的添加到购物车按钮下方添加自定义文本 - Add custom text below Add To Cart button in WooCommerce single product pages 如何在 Woocommerce 缺货产品文本下方添加单个产品的 Woocommerce 产品价格 - How can I add below of Woocommerce out of stock product text the Woocommerce product price on a single product WooCommerce产品类别存档,添加其他详细信息 - WooCommerce product category archive, add additional details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM