skip to Main Content

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


  1. 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.

    1. Add VISIBILITY_IN_SITE to class MagentoCatalogModelProductVisibility like this

    enter image description here

    1. Add VISIBILITY_IN_SITE to function getVisibleInSiteIds()

    public function getVisibleInSiteIds()
    {
      return [self::VISIBILITY_IN_SEARCH, self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH, self::VISIBILITY_IN_SITE];
    }
    

    1. Add VISIBILITY_IN_SITE to function getOptionArray() like this ,
      ‘Site’ is labels to show this option on backend

     public static function getOptionArray()
     {
        return [
          self::VISIBILITY_NOT_VISIBLE => __('Not Visible Individually'),
          self::VISIBILITY_IN_CATALOG => __('Catalog'),
          self::VISIBILITY_IN_SEARCH => __('Search'),
          self::VISIBILITY_BOTH => __('Catalog, Search'),
          self::VISIBILITY_IN_SITE => __('Site'),
        ];
     }
    

    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.

    enter image description here

    this work for me

    Login or Signup to reply.
  2. 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:

    1. Create a Custom Visibility Option:

      • Extend the MagentoCatalogModelProductVisibility class to add a custom visibility option.
    2. Override the Product Visibility Logic:

      • Update the methods in your custom visibility class to include the new visibility type.
    3. Update Dependency Injection:

      • In your di.xml file, use the preference to override the MagentoCatalogModelProductVisibility class with your custom class.

    Here is the step-by-step implementation:

    1. Extend the Visibility Class:
    <?php
    
    namespace YourVendorModuleNameRewriteMagentoCatalogModelProduct;
    
    class Visibility extends MagentoCatalogModelProductVisibility
    {
        const VISIBILITY_IN_SITE = 5;
    
        /**
         * Retrieve option array
         * @return array
         */
        public static function getOptionArray()
        {
            $defaultData  = parent::getOptionArray();
            $defaultData[self::VISIBILITY_IN_SITE] =  __('Site');
            return $defaultData;
        }
    
        /**
         * Retrieve all options
         * @return array
         */
        public static function getAllOptions()
        {
            $res = [];
            foreach (self::getOptionArray() as $index => $value) {
                $res[] = ['value' => $index, 'label' => $value];
            }
            return $res;
        }
    
        public function getVisibleInSiteIds(): array
        {
            return [self::VISIBILITY_IN_SEARCH, self::VISIBILITY_IN_CATALOG, self::VISIBILITY_BOTH, self::VISIBILITY_IN_SITE];
        }
    }
    
    1. Update the Dependency Injection Configuration (di.xml):
    <preference for="MagentoCatalogModelProductVisibility" type="YourVendorModuleNameRewriteMagentoCatalogModelProductVisibility" />
    

    This config ensures Magento uses your custom visibility logic.

    1. Apply 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search