woocommerce custom Related product code
-
I’m using the related-product.php code in my custom single-product.php to output related products but it won’t function properly if the product doesn’t have any product in the same category, it causes a page error (page html code just stops at the point it should start the related product details).
Any thoughts would be greatly appreciated
<?php global $product, $woocommerce_loop; $related = $product->get_related(); if ( sizeof( $related ) == 0 ) return; $args = apply_filters('woocommerce_related_products_args', array( 'post_type' => 'product', 'ignore_sticky_posts' => 1, 'no_found_rows' => 1, 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'post__in' => $related, 'post__not_in' => array($product->id) ) ); $products = new WP_Query( $args ); $woocommerce_loop['columns'] = $columns; if ( $products->have_posts() || sizeof( $related ) > 0 ) : ?> <div class="promo"> <div style="text-align:center;"> <div class="title-holder"><h3 class="title">Related<br /> Products</h3> </div> </div> <div class="related products"> <?php woocommerce_product_loop_start(); ?> <?php while ( $products->have_posts() ) : $products->the_post(); ?> <div style="text-align:center;"> <h2 class="product"><?php the_title(); ?></h2> <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>"> <?php woocommerce_show_product_sale_flash( $post, $product ); ?> <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?> <h3 class="price"><?php echo $product->get_price_html(); ?></h3> </a> <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?> </div> <?php endwhile; // end of the loop. ?> <?php woocommerce_product_loop_end(); ?> </div> </div> <?php endif ?> <?php wp_reset_postdata(); ?>
- The topic ‘woocommerce custom Related product code’ is closed to new replies.