I’ve made this file: catalog_category_view.xml
in the app/code/Namespace/Products/view/frontend/layout/
folder , with this content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="category.products.list">
<arguments>
<argument name="viewModel" xsi:type="object">NamespaceProductsViewModelListProductStock</argument>
</arguments>
</referenceBlock>
</body>
</page>
In the list.phtml
I have this:
$viewModel = $block->getData('viewModel');
echo $viewModel->getTest();
and I am getting an error that it doesn’t know about the getTest()
method. But if I add my xml code for the view model in the app/design/frontend/Magento/MyTheme/Magento_Catalog/layout/catalog_category_view.xml
, it works! Why ? Why I cannot use my view model xml code in my custom module ?
Also if I add this code: <referenceBlock name="category.products.list" remove="true"/>
in the app/code/Namespace/Products/view/frontend/layout/catalog_category_view.xml
, it works. So not sure why the view model doesn’t work .
Thanks