1/**
2 * @snippet Get Related Products by Same Sub Category with no Repeate Same Product @ WooCommerce Single
3 * @how-to Get CustomizeWoo.com FREE
4 * @author Rodolfo Melogli
5 * @compatible WooCommerce 3.8
6 * @donate $9 https://businessbloomer.com/bloomer-armada/
7 */
8
9add_filter('woocommerce_related_products', 'add_related_products');
10function add_related_products($related_product_ids)
11{
12 global $post;
13
14// get the cats this product is in
15 $terms = get_the_terms($post->ID, 'product_cat');
16
17// if there is only one category jump out.
18 if (count($terms) === 1) {
19 return $args;
20 }
21
22$cats = array();
23// remove anything that is a parent cat
24 foreach ($terms as $k => $term) {
25 if ($term->parent === 0) {
26 unset($terms[$k]);
27 } else {
28 // build list of terms we do want (children)
29 $cats[] = $term->term_id;
30 }
31 }
32
33 $post_ids = get_posts(array(
34 'post_type' => 'product',
35 'numberposts' => -1, // get all posts.
36 'exclude' => $post->ID,
37 'tax_query' => array(
38 array(
39 'taxonomy' => 'product_cat',
40 'field' => 'term_id',
41 'terms' => $cats,
42 ),
43// Remove current product
44 array(
45 'taxonomy' => 'product_cat',
46 'field' => 'term_id',
47 'terms' => $post->ID,
48 'operator' => 'NOT IN',
49 ),
50 ),
51 'fields' => 'ids', // Only get post IDs
52 ));
53
54 return $post_ids;
55}
56
1<?php
2/**
3 * Related Products
4 *
5 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.
6 *
7 * HOWEVER, on occasion WooCommerce will need to update template files and you
8 * (the theme developer) will need to copy the new files to your theme to
9 * maintain compatibility. We try to do this as little as possible, but it does
10 * happen. When this occurs the version of the template file will be bumped and
11 * the readme will list any important changes.
12 *
13 * @see https://docs.woocommerce.com/document/template-structure/
14 * @author WooThemes
15 * @package WooCommerce/Templates
16 * @version 3.9.0
17 */
18
19if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21}
22
23global $product, $woocommerce_loop;
24
25if ( empty( $product ) || ! $product->exists() ) {
26 return;
27}
28
29if ( ! $related = $product->get_related( $posts_per_page ) ) {
30 return;
31}
32
33// Get ID of current product, to exclude it from the related products query
34$current_product_id = $product->get_id();
35
36$cats_array = array(0);
37
38// get categories
39$terms = wp_get_post_terms( $product->id, 'product_cat' );
40
41// select only the category which doesn't have any children
42foreach ( $terms as $term ) {
43 $children = get_term_children( $term->term_id, 'product_cat' );
44 if ( !sizeof( $children ) )
45 $cats_array[] = $term->term_id;
46}
47
48$args = apply_filters( 'woocommerce_related_products_args', array(
49 'post_type' => 'product',
50 'post__not_in' => array( $current_product_id ), // exclude current product
51 'ignore_sticky_posts' => 1,
52 'no_found_rows' => 1,
53 'posts_per_page' => $posts_per_page,
54 'orderby' => $orderby,
55 'tax_query' => array(
56 array(
57 'taxonomy' => 'product_cat',
58 'field' => 'id',
59 'terms' => $cats_array
60 ),
61 )
62));
63
64$products = new WP_Query( $args );
65$woocommerce_loop['name'] = 'related';
66$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
67
68if ( $products->have_posts() ) : ?>
69
70 <section class="related products">
71
72 <?php
73 $heading = apply_filters( 'woocommerce_product_related_products_heading', __( 'Related products', 'woocommerce' ) );
74
75 if ( $heading ) :
76 ?>
77 <h2><?php echo esc_html( $heading ); ?></h2>
78 <?php endif; ?>
79
80 <?php woocommerce_product_loop_start(); ?>
81
82 <?php while ( $products->have_posts() ) : $products->the_post(); ?>
83
84 <?php wc_get_template_part( 'content', 'product' ); ?>
85
86 <?php endwhile; // end of the loop. ?>
87
88 <?php woocommerce_product_loop_end(); ?>
89
90 </section>
91
92<?php endif;
93
94wp_reset_postdata();