skip to Main Content

I want to show display a message when a product category has zero products. I want to do this via shortcode please.

I’ve tried searching StackOverflow and search engines for some codes to work off of but have been unsuccessful.

$category = get_queried_object();
$theCount = $category->count;

function farmdish_no_prod_msg_shortcode( $atts ) {

    $a = shortcode_atts(array('slug' => ''), $atts );

    if( ( is_product_category( '' . $a['slug'] . '' ) ) && ( $theCount > 0 ) ){


    }else {
        return '<span>No Products!</span>';
    }

}

add_shortcode( 'no-prod-msg', 'farmdish_no_prod_msg_shortcode' );

I need to show a simple text message when there are zero products in a WooCommerce product category. Thank your for any help!

3

Answers


  1. Shortcode is a wrong way of doing it.
    You can do it by adding a woocommerce template file:

    1. Create a folder named “woocommerce” in your theme directory (if not
      exists)
    2. Create another folder inside it called “loop”
    3. Create a file called “result-count.php” Add the following code to this file

      $total = wc_get_loop_prop( ‘total’ );
      if ($total === 0){
      echo ‘NO PRODUCTS HERE BUDDY…’;
      }

    Login or Signup to reply.
  2. Try this

    function farmdish_no_prod_msg_shortcode( $atts ) {
    
        $a = shortcode_atts(array('slug' => ''), $atts );
    
    
        $catObj = get_category_by_slug($a['slug']); 
    
        $theCount = $catObj->count;
    
        if( $theCount > 0  ){
    
    
        }else {
            return '<span>No Products!</span>';
        }
    
    }
    
    add_shortcode( 'no-prod-msg', 'farmdish_no_prod_msg_shortcode' );
    
    Login or Signup to reply.
  3. Try this:-

    function farmdish_no_prod_msg_shortcode( $atts ) {
        $a = shortcode_atts(array('slug' => ''), $atts );
    
        $catObj = get_category_by_slug($a['slug']); 
    
        $theCount = $catObj->count;
    
        if( $theCount == 0  ){
            return '<span>No Products!</span>';}
        }
    
    add_shortcode( 'no-prod-msg', 'farmdish_no_prod_msg_shortcode' );`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search