I need to display the only Size attribute drop-down on a category page listing. Size is a configurable option & Catalog Input Type for Store Owner value is Dropdown.
I have enabled Used in Product Listing property enable.
configurable-productconfigurable-product-optionsdropdown-optionsmagento2
I need to display the only Size attribute drop-down on a category page listing. Size is a configurable option & Catalog Input Type for Store Owner value is Dropdown.
I have enabled Used in Product Listing property enable.
Best Answer
This All depends if your looking to have swatches or dropdowns the answer for dropdowns is not so easy.
First create a custom module. And add the following to your di, file this class stops configurable products getting the correct add to cart.
Then create a custom php class Under Plugin called configurable
Next step is to overwrite the Magento_Catalog::templates/list.phtml and add the following under the hidden input fields that are already there.
Also you need to modify the form element itself to look like below
Now your list view has its actual fields, you need to add a catalog_category_view.xml layout either in the theme or in the module created above, and add the following. This will call the product view configurable template into your list.
You will need to do the same for catalogsearch_result_index.xml as the xml is different for search results though uses the same catalog list template
Now we have this you need to overide the Magento_Catalog::product/list/view/type/options/configurable.phtml template again in the theme or module, you then need to make it look like the below.
What has changed from default configurable.phtml
We added the below init script this makes the prices dynamic on the grid itself, without the below prices fail to inizalize and configurable options with static prices would fail.
We have also modified the paramatersin the configurable x-init to make them more specific to the grid inidviual items otherwise all prices would change on each item and so would the hidden configurable_selected hidden item.
Final Thing to do is to call the options in the loop in your list.phtml again, within the scope of the form,
What we are doing is getting our child block and setting the specific list item as the product on the block otherwise it would try and use the registry like it does on product view.
Thats it hope this helps.