How to filter product by Vendor at shop page

Theme and Plugin Support

How to filter product by Vendor at shop page Resolved

Viewing 13 reply threads
  • Author
    Posts
    • #72924
      ibrahimha
      Participant

      At Shop page, which displays all the products I want to allow users to filter products by Vendor ” store “. see image below displays all vendors in the dropdown and when a user clicks on one all vendor product will appear.

    • #73153

      Hi, in order to add this flow you have to do some custom code.

      For this, you need to use these filters :

      – Add dropdown in search bar

      /* hook to add dropdown or something for vendors */
      add_action( 'woocommerce_before_shop_loop', 'add_new_dropdown', 10 );
      
      function add_new_dropdown() {
      //add your code here//
      }
      copy

      – Sort the products as per vendors :

      /* sort products on shop page by vendor*/
      add_action('pre_get_posts','shop_filter_cat');
      
      function shop_filter_cat($query) {
         //for eaxmple if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
            $query->query_vars['author']=3;
         }//
      }
      copy
    • #73462
      ibrahimha
      Participant

      Hey Thanks for your reply, I want to display vendors in a dropdown can I cutomize the below short code to display only vendo name and id to build a dropdown and use it as a filter dropdown

      wcmp_vendorslist orderby=”registered” order=”ASC”]

      $query->query_vars[‘author’]=$cat_id;

      Again thanks for your help 🙂

    • #73481
      ibrahimha
      Participant

      I deleted the extra info from shortcode/vendor_lists.php and I managed to display vendors in select, and filter products by the selected vendor from the dropdown when the user click on submit button.

      Issues
      If I make the form submit on change

      <select name="spbv" onchange="this.form.submit()">
      copy

      the page keep refresh cause the select options are inside foreatch.

         
      /themes/themename/dc-product-vendor/shortcode/vendor_lists.php  
        
           <form name="vendor_sort" method="post" >
                      <div class="vendor_sort">
                          
                          <select class="select short" id="vendor_sort_type" name="spbv" onchange="this.form.submit()">
      
                          <?php
                           if ($vendors && is_array($vendors)) {
                              foreach ($vendors as $vendor_id) {
                                  $selected = '';
                                  $vendor = get_wcmp_vendor($vendor_id);
                                  $image = $vendor->get_image() ? $vendor->get_image('image', array(125, 125)) : $WCMp->plugin_url . 'assets/images/WP-stdavatar.png';
                                  $banner = $vendor->get_image('banner') ? $vendor->get_image('banner') : '';
                                  
                                   if (isset($_POST['spbv']) && $_POST['spbv'] == $vendor->id) {    
                                       
                                         $selected = 'selected="selected"';
                                   }
      
                                  echo '<option value="' . $vendor->id . '">' . $vendor->page_title . '</option>';
                                  ?>
                          
                                  <?php
                              }
                          } else {
                              _e('No vendor found!', 'dc-woocommerce-multi-vendor');
                          }
                          ?>
                              <noscript><input type="submit" value="Submit"></noscript>
      
                          </select>
                          </div>
                  </form>
      
      copy
      
      functions.php
      
       add_action( 'woocommerce_before_shop_loop', 'add_new_dropdown', 10 );
      
       function add_new_dropdown() {
      
         echo do_shortcode('[wcmp_vendorslist orderby="registered" order="ASC"]');
      
       }
      
       add_action('pre_get_posts','shop_filter_cat');
         function shop_filter_cat($query) {
      
            if( isset($_POST['spbv']) || '' !== $_POST['spbv']) {
                  
      	   $size = $_POST['spbv'];
                 //for eaxmple if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
                 $query->query_vars['author']=$size;
      
                  
              }
                      
       }
      
      
      copy
    • #73487
      ibrahimha
      Participant

      Fixed it by this small function

                          var val = document.vendor_sort.spbv.value;
                          console.log(val);
                          if(val!=-1){
                              document.vendor_sort.submit();
                          }
                      }
      
      copy
    • #73488
      ibrahimha
      Participant

      How to display the filter I created on shop page sidebar? widget id is wooshopsidebar

    • #73490
      ibrahimha
      Participant

      Fixed,

      How to query an array, cause user can checkbox input

      $query->query_vars['author']=array(2,5,6);
      copy

      When I tried this

      $query = new WP_Query( array( 'author' => '2,6,17,38' ) );
      copy

      A Fatal error appears dont know why!

      
      Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in /home3/x/public_html/wp-includes/class-wp-query.php on line 878
      
      Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in /home3/x/public_html/wp-includes/functions.php on line 5971
      
      
      copy

      any thoughts ?

    • #73503

      Hi @ibrahimha, it seems like this requires more memory than the default allocated memory, hence this error message.

      So, you need to increase PHP memory limit in WordPress to avoid memory exhausted error.

      For this please do follow this procedure – https://docs.wp-rocket.me/article/128-increase-php-memory-limit-in-wordpress

      Let us know if you have any further query.

    • #73513
      ibrahimha
      Participant

      Thanks @NerdySupportExpe, even if I make it 2048 I keep git the same fatal error the query should return like 10 products!

      could you check my code

      
      add_action('pre_get_posts','shop_filter_cat');
      
      function shop_filter_cat($query) {
          
          if (  is_product_category() || is_product_tag() || is_shop() ) {
      
          	if( isset($_POST['spbv']) || '' !== $_POST['spbv']) {
          
          		$vendor  =   $_POST['spbv'];
                // products from user '2'
                // $query	->	query_vars['author']	=	"2";
                //  get products from user 2,3 return the fatal error Allowed memory size though it's set to 2048M
                  $query = new WP_Query( array( 'author' => '2,3' ) );
              }
          }
      
      }
      
      copy
    • #73613

      @ibrahimha, can you please just add this code in your function.php and remove all the other codes for testing purpose?

      Then check whether you are still getting this error?

      • #73636
        ibrahimha
        Participant

        Yup nd I still get the same error.

    • #73710

      @ibrahimha, As you had added ‘author’, this will only accept one user_id

      When you want to pass multiple author then you should use

      author__in
      copy

      in place of author, and it should be an array format.

    • #73813
      ibrahimha
      Participant

      all good;
      Is it possible to use the same shortcode/vendor_lists.php file for tow HTML designs one with images and another display name only?

      I tried to duplicate the file and make shortcode for it, but there are variables passed to the template file form this class WCMp_Shortcode_Vendor_List like vendors.

      Simple code

      <?php if(do_shortcode_bool_with_images == true ) { ?>
      <h2>Print Items with Icons </h2>
      
      <ul>
          <li>Coffee</li>
      </ul>
      <?php } ?>
      
      <?php if(do_shortcode_bool_text_only == true ) { ?>
      
      <h2>Print Names only without icons</h2>
      
      <ol>
          <li>Coffee</li>
      </ol>
      <?php } ?>
      copy
    • #73911

      @ibrahimha, this is a shortcode. Hence just adding the code will not work.

      You need to check our code and implement the concept in your way.

    • #75232
      MOUMITA Haldar
      Participant

      Hi @ibrahimha,

      I presume that your issue is resolved, since, we haven’t heard back from you.

      We are marking this thread as resolved.

      Kindly open a new thread if you face any further query.

Viewing 13 reply threads

The topic ‘How to filter product by Vendor at shop page’ is closed to new replies.

WCMp bundle is available at a slashed price

Grab It Now