0

I am rewritting the /vendor/magento/module-catalog/view/base/templates/product/price/amount/default.phtml file. The file MyCompany_MyApp::product/default.phtml works fine, but I want to pass a viewModel into the block as an argument but this doesn't work as expected.

This doesnt work when trying to add:

     <argument name="viewModel" xsi:type="object">MyCompany\MyApp\ViewModel\ProductLoader</argument>

Into: MyCompany/MyApp/view/frontend/layout/catalog_product_prices.xml

<block class="Magento\Framework\Pricing\Render\RendererPool" name="render.product.prices">
    <arguments>
        <argument name="viewModel" xsi:type="object">MyCompany\MyApp\ViewModel\ProductLoader</argument>
        <argument name="default" xsi:type="array">
            <item name="default_render_class" xsi:type="string">Magento\Catalog\Pricing\Render\PriceBox</item>
            <item name="default_render_template" xsi:type="string">Magento_Catalog::product/price/default.phtml</item>
            <item name="default_amount_render_class" xsi:type="string">Magento\Framework\Pricing\Render\Amount</item>
            <item name="default_amount_render_template" xsi:type="string">MyCompany_MyApp::product/default.phtml</item>
            <item name="prices" xsi:type="array">
                <item name="special_price" xsi:type="array">
                    <item name="render_template" xsi:type="string">Magento_Catalog::product/price/special_price.phtml</item>
                </item>
                <item name="tier_price" xsi:type="array">
                    <item name="render_template" xsi:type="string">Magento_Catalog::product/price/tier_prices.phtml</item>
                </item>
                <item name="final_price" xsi:type="array">
                    <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\FinalPriceBox</item>
                    <item name="render_template" xsi:type="string">Magento_Catalog::product/price/final_price.phtml</item>
                </item>
                <item name="custom_option_price" xsi:type="array">
                    <item name="amount_render_template" xsi:type="string">MyCompany_MyApp::product/default.phtml</item>

                </item>
                <item name="configured_price" xsi:type="array">
                    <item name="render_class" xsi:type="string">Magento\Catalog\Pricing\Render\ConfiguredPriceBox</item>
                    <item name="render_template" xsi:type="string">Magento_Catalog::product/price/configured_price.phtml</item>
                </item>
            </item>
            <!--<item name="adjustments" xsi:type="array"></item>-->
        </argument>
    </arguments>
</block>

This works, I can pass this viewModel into: MyCompany/MyApp/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="catalog_product_opengraph" />
    <update handle="page_calendar"/>
    <body>
        <referenceContainer name="product.info.main">
            <block class="Magento\Catalog\Block\Product\View\Description" name="changeproduct" template="Ecommerce121_FlooringCalculator::product/view/accessProduct.phtml" after="product.info.price">
                <arguments>
                    <argument name="viewModel" xsi:type="object">MyCompany\MyApp\ViewModel\ProductLoader</argument>
                </arguments>
            </block>
            <block class="Magento\Catalog\Block\Product\View\Description" name="showCalculator" template="Ecommerce121_FlooringCalculator::product/view/showApp.phtml" after="product.info.price">
                <arguments>
                    <argument name="viewModel" xsi:type="object">MyCompany\MyApp\ViewModel\AppLogic</argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

How do I make it work in catalog_product_prices.xml

1
  • did you find any solution for this?
    – rukrlf
    Jun 29, 2022 at 2:29

1 Answer 1

2

I was facing an issue where my view_model was working fine on product detail page for app/design/frontend/Vendor/theme-default/Magento_Catalog/templates/product/price/final_price.phtml, but was $block->getData('view_model') was null on list page. Then after digging a bit I added this code inside catalog_category_view.xml for frontend and it worked. Passed the viewModel class referencing product.price.render.default block.

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock name="product.price.render.default">
        <arguments>
            <argument name="view_model" xsi:type="object">Vendor\Module\ViewModel\PriceInfo</argument>
        </arguments>
    </referenceBlock>
</body>
1
  • Thank you for your own response. You saved my time ;)
    – GieDe
    Feb 22 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.