• I have a loop on my product-archive.php template file that displays products by category.

    However, because my shop page uses archive-product.php template file and post_type=product, the custom category loop doesn’t work on the shop page specifically. How can I have multiple loops on archive.php dependent on different conditions?

    I haven’t found the same question here yet. Based on this documentation, I’m using is_shop for the shop page loop, and is_product_category() for the category loop.

    This is my archive-product.php template file but it results in the shop page loop not displaying any products:

    <?php
    
    if ( woocommerce_product_loop() ) {
    
        /**
         * Hook: woocommerce_before_shop_loop.
         *
         * @hooked woocommerce_output_all_notices - 10
         * @hooked woocommerce_result_count - 20
         * @hooked woocommerce_catalog_ordering - 30
         */
        do_action( 'woocommerce_before_shop_loop' ); ?>
    
        <ul class="wpf-search-container products">
        <li class="product">
        <?php
    
    // Get The queried object ( a WP_Term or a WP_Post Object)
    $term = get_queried_object();
    
    // To be sure that is a WP_Term Object to avoid errors
    if( is_a($term, 'WP_Term') ) :
    
    if ( is_product_category() ) :
    // Setup your custom query
    $loop = new WP_Query( array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'tax_query'      => array( array(
            'taxonomy' => 'product_cat', // The taxonomy name
            'field'    => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
            'terms'    => $term->term_id, // can be an integer, a string or an array
        ) ),
    ) );
    
    if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();
    the_post_thumbnail( 'thumbnail');
    endwhile;
    wp_reset_postdata(); // Remember to reset
    endif; endif;
        ?>
            
    <?php
        
    if ( is_shop() ) :
    // Setup your custom query
    $loop = new WP_Query( array(
        'post_type'      => 'product',
        'posts_per_page' => -1,
        'post_status'    => 'publish',
    ) );
    
    if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();
    the_post_thumbnail( 'thumbnail');
    endwhile;
    wp_reset_postdata(); // Remember to reset
    endif; endif; endif;
    ?>
            </li>
    </ul>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Isn’t the shop page a named page under “Pages”? Then the queried object would be a post object, not a product_cat term, so your ‘is a term’ check could be failing the entire process.

    Be leery of nested if(): endif; blocks. IME they don’t always work as expected. I suggest demarcating the inner blocks with curly braces instead to ensure there is not unexpected behavior. I’m not saying nested endifs are wrong per se, I just don’t trust them.

    Be sure it’s really your template being used for the shop page. I’d expect one of the page templates be used instead of an archive template. That said, plugins can override the template used and I’m not that well versed with WooCommerce.

    If you still have trouble, ensure each of your conditionals are working as expected. Insert else conditions and always output something unequivocal in each case so you always get feedback about what’s happening even if the queries fail to do anything. You can remove these or comment them out later once it’s all working.

    If the queries themselves are failing (they shouldn’t if execution is able to get to them), output the query object’s request property (the SQL query) and examine it for reasons the query would fail. There’s invariably something unexpected here when queries fail to work as expected.

Viewing 1 replies (of 1 total)
  • The topic ‘two loops based on different conditions’ is closed to new replies.