woocommerce : non available zero products

The woommerce defaut behaviour for products where price is not set is to disable the add to cart button  : my customer wanted this behaviour for product that have zero mentionned. Default woocommerce makes zeo product free!, but we decided to disable shopping cart for those too. very simple thanks to filters and template system:

 

  • in your functions .php , add
     add_filter("woocommerce_free_price_html", "po_free");
     function po_free($price)
     {
        return '' ;
     }

     

  • then create in your theme te following files 
    • /woocommerce/single-product/add-to-cart/simple.php
    • /woocommerce/loop/add-to-cart.php
    • and modify the first conditionnal test to make woocommerce display "not available" if price is zero : 
      if(( $product->get_price() === ''   || $product->get_price()==0 )&& $product->product_type != 'external' ) {echo "<b>NON DISPONIBLE</b>" ;return;}

       

wordpress search by attachment title

Funny enough, there many search enhancement plugins for wordpress, but It seems none of them can do what I want : return posts if one its child attachments matches the search query. It's easy enough when you think about it but the code below will save you the hour I spent looking for it on google .

<?php
 
 
 
add_filter('posts_join', 'po_search_join' );
add_filter('posts_where', 'po_search_where' );
add_filter('posts_groupby', 'po_search_groupby' );

function po_search_join( $join )
{
  global  $wpdb, $wp_query;
       
  if($wp_query->is_search  ) {
    $join .= " left JOIN {$wpdb->prefix}posts p2  ON  {$wpdb->prefix}posts.ID = p2.post_parent  "   ;
 
  }

  return $join;
}

function po_search_where( $where )
{ global  $wpdb, $wp_query;    
  if($wp_query->is_search    ) {
      $s=get_query_var('s');
    $where = $where . " or (p2.post_title LIKE '%" . addslashes_gpc($s)  . "%'
    ) ";
   }                  
 
  return $where;
}

function po_search_groupby( $groupby )
{
  global  $wpdb, $wp_query;  

  if( !$wp_query->is_search ) {     return $groupby; }

  // we need to group on post ID
  $mygroupby = "{$wpdb->posts}.ID";

  if( preg_match( "/$mygroupby/", $groupby )) {
    // grouping we need is already there
    return $groupby;
  }

  if( !strlen(trim($groupby))) {
    // groupby was empty, use ours
    return $mygroupby;
  }

  // wasn't empty, append ours
  return $groupby . ", " . $mygroupby;
}