1

I have created a ViewModel, when I am calling it in the phtml it giving me the below error Fatal error: Uncaught Error: Call to a member function getSomething() on null in Magento_Catalog/templates/product/list/items.phtml:16 Stack trace: #0

here is my catalog_product_view.xml

<block name="product.info.upsell.customviewmodel"
                   class="Magento\Framework\View\Element\Template"
                   template="Magento_Catalog::product/list/items.phtml">
                <arguments>
                        <argument name="view_model" xsi:type="object">Noshad\VMpractice\ViewModel\PreparePostData</argument>
                    </arguments>
            </block>

here is my ViewModel class

<?php

namespace Noshad\VMpractice\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;


class PreparePostData implements ArgumentInterface
{


    public function getSomething()
    {
        echo 'find me';
    }

}

here are my items.phtml code

/* @var Noshad\VMpractice\ViewModel\PreparePostData $viewModel */

$viewModel =  $block->getData('view_model');

echo $viewModel->getSomething();

whenever I load the product detail page I gets the below error: Fatal error: Uncaught Error: Call to a member function getSomething() on null in /var/www/html/practice234/app/design/frontend/Smartwave/porto_child/Magento_Catalog/templates/product/list/items.phtml:16 Stack trace: #0

2 Answers 2

1

Try this below code :-

ViewModel/Custom.php

<?php

namespace Mage\Mohit\ViewModel;

class Custom implements \Magento\Framework\View\Element\Block\ArgumentInterface
{

    public function __construct(
       
    ) {
    }

    public function getSomething() {
        return "Hello Magento Stackexchange";
    }
}

items.phtml

<?php

$viewModel = $block->getData('view_model');
echo $viewModel->getSomething();

catalog_product_view.xml

<block name="product.info.upsell.customviewmodel"
                   class="Magento\Framework\View\Element\Template"
                   template="Mag_Mohit::items.phtml">
                <arguments>
                        <argument name="view_model" xsi:type="object">Mage\Mohit\ViewModel\Custom</argument>
                    </arguments>
            </block>

After below commmands run :-

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
php bin/magento c:f

enter image description here

THANKS.

4
  • Thank you for your reply, the value is working if I place the items.phtml file in the same module if I give the reference of magento_catalog items.phtml then it throws the same error, I think the view model will work in the same module where I have created it. Thanks
    – Noshad
    Dec 21, 2020 at 10:45
  • pls accept answer Dec 21, 2020 at 10:46
  • done, please like my question also
    – Noshad
    Dec 21, 2020 at 13:14
  • Please accept answer if helped my answer Dec 22, 2020 at 3:39
0

You need to add the \ in your xml before your class

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.