Removing/ Hiding Products in Magento Category and Search Result Pages

Table of Contents

  1. Introduction
  2. Understanding Product Visibility in Magento
  3. Step-by-Step Guide to Hide Products
  4. Ensuring Product Accessibility via Direct SKU Search
  5. Conclusion
  6. FAQ

Introduction

Ever found yourself needing to manage product visibility effectively in your Magento store? Perhaps you have certain products that you wish to exclude from category and search result pages but still want them accessible via direct SKU searches. Understanding how to refine product visibility in Magento is crucial for maintaining an organized and user-friendly online store.

In this comprehensive guide, we'll delve into how to utilize a Magento attribute to control the visibility of products on category and search result pages. By the end of this article, you'll be equipped with the knowledge to implement this feature seamlessly, enhancing both your site's performance and user experience.

Understanding Product Visibility in Magento

Importance of Product Visibility

Product visibility is a fundamental aspect of any e-commerce platform, impacting how users interact with the store and find what they are looking for. Proper management of visibility settings ensures that customers are not overwhelmed with irrelevant options, thereby improving search accuracy and overall shopping experience.

Setting up Custom Attributes

In Magento, custom attributes can be created to manage product visibility. These attributes can control various aspects of a product's display settings based on criteria such as stock status, category relevance, and promotional phases.

Step-by-Step Guide to Hide Products

Creating the "Discontinued" Attribute

To start, you need to create a custom attribute called "discontinued" that is of the Yes/No type. This attribute will be used to mark the products that should be hidden from the category and search result pages.

  1. Navigate to Magento Admin Panel: Log in to your Magento admin panel.
  2. Create Attribute: Go to Stores > Attributes > Product. Click on Add New Attribute.
  3. Configure Attribute Settings: Fill in the necessary details:
    • Attribute Code: discontinued.
    • Input Type: Yes/No.
    • Set other settings as required (e.g., Manageable in the admin grid).

Assigning the Attribute to Products

Once the attribute is created, it needs to be assigned to products.

  1. Go to Products: Navigate to Catalog > Products.
  2. Edit Product Details: Edit the products you wish to hide and set the "discontinued" attribute to Yes.

Modifying Category and Search Result Display Logic

Now, let's modify the template and logic to exclude the products marked as “discontinued”:

  1. Access Theme Files: On your server, locate your Magento theme files, typically found under app/design/frontend.
  2. Edit Category Template: Open the category product listing template file, which is usually located at Magento_Catalog/templates/product/list.phtml.
  3. Modify the Query: Adapt the query to exclude products where the “discontinued” attribute is set to Yes.
<?php
$_productCollection = $block->getLoadedProductCollection();
$_productCollection->addAttributeToFilter('discontinued', ['neq' => 1]);
?>

Updating the Search Result Page

Similarly, adjust the search results page to hide discontinued products.

  1. Access Search Templates: Navigate to Magento_Search/templates.
  2. Edit Search Result Template: In the search result template, usually found in a similar path like category templates, ensure to modify the query to filter out the discontinued products.
<?php
$searchCollection = $searchBlock->getLoadedProductCollection();
$searchCollection->addAttributeToFilter('discontinued', ['neq' => 1]);
?>

Ensuring Product Accessibility via Direct SKU Search

While ensuring that discontinued products are removed from the general listing and search results, it's important that these products remain accessible when searched via SKU directly.

Direct SKU Search Logic

  1. Adjust Search Settings: Navigate to Magento Admin and ensure the search settings are configured to direct to the product page if only one result is found.
  2. Special SKU Handling: Enhance the logic to handle SKU-based searches specifically. This involves configuring search settings to always show the product page if the search term matches a SKU exactly.
$searchTerm = $this->getRequest()->getParam('q');
if (is_sku($searchTerm)) {
    $product = $productRepository->get($searchTerm);
    if ($product && $product->getId()) {
        $this->_redirect($product->getProductUrl());
        return;
    }
}

Here, is_sku() is a hypothetical function that checks whether the search term is an exact SKU match.

Conclusion

Effectively managing product visibility in Magento is vital for optimizing both the store's functionality and user experience. By setting up a "discontinued" attribute and adjusting your theme's logic, you can ensure that discontinued products are hidden from category and search result listings while still being accessible via direct SKU search.

FAQ

How can I ensure that the changes won't impact my live store immediately?

It's always recommended to test changes in a staging environment before deploying them to your live store. This helps in identifying any potential issues without affecting your active customers.

Can I automate the process of hiding discontinued products?

Yes, you can automate the process using cron jobs or custom scripts that update the “discontinued” attribute based on certain conditions like inventory levels, time periods, etc.

Will hiding products affect SEO?

When a product is hidden, it won't appear in category pages or search results but will still be indexed if directly linked. Using noindex meta tags on discontinued product pages can help manage their visibility in search engines.

What if I need to revert the products back to being visible?

Simply change the “discontinued” attribute back to No for the products, and they will reappear in the category and search results as before.

Implementing these strategies ensures that your Magento store remains user-friendly, responsive, and organized, providing a seamless shopping experience.