4

I'm busy with a wordpress/woocommerce site and I'd like to hide the price of an item when it's zero on the product category/archive page.

I've tried looking on the web and in the php files but couldn't find where I should add the if statement.

My php knowledge is pretty limited. I was wondering if someone else has experience with this or could help me in the right way

Thanks!

Iliass Assoued
  • 111
  • 1
  • 2
  • 8

6 Answers6

10

This code works with Woocommerce 3.6.3. Copy into your functions.php

//Hide Price when Price is Zero
add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if($product->get_price()>0){
          return $price_html;
     }
     return '';
 } 
// End of above code
KoolPal
  • 374
  • 5
  • 17
  • 1
    This works for me in WooCommerce 4.5.2. I checked the server logs and no warnings are thrown, either, so this implementation works. I also included a check for `is_admin()` so the prices in the product list in the backend were not affected. – Ryan Burney Oct 01 '20 at 02:42
6

Here's another approach:

add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if($product->get_price()>0){
          return $price_html;
     }
     return '';
 }
CragMonkey
  • 808
  • 1
  • 11
  • 22
  • 1
    The above code is elegant but gave an error `Notice: "price" was called incorrectly. Product properties should not be accessed directly.` when used with Woocommerce 3.6.3 See my new answer where this code works again – KoolPal May 19 '19 at 10:01
3

I haven't tested it but I expect something like this should do the job. Add this to your functions.php file.

add_action('woocommerce_before_shop_loop_item','custom_remove_loop_price');
function custom_remove_loop_price(){
    global $product;
    if(!$product->price){
        remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
    }
}
Jared
  • 718
  • 5
  • 11
  • It removes every price from category page. Not 100% what i was looking for but it might be a better solution for my site. Thanks a lot! – Iliass Assoued Apr 11 '15 at 21:35
  • 1
    That's the issue with this code, if the $product->price returns false once while in the loop, it hides all prices no matter what. This is not the answer. – Caleb Prenger Jul 10 '17 at 15:51
2

This one finally did it for me (using woocommerce 3.0.8):

function cw_change_product_price_display( $price ) {
    $clear = trim(preg_replace('/ +/', ' ', preg_replace('/[^A-Za-z0-9 ]/', ' ', urldecode(html_entity_decode(strip_tags($price))))));
    if($clear == "0 00"){
      return '';
    }
    return $price;
  }
  add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
  add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
Markus Andreas
  • 935
  • 1
  • 13
  • 12
1

!is_admin()

add_filter( 'woocommerce_get_price_html','maybe_hide_price',10,2);
function maybe_hide_price($price_html, $product){
     if( $product->get_price() == 0 && !is_admin() ){
          return ''; //or return 'Any text'
     }
     return $price_html;
 } 
0

Tried Jared's answer but just as someone stated, it removed every price once the condition was met. Ultimately I went this solution where it removed the action if the price was 0 and added it back if it wasn't. Seems to work.

public function woocommerce_after_shop_loop_item_title_remove_product_price(){
    global $product;
    $price = floatval( $product->get_price() );
    if( $price <= 0 ){
        remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    }else{
        add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    }
}

add_action( 'woocommerce_after_shop_loop_item_title', 'category_page_changes', 0 );
rip747
  • 9,375
  • 8
  • 36
  • 47