How to display compare products,recently viewed/compared products in category and product page only?

How to display compare products,recently viewed/compared products in category and product page only?

October 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to display compare products,recently viewed/compared products in category and product page only?

Displaying Compare/Recent Products Blocks Only on Category and Product Pages

As an experienced Laravel developer, I understand the importance of optimizing your e-commerce store’s layout to provide the best user experience. In this post, we’ll guide you through displaying ‘Compare products’ and ‘Recently compared’ blocks only on your category and product pages while hiding them from all other sections in a modern theme.

Why Limit Display to Specific Pages?

Displaying these blocks exclusively on category and product pages serves multiple purposes:

  1. Relevance: Users are more likely to compare or review recently visited products within the context of browsing categories or individual items.
  2. Clutter reduction: Keeping your homepage, cart page, and checkout process clean reduces distractions for users.
  3. Performance: Loading fewer blocks improves page load times.

Step-by-Step Guide

1. Identify the Blocks in Your Theme

First, locate the relevant PHP files or Blade templates where these comparison blocks are rendered. Common locations include:

  • resources/views/layouts/app.blade.php (check for conditional statements)
  • resources/views/category.blade.php
  • resources/views/product.blade.php

2. Remove Unwanted Display Locations

In your layout file(s), look for any instances of the comparison blocks being rendered globally. You’ll likely find something like:

@if(count($recently_compared) > 0)
    <!-- Recently compared products block -->
@endif

@if(count($compare_products) > 0)
    <!-- Compare products block -->
@endif

Comment these sections out or remove them entirely from the layout file(s). This will prevent the blocks from appearing on any page where they aren’t specifically called.

3. Call Blocks Conditionally in Category and Product Pages

Now, let’s add conditional statements to display these blocks only when necessary:

In your category template (category.blade.php):

@if(count($recently_compared) > 0 || count($compare_products) > 0)
    <!-- Recently compared products block -->
    @include('comparison_blocks.recent')
    <!-- Compare products block -->
    @include('comparison_blocks.compare')
@endif

In your product template (product.blade.php):

@if(count($recently_compared) > 0 || count($compare_products) > 0)
    <!-- Recently compared products and compare blocks for this specific product -->
    @include('comparison_blocks.recent_product')
    @include('comparison_blocks.compare_product')
@endif

Ensure that you have the appropriate Blade templates for each block in your resources/views/comparison_blocks directory.

4. Optimize Your Comparison Logic

To ensure your comparison features function smoothly, consider using Laravel’s built-in caching and session handling:

  • Use Laravel’s Cache facade to store recently compared items.
  • Utilize sessions to maintain user-specific compare lists across different pages.

For more advanced customization options, explore Laravel Company’s suite of e-commerce solutions that offer robust, optimized, and highly customizable comparison features: Laravel Company E-commerce Solutions.

Conclusion

By following this guide, you’ve successfully restricted the display of ‘Compare products’ and ‘Recently compared’ blocks to only your category and product pages. This targeted approach enhances user experience, reduces layout clutter, and improves overall site performance.

Remember that customizing your theme’s behavior often requires a solid understanding of Laravel’s Blade templating system and conditional statements. If you encounter any challenges or need further assistance with your e-commerce store’s development, don’t hesitate to explore the extensive resources available at Laravel Company.

Happy coding!

Checkout Catalog Performance PHP

Our network