0

I am new to wp dev and trying to figure out how to replace or change the query woocommerce makes that shows all my products on any archive page, or page that shows my list of products, not sure what it is called but the page that shows the pic, name, price & link to product page. I have multiple categories so i guess i would need to add the code to show the different categories depending on which cat page they are on? Unless i am looking it to this too deeply.

I am trying to make an advanced loop that shows me groups of products based on a few things. First i want to see all the featured products, then i want to see the products that are not older than 6 months old, then everything else in menu order then title order ascending.

To make it even more complicated i don't want any duplicates. I think my code is working but I am not sure exactly where or how to place it so woo takes off with it and shows my results. Below is the code i am using, I am placing it in the woocommerce.php page in my child theme and it doesn't show me anything unless i echo or print_r something. So I know I am missing something but being new I don't know what I need to search for to include to see the results.

My code: (woocommerce.php)

<?php

if ( is_shop() || is_product_category() || is_product_tag() ) { // Only run on shop archive pages, not single products or other pages
    // Products per page
    $per_page = 12;
    if ( get_query_var( 'taxonomy' ) ) { // If on a product taxonomy archive (category or tag)   

        //First loop 
      $args = array(
            'post_type' => 'product',
            'orderby'  => array( 'meta_value' => 'DESC' ),
            'meta_key' => '_featured',
            'posts_per_page' => $per_page,
            'paged' => get_query_var( 'paged' ),
        );    
      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;
      rewind_posts();

      //Second loop    
      $args = array( 
          'post_type' => 'product',
          'orderby' => 'date',
          'order' => 'DESC',
          'date_query' => array(
          array(
              'before' => '6 months ago',
              ),
          ), 
            'post__not_in' => $do_not_duplicate
      );
      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;
      rewind_posts();

      //Third loop 
      $args = array(
          'post_type' => 'product',
          'orderby'  => array( 
              'menu_order' => 'ASC', 
              'title' => 'ASC',
              'post__not_in' => $do_not_duplicate
          ), 

        );  

      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;              

    } else { // On main shop page
        $args = array(
            'post_type' => 'product',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => $per_page,
            'paged' => get_query_var( 'paged' ),
        ); 

    }

    // Set the query
    $products = new WP_Query( $args );

    // Standard loop
    if ( $products->have_posts() ) :
        while ( $products->have_posts() ) : $products->the_post();  
        endwhile;
        wp_reset_postdata();
    endif;

} else { // If not on archive page (cart, checkout, etc), do normal operations
    woocommerce_content(); 
}

Any help to steer me in the right direction would be great help!

Update

I forgot to add 'post_type' => 'product'in my args (updated it just now), but now that I can see my titles when I add the_title(); in my while loop, how do I tell it instead to spit it all out like it would for any archive page(call a template?), also how do I make it more generic so depending on which category/archive page I am on it will just show products for that category? Thanks again!

3 Answers 3

0
    // Standard loop
if ( $products->have_posts() ) :
    while ( $products->have_posts() ) : $products->the_post();  
    endwhile;
    wp_reset_postdata();
endif;

This literally shows no data. Try adding at least the_title(); in there.

1
  • Still nothing. Completely blank. Oct 12, 2016 at 16:38
0

The page you are referring to is archive-product.php which is basically shop page in woocommerece and talking about the query in woocommerece i will give u a link hope that can solve your issue regarding this woocommerece loop

0

Don't rewrite Woocommerce template files unless you have no choice. Use filters if you have to or shortcodes if it's simple. The things you're trying to do can be done with just shortcodes using the documentation I linked to above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.