Hide Vendors With No Items

These two snippets will hide vendors who have not yet gotten around to publishing any items from both the Vendors Widget and the Vendors List page. WCMp displays these vendors by default… if you don’t want that, this snippet takes care of it for you. Note, this will not hide vendors who have all their products out of stock so it might appear that it doesn’t work; this only checks if they have published items.

As always, this is either added to your Child Theme’s functions.php file, or consider using the My Custom Functions Pro plugin to manage and organize all your snippets.

//Vendor Page
add_filter('wcmp_vendor_list_get_wcmp_vendors_args', 'hide_zero_product_vendor_on_vendor_list_page' , 10 , 4 );
function hide_zero_product_vendor_on_vendor_list_page ( $query, $order_by, $request, $atts ) {
   $vendor = get_wcmp_vendors();
   $vendor_ids = wp_list_pluck($vendor , 'id');
   foreach ($vendor_ids as $key => $value) {
      $vendor = get_wcmp_vendor($value);
      $vendor_products = $vendor->get_products();
      if (empty($vendor_products)){
          $get_vendor[] = $value;
       }
   }
   $query['exclude'] = $get_vendor;
   return $query; 
} 

//Vendors List Widget
add_filter('wcmp_widget_vendor_list_query_args','wcmp_hide_zero_product_vendor_on_vendor_list_widget');
function wcmp_hide_zero_product_vendor_on_vendor_list_widget($vendor_ids){
    $array_value = array();
    $get_vendor = get_wcmp_vendors($vendor_ids);
    $block_vendors = wp_list_pluck(wcmp_get_all_blocked_vendors(), 'id');
    foreach ($get_vendor as $key => $value) {
        $vendor = get_wcmp_vendor($value->id);
        $vendor_products = $vendor->get_products();
        if (empty($vendor_products)){
            $array_value[] = $value->id;
        }
    }
    $marge_block_and_zero_product_vendor = array_merge($array_value,$block_vendors);
    $vendor_ids['exclude'] =  $marge_block_and_zero_product_vendor;
    return $vendor_ids;
}

Tested and working on Woocommerce 4.6.1, WCMp 3.5.10

Is this no longer relevant or not working? Please let us know in the comments!

Comments on this post

Your email address will not be published. Required fields are marked *