Skip to content

Instantly share code, notes, and snippets.

@deadlyhifi
Last active June 23, 2022 07:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save deadlyhifi/5b7115d79e610f125a3c to your computer and use it in GitHub Desktop.
Save deadlyhifi/5b7115d79e610f125a3c to your computer and use it in GitHub Desktop.
Have WooCommerce use only the subcategory a product is in for related products (not parent category)
@S4Mi
Copy link

S4Mi commented May 22, 2018

Thanks!

@anhdq1801
Copy link

I tried this code with WooCommerce 3.4.3 it doén't show related product.

@midi9x
Copy link

midi9x commented Mar 8, 2019

Try using woocommerce_output_related_products_args hook, it working :D

@iake88
Copy link

iake88 commented Mar 13, 2019

Hi! What does it mean? Can you correct the code?

@mikecalland
Copy link

How should people use this code? there are no instructions where to place it.

@deadlyhifi
Copy link
Author

deadlyhifi commented Aug 20, 2020

@mikecalland, you can put this code in your functions.php file -https://developer.wordpress.org/themes/basics/theme-functions/. This code snippet is quite old so I’m not sure if it still works as expected.

@Anna-Webdesign
Copy link

Tried it but it is not working :(

@deadlyhifi
Copy link
Author

Tried it but it is not working :(

Sorry, this was done a few years ago and things might have changed. I’m not working on any woocom sites so can’t really check it out.

@m16averick
Copy link

m16averick commented Dec 18, 2020

Here's my solution for newer WooCommerce, add this to functions.php and include this filter in post query in carousel or somewhere :p merry xmas!

/**
 * GET RELATED PRODUCTS FROM DIRECT CATEGORY
 */
add_filter( 'woocommerce_product_related_posts', 'woocommerce_get_direct_related_products' );
function woocommerce_get_direct_related_products($args) {
    global $woocommerce, $product;

    // Related products are found from category
    $cats_array = array(0);

    // Get categories
    $terms = wp_get_post_terms( $product->id, 'product_cat' );

    //Select only the category which doesn't have any children
    foreach ( $terms as $term ) {
        $children = get_term_children( $term->term_id, 'product_cat' );
        if ( !sizeof( $children ) )
            $cats_array[] = $term->term_id;
    }

    // Don't bother if none are set
    if ( sizeof( $cats_array ) == 1 ) return $args;


    // Meta query
    $meta_query = array();
    $meta_query[] = $woocommerce->query->visibility_meta_query();
    $meta_query[] = $woocommerce->query->stock_status_meta_query();

    $limit = -1;

    $post_ids = get_posts(array(
        'orderby'       => 'rand',
        'posts_per_page'=> $limit,
        'post_type'     => 'product',
        'fields'        => 'ids',
        'meta_query'    => $meta_query,
        'post__not_in' => array( $product->get_id() ),  //this is the way i remove current prod xd
        'tax_query' => array(
            array(
                'taxonomy'  => 'product_cat',
                'field'     => 'id',
                'terms'     => $cats_array
            ),
        ),
        'fields' => 'ids', // Only get post IDs
    ));

    // Alter the query
    $args['post__in'] = $post_ids;

    return $args;

}

@deadlyhifi
Copy link
Author

Thanks @m16averick.

@jodzeee
Copy link

jodzeee commented Dec 20, 2020

@m16averick - It's not working for me, but I have hierarchal categories. Can it be made to work that way or with tags?

@Maurizio288
Copy link

I tested the code on my site.
I see no effect on how it works in related products. Probably because all the products also have the "Uncategorized" category. Should the code be changed to remove this category from the search for related products?

I'm using the DIVI theme, I don't wish it was a DIVI problem. What do you think?

Thanks

@itsprabhucbe
Copy link

I find this plugin which is suitable for our requirement - Related Products for WooCommerce by WebToffee,
After installing the plugin you can able to display products based on category and tags. If you want to display products based on sub-category follow this tutorial. https://www.webtoffee.com/related-products-woocommerce-user-guide/#sub_category

@ngoduyangit
Copy link

I find this plugin which is suitable for our requirement - Related Products for WooCommerce by WebToffee, After installing the plugin you can able to display products based on category and tags. If you want to display products based on sub-category follow this tutorial. https://www.webtoffee.com/related-products-woocommerce-user-guide/#sub_category

Thanks alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment