I have defined an attribute in Magento 2. When I say Dont Show in Category in this attribute, I do not want the product to be shown in Category and Search. But I don’t want to completely remove the product’s URL, this is important for me in terms of SEO. Can you help me?
I made the visible property and it did not show the product in the category or search, but when I entered the URL, it gave a 404 error.
2
Answers
You should override class MagentoCatalogModelProductVisibility.
This class provides a backend for managing product visibility and allows you to define conditions for product display in catalogs, search results, and by direct URL.
‘Site’ is labels to show this option on backend
In the backend, you’ll gain a new "Visibility" option called "Site." Products assigned this visibility will be hidden from both category pages and search results. However, customers can still access the product detail page directly using the product URL.
this work for me
To achieve your goal of hiding the product from category and search results while keeping the product accessible via its URL, you can extend Magento’s product visibility options and create a custom visibility type. Here’s how you can do it:
Create a Custom Visibility Option:
MagentoCatalogModelProductVisibility
class to add a custom visibility option.Override the Product Visibility Logic:
Update Dependency Injection:
di.xml
file, use the preference to override theMagentoCatalogModelProductVisibility
class with your custom class.Here is the step-by-step implementation:
di.xml
):This config ensures Magento uses your custom visibility logic.
In your business logic, ensure that when setting the product visibility, the new custom visibility type
VISIBILITY_IN_SITE
is properly utilized as needed.By following these steps, your products will not appear in category or search results but can still be accessed directly via their URL, retaining the SEO benefits.