Support » Plugin: WooCommerce » Show products which are in 2 different taxonomies

  • Hello!

    I have the following structure on my website.

    Main Categories:
    Street, Motocross and Cruiser.

    Also I created 3 custom taxonomies for them: (Gender, Product Type and Brand).

    So for those 3 main categories every one of them should have a shop page which I’ll set up new arrivals etc.

    The big problem is how would I show t-shirts on the Motocross page?

    Should I create a product loop to get all products contained in the Category Motocross and the Custom Taxonomy Product Type (t-shirt)

    Can you guys help me?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • So for those 3 main categories every one of them should have a shop page which I’ll set up new arrivals etc.

    I believe those would be category archive pages. So for if you assign both Motorcross & T-shirt (custom taxonomy) to a product then when user clicks on Motorcross category, the resultant category archive page is going to show all products which have Motorcross and T-shirt assigned to it.

    But, I will suggest revisit your requirement and think if you really need to make Gender, Product Type & Brand as custom taxonomy and not product attributes?

    Thread Starter eddiedoidao

    (@eddiedoidao)

    Hi Prasad!

    I would like to use those custom taxonomies because it would be easier to bulk edit them.
    Also would be easier to edit any product just marking those checkbox at the right side.

    So what I’m thinking is to make 3 pages. I’ll explain how it would be for the Street category.

    1 Section with new arrivals
    1 Section with Best Sellers Shirts (SHOW ONLY SHIRTS THAT ARE IN THE CATEGORY STREET AND HAVE THE CUSTOM TAXONOMY T-SHIRT) <– thats my real problem.

    I don’t know how can I show a product being of the category Street and having a custom taxonomy (product type) called t-shirt.

    About the brand, I can change to attribute, but it’s not my real problem.

    Hope you understand my problem.

    Thank you so much!!

    So you will create “Street” as category using WooCommerce Default product category. Then you will create “Type” as custom taxonomy and “T-shirt” as category under that custom taxonomy. (Note that you may run into problems if you register “Product Type” as custom taxonomy because that’s WooCommerce’s default hidden taxonomy).

    To guide you, I registered custom taxonomy “Type” on my local using below code. I placed this code in my theme’s functions.php file (you may not need this if you have already registered custom taxonomy):

    add_action( 'init', 'pn_create_product_type_tax' );
    
    function pn_create_product_type_tax() {
    	register_taxonomy(
    		'type',
    		'product',
    		array(
    			'label' => __( 'Product Type' ),
    			'rewrite' => array( 'slug' => 'type' ),
    			'hierarchical' => true,
    		)
    	);
    }
    

    Then I assigned “Street” category to some of the products and “T Shirt” category from the “Type” custom taxonomy. Then I created the shortcode using following code which I placed in my theme’s functions.php file:

    add_shortcode('best_sellers', 'pn_show_best_sellers');
    
    function pn_show_best_sellers( $atts ) {
        $atts = shortcode_atts( array(
          'columns' => '4',
          'orderby' => 'title',
          'order'   => 'asc',
        ), $atts );
    
        $query_args = array(
          'post_type'           => 'product',
          'post_status'         => 'publish',
          'ignore_sticky_posts' => 1,
          'orderby'             => $atts['orderby'],
          'order'               => $atts['order'],
          'posts_per_page'      => 12,
          'product_cat'         => 'street', /* you may need to change this as per your category slug */
          'type'                => 't-shirt' /* you may need to change this as per your category slug */
        );
    
        $products = new WP_Query($query_args);
    
    		$columns = $atts['columns'] ;
    		$woocommerce_loop['columns'] = $columns;
    
    		ob_start();
    
    		if ( $products->have_posts() ) {
    			?>
    
    			<?php woocommerce_product_loop_start(); ?>
    
    				<?php while ( $products->have_posts() ) : $products->the_post(); ?>
    
    					<?php wc_get_template_part( 'content', 'product' ); ?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			<?php woocommerce_product_loop_end(); ?>
    
    			<?php
    		} else {
          echo __( 'No products found' );
        }
    
    		woocommerce_reset_loop();
    		wp_reset_postdata();
    
    		return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
    }

    I created simple page named “Best Seller Shirts” and placed the shortcode like below:

    [best_sellers]

    On front end it listed only those products which had both “Street” and “T Shirt” category assigned.

    I hope this helps.

    • This reply was modified 5 years, 4 months ago by Prasad Nevase. Reason: code formatting and comments
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show products which are in 2 different taxonomies’ is closed to new replies.