WCMp bundle is available at a slashed price

Grab It Now

How to filter product by Vendor at shop page

How to filter product by Vendor at shop page2019-09-05T07:29:10-05:00

Home Support Forum Theme and Plugin Support How to filter product by Vendor at shop page

Theme and Plugin Support

How to filter product by Vendor at shop page Open

  • Author
    Posts
  • ibrahimha
    Participant
    Post count: 9

    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.

  • NerdySupportExpert Moumita
    Keymaster
    Post count: 5108

    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//
    }

    – 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;
       }//
    }
  • ibrahimha
    Participant
    Post count: 9

    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 🙂

  • ibrahimha
    Participant
    Post count: 9

    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()">

    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>
    
    
    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;
    
                
            }
                    
     }
    
    
  • ibrahimha
    Participant
    Post count: 9

    Fixed it by this small function

                        var val = document.vendor_sort.spbv.value;
                        console.log(val);
                        if(val!=-1){
                            document.vendor_sort.submit();
                        }
                    }
    
  • ibrahimha
    Participant
    Post count: 9

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

  • ibrahimha
    Participant
    Post count: 9

    Fixed,

    How to query an array, cause user can checkbox input $query->query_vars['author']=array(2,5,6);

    When I tried this $query = new WP_Query( array( 'author' => '2,6,17,38' ) );
    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
    
    

    any thoughts ?

  • NerdySupportExpert Moumita
    Keymaster
    Post count: 5108

    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.

  • ibrahimha
    Participant
    Post count: 9

    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' ) );
            }
        }
    
    }
    
  • NerdySupportExpert Moumita
    Keymaster
    Post count: 5108

    @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?

    • ibrahimha
      Participant
      Post count: 9

      Yup nd I still get the same error.

  • NerdySupportExpert Moumita
    Keymaster
    Post count: 5108

    @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 in place of author, and it should be an array format.

  • ibrahimha
    Participant
    Post count: 9

    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 } ?>
  • NerdySupportExpert Moumita
    Keymaster
    Post count: 5108

    @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.

Please LOGIN to reply to this topic