I'm trying to set a custom template file for block category.products.list for just a few marked categories.
Sounds simple enough, but it turns out it isn't, mostly because of some bugs in the version I'm using. (2.3.3) Updating Magento is usually a project in its own right so I'm not open to doing so right now.
Who knows another, more elegant way to do it?
Here's a list of the methods I already explored.
catalog_category_view_id_[id].xml should alter the layout for only that category with id [id].
Not usable: I can't use that one, because I can't hard code a database ID into a module. I want it to be usable for multiple categories in any database.
Why is it built like this anyway?
use custom layout XML in the admin panel. Enter
<update handle="mymodule_category_view"/> for every category that needs changing.
mymodule_category_view.xml with the changes.
Failed: This doesn't work because of a bug that's solved in 2.3.4. All
<update> tags are simply ignored when they're in the layout update xml.
use custom layout XML in the admin panel to change the template file of block category.products.list and others.
<referenceContainer name="content"> <referenceBlock name="category.products.list"> <action method="setTemplate"> <argument name="template" xsi:type="string">Vendor_Module::product/customlist.phtml</argument> </action> </referenceBlock> </referenceContainer>
Failed: This doesn't work because of what looks like a bug.
There's a module in my shop that has a catalog_category_view.xml that also changes the template using Action + setTemplate. Apparently that overrides the custom layout XML.
When I disable that module or just comment out that part of that catalog_category_view.xml suddenly my custom XML becomes active so that proves my syntax is valid and the override order is handled wrong.
Method 4: use a plugin to change the template in a beforeToHtml on the block class. This should override any XML changes. Relevant categories should be marked in some way, possibly using a new EAV attribute or something. Sometimes block classes are used for multiple blocks with different templates, so extra checks should be in place.
Undesired: It'd work but it's not very elegant. It sabotages the whole layout xml functionality for that block class. Adding an EAV attribute (alsoi to UI) to mark categories is also quite a lot for a simple workaround.
Method 5: Use an observer Like mentioned here
This is better than the plugin but still requires adding an EAV attribute.