skip to Main Content

We need set site-wide cookie filter by brands.

WooComerce have build-in Brands taxonomy.

At the start landing page user will be select one from several brands. And after, user views products on site only by selected brand. If user will visit brand url (www.example.com/brands/brand-second), then need change filter to another brand, and shows products only by last selected brand. Others products not allowed to shows (another brand or without brand).

What any ideas for realize this?

2

Answers


  1. Chosen as BEST ANSWER

    I found solution for my question!

    function filter_products_by_brand( $q ) {
    
        $tax_query = (array) $q->get( 'tax_query' );
    
        $tax_query[] = array(
            'taxonomy' => 'brand',
            'field' => 'slug',
            'terms' => array( 'dobro' ),  // Don't display products in the clothing category on the shop page.
            'operator' => 'NOT IN'
        );
    
        $q->set( 'tax_query', $tax_query );
    }
    add_action( 'pre_get_posts', 'filter_products_by_brand' );
    

  2. I think you can try hold the visible brand in a cookie, and use woocommerce_product_query or pre_get_posts filters to filter according to that cookie value.

    Each time the user visits that brand URL, change the cookie value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search