Louis
Louis

Reputation: 2608

WordPress loop on all products

I am trying to loop with a while over every products in my shop. This is to modify the "related products" feature. But it runs only 6 times, whereas I have 49 products.

Why is this the case?

if ( is_singular('product') ) {
    global $post;
    // get categories
    $terms = wp_get_post_terms( $post->ID, 'product_cat' );

    $title = get_the_title($post);


    $pattern = '/[^a-zA-Z0-9 ]/';
    // $pattern = '/ /';
    $matches = array();
    $dummy = preg_match($pattern, $title, $matches);
    $posi = strpos($title, $matches[0]);
    // print_r($posi);
    $productname = substr($title, 0, $posi-1);
    // print_r($productname);
    $productcolor = substr($title, $posi+3);
    // print_r($productcolor);



    foreach ( $terms as $term ) $cats_array[] = $term->term_id;
    $query_args = array( 'orderby' => 'title',
                         'post__not_in' => array( $post->ID ),
                         'posts_per_page' => 200,
                         'no_found_rows' => 10,
                         'post_status' => 'publish',
                         'post_type' => 'product',
                         'tax_query' => array(
                                                array(
                                                    'taxonomy' => 'product_cat',
                                                    'field' => 'id',
                                                    'terms' => $cats_array
                                                )
                                            )
                        );
    $r = new WP_Query($query_args);


    if ($r->have_posts()) { ?>

        <div class="related products">
            <h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>

            <?php woocommerce_product_loop_start(); ?>

                <?php 
                    $counter = 0;
                    while ($r->have_posts()) : 
                        $r->the_post(); 
                        global $product; 
                        $counter = $counter + 1;
                    

                        wc_get_template_part( 'content', 'product' ); 


                    endwhile; // end of the loop. 
                    print_r($counter)
                ?>

            <?php woocommerce_product_loop_end(); ?>

        </div>

        <?php

        wp_reset_query();
    }
}

Upvotes: 0

Views: 393

Answers (1)

Daniel Fonda
Daniel Fonda

Reputation: 111

Not sure if it applies, but I recommend just modifying the related products template, which can be found here: https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/related.php

To do this:

  1. Create a "wooocommerce" folder inside your theme root.
  2. Copy over the related.php file from /wp-content/plugins/woocommerce/woocommerce/templates/single-product/related.php
  3. The location of the new file should be /wp-content/your-theme/wooocmmerce/single-product/related.php
  4. Make the desired modifications to said file.

Again, it completely depends on what you're trying to achieve. I'd be more than happy to edit your code, but would need to know what you wish to modify, so to not remove anything important.

Upvotes: 1

Related Questions