I am overriding the layout and adding ViewModel in that layout but when I try to fetch the ViewModel getting below error

PHP Fatal error: Uncaught Error: Call to a member function getCodes() on null

My XML files look life which resides under


    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">+
        <referenceBlock name="product_additional_data">
                <argument name="view_model" xsi:type="object">Custom\ProductModel\ViewModel\LocaleInfo</argument>

and my ViewModel resides under


and files look like

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Locale\Resolver as LocaleResolver;
use Magento\Store\Model\ScopeInterface;

class LocaleInfo implements ArgumentInterface
    protected $scopeConfig;
    protected $localeResolver;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        LocaleResolver $localeResolver
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->localeResolver = $localeResolver;

    public function getCodes($storeId = null)
        $configPath = $this->localeResolver->getDefaultLocalePath();
        $localeCode = $this->scopeConfig->getValue($configPath, ScopeInterface::SCOPE_STORE, $storeId);

        return $localeCode;

trying to fetch like below

$myViewModel = $block->getViewModel();
echo $myViewModel->getCodes();
  • Please provide the phtml file location and also do you have namespace declaration like namespace Custom\ProductModel\ViewModel; ? You also need to clear your caches and perform compilation – Rajeev K Tomy May 25 '18 at 9:33
  • app/design/frontend/Custom/Theme/Magento_Review/templates/product/view/list.phtml thats the phtml file location – Daniel_12 May 25 '18 at 10:10
  • Yes I have declared namespace as well – Daniel_12 May 25 '18 at 10:10

This is the block you are referencing in your layout handle XML file.

<block class="Magento\Review\Block\Product\View\ListView" 

You are referencing this block with its alias name; i.e, product_additional_data which is wrong. You should refer it with its name; i.e, product.info.product_additional_data.

So you should change your code like this:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">+
        <referenceBlock name="product.info.product_additional_data">
                <argument name="view_model" xsi:type="object">Custom\ProductModel\ViewModel\LocaleInfo</argument>

After this changes made please clear your cache and then you are good to go.


Your layout update XML file location also seems wrong. It should be like:


  • No luck :( still have same error nothing change – Daniel_12 May 25 '18 at 11:54
  • @Daniel_12 see my edit. – Rajeev K Tomy May 25 '18 at 11:57
  • I am overriding layout and I think its location is correct. check this link devdocs.magento.com/guides/v2.2/frontend-dev-guide/layouts/… – Daniel_12 May 25 '18 at 12:05
  • But I tried as you say but still no luck same error – Daniel_12 May 25 '18 at 12:05
  • @Daniel_12 you here did a theme override. So the location should be as shown in my answer. You can see that in the given link too. With this, your code should work. I cannot see any other mistakes. check your logs for any errors. – Rajeev K Tomy May 25 '18 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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