Show related products by same category - by Divites

This topic has 8 replies, 2 voices, and was last updated 6 years ago ago by Rose Tyler

  • Avatar: Divites
    Divites
    Participant
    March 25, 2018 at 11:19

    Hi,

    I guess it’s WooCommerce stuff, but they don’t respond to my question. What I would like to achieve is that only the related products of the same (sub)category are shown on a single product page. Now it takes the mother category which is not specific. I know you can set upsell or cross sell, but if possible I would like to show the related products in the same (subcategory) by using code in related.php or function.

    Really hope you can help me out.

    Thx for your prompt reply.

    Regards,

    Johan

    7 Answers
    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    March 26, 2018 at 12:06

    Hello,

    You can copy related.php (xstore/woocommerce/single-product) to xstore-child/woocommerce/single-product and make such changes https://prnt.sc/iwgubc:

    $cats_array = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'ids'));
    $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->get_id() ),
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $cats_array
    		)
    	)
    ) );

    Regards

    Avatar: Divites
    Divites
    Participant
    March 27, 2018 at 13:49

    Hi Rose,

    Thx for your great support. I have made the changes, but unfortunately cannot see the effect. For example: when viewing this product http://rando.divi-test.nl/product/elcon-verticale-platenzaag-advance-quadra/ I would like to show in the related products only product from the same subcategory “verticale platenzagen”.

    Can you please help me achieve that?

    Thx again.

    Regards,

    Johan

    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    March 27, 2018 at 13:56

    Hello,

    Please provide temporary wp-admin and FTP access in Private Content.

    Regards

    Avatar: Divites
    Divites
    Participant
    March 29, 2018 at 14:26

    Hi Rose,

    Please find the login credentials in the private section of this message.

    Really hope you can help me out.

    Thx so much.
    Regards,

    Johan

    Please, contact administrator
    for this information.
    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    March 29, 2018 at 15:46

    Hello,

    Unfortunately, we can’t connect to your server via FTP.
    But please change the previous code to https://prnt.sc/iy2jak

    $cats_array = wp_get_post_terms($product->get_id(),'product_cat',array('fields'=>'ids'));
    $sub_cats = array ();
    for ( $i =0; $i < count($cats_array); $i++ ) {
    	$args = array(
           'hierarchical' => 1,
           'show_option_none' => '',
           'hide_empty' => 0,
           'parent' => $cats_array[$i],
           'taxonomy' => 'product_cat'
        );
        foreach (get_categories($args) as $key) {
        	$sub_cats[] = $key->term_id;
        }
    }
    $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->get_id() ),
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $sub_cats
    		)
    	)
    ) );
    

    Regards

    Avatar: Divites
    Divites
    Participant
    March 29, 2018 at 20:09

    Hi,

    Thx for your prompt reply and custom code.
    I have added the code the related.php and I notice some difference but unfortunately, its not working quite the way my client wants it to. For example they want to show below the single product in the subcategory “aanvoerapparaten” only other products from the same subcategory.Now it looks like this http://rando.divi-test.nl/product/steff-2048/. As you can see other machines of different subcategories are listed as well.

    Has it something to do with the permalink structure?

    The FTP should work. Have you tried to set it to insecure connection?

    Really hope you can help me out again.

    Thx for your kind assistance.

    Regards,

    Johan

    Avatar: Rose Tyler
    Rose Tyler
    Support staff
    March 30, 2018 at 08:51

    Hello,

    You can contact WP Kraken team if you need help with this because customization is out the scope of our support.

    Regards

  • Viewing 8 results - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.