skip to Main Content

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

2

Answers


  1. You can use acf field or metabox or custom field woocommerce. All you need to do is hook it into place. For example woocommerce_after_shop_loop_item

    Login or Signup to reply.
  2. 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;
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search