Latest Product is not display in related product section
-
Hello, we are facing issue in our site, so that in related product section (You may also like…), it is displaying old product, we want to display latest product in related product section.
Is there any code/snippet is available that will display latest product only in related products section?The page I need help with: [log in to see the link]
-
Hello @avinashdigitl,
Thank you for reaching out!
I understand you want to show only the recently added product in the “You may also like…” section, correct?
If you’re using a block-based theme like Twenty Twenty-Three, you can add the Newest Products Block to the single product page template.
Here’s how:- Navigate to Appearance → Editor → All templates
- Locate the Single Product template
- Remove the Related Products block and add the Newest Products Block instead.
Link to image: https://d.pr/i/ifLDFMHope this helps!
Hello we are using classic theme and overriding woocommerce template files into our theme, is there any way for this?
Hello, is there any way, so i can display latest products in Related product section(You may also like…) in classic theme? (Note: template override in parent theme.)
Hi there @avinashdigitl ??
is there any way, so i can display latest products in Related product section(You may also like…) in classic theme? (Note: template override in parent theme.)
Thank you for your patience! We are experiencing an unusually high volume of support requests at the moment, and that has affected our response times.
I understand you’d like to override templates via a theme.
For doing so, feel free to check out our dedicated page on Template structure & Overriding templates via a theme (linked here).
Furthermore, if you are needing help with customizations or troubleshooting WooCommerce and do not have a developer or company you can depend on, then we’ve got recommendations for you.
Visit?https://woocommerce.com/customizations/?to find the right agency or developer for your specific needs.
I trust that points you in the right direction, but if you have more questions, let us know.
We’re happy to help.
Hello, is there any update here?
Are there any hook, action is available which can display latest product in “Cross Sells / You May Also Like” section in single product page?- This reply was modified 1 year, 2 months ago by Avinash Patel.
- This reply was modified 1 year, 2 months ago by Avinash Patel.
To provide some further context, the customer today has got back to us with the following, which sounds more like a bug than a customisation thats needed:
We are in the processing of launching several new designs, so these products should be being included in the auto generated products for the Cross sells/You May Also Like section.
Currently, no products that we have listed since July 17th?2022 appear on this section.
Hi @avinashdigitl ,
Hello, is there any update here?
Are there any hook, action is available which can display latest product in “Cross Sells / You May Also Like” section in single product page?
Please can you clarify what you want an update on, specifically? I can see that our last reply provided links to overriding templates via a theme and a link for any customization requests.
Hi @acourageous ,
Since each site setup differs, and in order to align with?forum best practices, please can you?start your own topic?so that we can address your issue(s) separately.
Cheers!
Hello, here i am sharing code which display related products but for some reasons, it is not displaying latest related product in this section.
Fila path is: theme/woocommerce/single-product/related.php<?php /** * Related Products * * This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php. * * HOWEVER, on occasion WooCommerce will need to update template files and you * (the theme developer) will need to copy the new files to your theme to * maintain compatibility. We try to do this as little as possible, but it does * happen. When this occurs the version of the template file will be bumped and * the readme will list any important changes. * * @see https://docs.woocommerce.com/document/template-structure/ * @package WooCommerce\Templates * @version 3.9.0 */ if (!defined('ABSPATH')) { exit; } global $product; if ($related_products): ?> <section class="related products"> <?php $product_you_may_like_mobile_cta = get_field('product_you_may_like_mobile_cta'); $matching_items_mobile_cta = get_field('matching_items_mobile_cta', 'option'); if ($product_you_may_like_mobile_cta) { $link = $product_you_may_like_mobile_cta; } else { $link = $matching_items_mobile_cta; } $heading = apply_filters('woocommerce_product_related_products_heading', __('You may also like…', 'woocommerce')); if ($heading): ?> <div class="container"> <h2 class="h1"> <?php echo esc_html($heading); ?> </h2> </div> <?php endif; ?> <div class="product-slider-wrapper"> <?php woocommerce_product_loop_start(); ?> <?php foreach ($related_products as $related_product): ?> <?php $post_object = get_post($related_product->get_id()); setup_postdata($GLOBALS['post'] =& $post_object); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited, Squiz.PHP.DisallowMultipleAssignments.Found wc_get_template_part('content-product', 'slide'); ?> <?php endforeach; ?> <?php woocommerce_product_loop_end(); ?> </div> <?php if ($link): $link_url = $link['url']; $link_title = $link['title']; $link_target = $link['target'] ? $link['target'] : '_self'; echo '<div class="cta-btn-wrap text-center"><a class="cta-btn btn" href="' . esc_url($link_url) . '" target="' . esc_attr($link_target) . '">' . esc_html($link_title) . '</a></div>'; endif; ?> </section> <?php endif;
Hello @avinashdigitl,
Thank you for providing additional context. The related products function uses tags and categories of a given product to generate the related products. By default, it orders randomly. To override this behavior and order by date, you may use this custom function:
add_filter( 'woocommerce_output_related_products_args', 'wc_child_related_product_args' ); function wc_child_related_product_args( $args ) { $args['orderby'] = 'date'; return $args; }
If you’d like to replace related products with recent products, then you may replace the
woocommerce_output_related_products
function withWC_Shortcodes::recent_products
like this:add_action( 'woocommerce_init', 'wc_child_replace_related_with_recent', 20 ); function wc_child_replace_related_with_recent() { remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); add_action( 'woocommerce_after_single_product_summary', 'wc_output_recent_products', 20 ); } function wc_output_recent_products() { echo WC_Shortcodes::recent_products( array( 'limit' => 4) ); }
Please note the above code is for guidance purpose only and it works with the Storefront theme and other classic template themes.
Hope this helps.
Thank you for response, i am working on above fix and will let you know here.
- The topic ‘Latest Product is not display in related product section’ is closed to new replies.