display product from same categories related products woocommerce

Solutions on MaxInterview for display product from same categories related products woocommerce by the best coders in the world

showing results for - "display product from same categories related products woocommerce"
Kevin
21 Jan 2020
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
Isaac
16 May 2018
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();
queries leading to this page
display related products in woocommercewoocommerce product relateddisplay different product category in woocommereceshow products belonging two categories woocommercehow to show a product category in single products in woocommerceget all product in two category woocommercewoocommerce related products not based on categorywoocommerce display product categories in menuassign a product to all categories woocommercedisplay woocommerce product categories in wordpresswoocommerce product categories in menudisplay product from same categories related products woocommerceshow single product categories woocommercehow to display related products in woocommerceshow products from two category woocommercedifferences between categories and tags in woocommercewoocommerce related products same categorywoocommerce display product categories and subcategories queryhow to show all product in single product page woocommerce instead of realted productget product categories with sub category woocommercedisplay all product categories woocommerceget related products woocommerce all categoriesdispaly product categories on side woocommercewoocommerce change multiple products categorydisplay related product by category woocommercewoocommerce product with optionswoocommerce get related products by same sub category with no repeate same producthow to show product categories wordpress woocommercehow to show 12 products in related product in woocommerceget related product category woocommerceget all product categories woocommercewoocommerce detail product related productshow all product in woocommerce single product page pluginshow related products in woocommerce product pagewoocommerce related product by category phpwoocommerce product categories showing other productsshow woocommerce product categorieswoocommerce show product categories in menuhow to show the related product on single product page woocommercewoocommerce show product categorieshow to get product categories in woocommercedisplay product with category id woocommerceshow categories of a single product woocommercehow to display woocommerce product categories in menuwoocommerce is product category 28 29 allhow to display all woocommerce product categorydisplay product description in product list in woocommercehow to display particular category product in woocommerceadd different product categories to menu woocommercehow to show woocommerce product categories in product listhow to show product with category in woocommercehow to display product from one category in woocommerceshow product categories woocommercecan i put some product in two categories in woocommerceshow products from two categories woocommercehow to get all product category in woocommerceshow product and categories woocommercedisplay product from same categories related products woocommerce