Woocommerce related product from primary category

#282672
  • My site is built on Woocommerce & I am using Rank math’s Breadcrumb for Showing the Primary category of the Product.

    But at the Bottom side Related Product tab Shows Product from the Main category But,

    I want Woocommerce related product from primary category only So that primary category product show in Related tab instead of the whole category.

    I try to find the solution on Google but I did not find any solution.
    Yoast SEO users have a solution to this issue.

    Can you please tell me or give me the Function code or solution of this Problem Please.

    My website : https://anayadesignerstudio.com/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Anas
    Rank Math business

    Hello,

    Thank you for contacting Rank Math.

    Are you are referring to the Related Products shown on the frontend?
    frontend

    If yes, then these products are added by the theme or any plugin.

    If you have these options in the theme or if you are using any plugin to add the related product, you will have to change the settings from within that plugin/theme.

    If you are referring to some other option, please share a screenshot so we can advise accordingly.

    We look forward to helping you.

    I think you are talking about Adding Upsell & cross-sell Right?

    I have that Feature in my Theme but Manually adding Upsell takes too much time. that I don’t want.

    that’s why I Just want Related product pick from the Primary category only.

    By default woocommerce shows any product for showing in the related product tab. (Which is below single product page, as you have share screenshot)

    Let me share the Yoast SEO code that they have shared with their user to this problem. I think it’s function.php code. Please check Here’s the link.

    Link – https://wpexplorer-themes.com/total/snippets/yoast-seo-primary-category-related-products/

    Link 2 – https://www.dhirenpatel.me/related-products-using-yoast/

    Hello,

    Please use the code given below in your theme’s functions.php file to display related products from the primary category:

    function rank_math_get_primary_term_id( $taxonomy = 'category', $post = null ) { 
                $post = get_post( $post ); 
             
                $primary_term = get_post_meta(get_queried_object_id(), 'rank_math_primary_product_cat', true);
                return $primary_term;
            } 	
    add_filter( 'woocommerce_get_related_product_cat_terms', function( $terms, $product_id ) {
        if ( function_exists( 'rank_math_get_primary_term_id' ) ) {
            $primary_term_product_id = rank_math_get_primary_term_id( 'product_cat', $product_id );
            if ( $primary_term_product_id ) {
                return array( $primary_term_product_id );
            }
        }
        return $terms;
    }, 10, 2 );

    Hope this helps. Let us know if you need any other assistance.

    Thanks.

    Ok I have just Pasted this Code in Function.php file.

    I think I need to Wait because Woocomerce has 10 hours of cache to Related product….

    I will update you Tommorrow.

    Hello,

    I have checked the filter and it worked fine on my end. Let us know how that goes.

    Thanks.

    Yes sir all working fine.

    But,

    Is there any way to remove Out of stock products? Because out of stock products also showing up which I do not want.

    Can you please update this code?

    https://ibb.co/hXbP93h

    Hello,

    We have enabled the option to remove out of stock products and they should no longer be added to the related products list:
    Out of stock

    We tested a couple of dresses and didn’t see any out-of-stock products.

    Hope this helps solve your issues.

    Don’t hesitate to get in touch if you have any other questions.

    Thank you so much. It’s working fine.

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

Viewing 9 replies - 1 through 9 (of 9 total)

The ticket ‘Woocommerce related product from primary category’ is closed to new replies.