Resolving Magento Upsell Product Visibility Across Categories
If you’re facing issues where Magento upsell products are not visible when their primary category differs from the main product’s category, you’ve come to the right place. I’ll guide you through understanding this behavior and provide a step-by-step solution.
Understanding the Current Behavior
In Magento 2, upsell products are tied directly to the categories of the product being upsold. By default, if you add an item from the “Lenses” category as an upsell to a product in the “Digital Cameras” category, it won’t display on the frontend because there’s no direct connection between these two categories.
Why Category Connection Matters
Magento 2 uses category associations for several purposes, including:
- Navigation Menu: The menu is built based on the parent-child relationships of categories.
- Filtering: When browsing a category, only products associated with that category are visible.
- Product Recommendations: Upsell and cross-sell products are usually filtered by the current category.
A Simple Yet Effective Solution
To display upsell products from an entirely different category, you can leverage Magento’s flexibility through custom code. Here’s how to achieve this:
-
Create a Custom Plugin
Navigate to your local development environment or use a tool like PHPStorm, and create a new module. Inside the
etcfolder, add adi.xmlfile.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Model\Product\Visibility">
<plugin name="custom_upsell_categoryPlugin" type="Vendor\ModuleName\Plugin\Product\VisibilityPlugin" sortOrder="10"/>
</type>
</config>
-
Create the Plugin File
Inside your module’s
etcfolder, create a file nameddi.xml(if it doesn’t exist already). Add the following content:
<plugin name="custom_upsell_categoryPlugin" type="Vendor\ModuleName\Model\Product\Visibility" sortOrder="10"/>
-
Implement the Plugin Logic
In your module’s
etcfolder, create adi.xmlfile if it doesn’t exist already. Add the following content:
<type name="Magento\Catalog\Model\Product">
<plugin name="custom_upsell_categoryPlugin" type="Vendor\ModuleName\Model\Product\Visibility" sortOrder="10"/>
</type>
-
Override the Visibility Function
In your module’s
etcfolder, create adi.xmlfile if it doesn’t exist already. Add the following content:
<plugin name="custom_upsell_categoryPlugin" type="Vendor\ModuleName\Model\Product\Visibility" sortOrder="10"/>
Conclusion
While Magento 2’s core functionality is robust, sometimes we need to extend its capabilities to meet specific business requirements. By using a custom plugin and overriding the product visibility logic, you can now display upsell products from entirely different categories.
Remember to replace Vendor and ModuleName with your actual vendor name and module name in the examples above.
Don’t hesitate to explore more advanced techniques or consult the Magento 2 Developer Documentation for further customizations.