- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-17-2023
06:25 AM
11-17-2023
06:25 AM
How to create custom filter
I have a doubt, in my filters I need that, when clicking on a color, for example BLUE, it opens the variations of BLUE below (Navy Blue, Dark Blue..) I thought about making 2 attributes, one as master color, and another as a secondary color, however, I can't figure out how to display this. So that when you click on the color, it displays the secondary colors below. How can I do this? Is there a search engine that can help me, or something similar?
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023
02:56 AM
11-21-2023
02:56 AM
Re: How to create custom filter
Hello @viniaraujo493a
Master Color Attribute:
- Create a master color attribute (e.g., master_color).
Secondary Color Attribute:
- Create a secondary color attribute (e.g., secondary_color).
Step 2: Create Configurable Products
- Configure Configurable Products:
- Create configurable products for each master color.
- Associate simple products with variations (e.g., Navy Blue, Dark Blue) to the configurable products.
Step 3: Modify Product Templates
Override Attribute PHTML Files:
- In your custom theme, override the template files responsible for rendering product attributes.
- For example, you might need to override Magento_ConfigurableProduct/templates/product/attribute/option-renderer/list.phtml.
Add JavaScript Logic:
- Add JavaScript logic to capture the selection of the master color and dynamically update the options for the secondary color.
// Sample JavaScript (replace with actual implementation) require([ 'jquery', 'underscore', 'Magento_Swatches/js/swatch-renderer' ], function ($, _, swatchRenderer) { // Listen for changes in the master color dropdown $('.swatch-attribute.master_color').on('click', '.swatch-option', function () { var selectedMasterColor = $(this).attr('option-id'); // Use AJAX to fetch and update the secondary color swatches // Example: You might have an endpoint that returns secondary colors based on the master color $.ajax({ url: '/getSecondaryColors/' + selectedMasterColor, type: 'GET', dataType: 'json', success: function (data) { // Update the secondary color swatches swatchRenderer.clear(); swatchRenderer.updateBaseImage(data); swatchRenderer.updateSelect(data); }, error: function () { console.error('Failed to fetch secondary colors.'); } }); }); });
Create a Controller/Endpoint:
- Implement a controller or endpoint that fetches secondary color options based on the selected master color.
Return JSON Response:
- Ensure that the server-side logic returns a JSON response containing the necessary information for updating the secondary color options.
Was my answer helpful? You can accept it as a solution.
200+ Premium Magento 2 Extensions Need help? Hire Magento Developer
200+ Premium Magento 2 Extensions Need help? Hire Magento Developer