0

How can I get the current language code in Magento_Catalog::product/price/amount/default.phtml?

1 Answer 1

0

Create an after plugin on \Magento\Framework\Pricing\Render\RendererPool::createAmountRender and pass currency as a Data value to Block.

Declare di.xml

app/code/{VendorName}/{ModulePath}/etc/frontend/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Pricing\Render\RendererPool">
        <plugin disabled="false" name="StackExchange_MagentoModule_Plugin_Frontend_Magento_Framework_Pricing_Render_RendererPool" sortOrder="10" type="StackExchange\MagentoModule\Plugin\Frontend\Magento\Framework\Pricing\Render\RendererPool"/>
    </type>
</config>

Plugin Class:

app/code/{VendorName}/{ModulePath}/Plugin/Frontend/Magento/Framework/Pricing/Render/RendererPool.php

Code:

<?php

namespace StackExchanges\MagentoModule\Plugin\Frontend\Magento\Framework\Pricing\Render;

use Magento\Framework\Locale\Resolver;
use Magento\Framework\Pricing\Render\PriceBoxRenderInterface;

class RendererPool
{

    /**
     * @var Resolver
     */
    private $resolver;

    public function __construct(Resolver $resolver)
    {
        $this->resolver = $resolver;
    }
    public function afterExecute(
        \Magento\Framework\Pricing\Render\RendererPool $subject,
        $result
    ) {
        if ($result instanceof  PriceBoxRenderInterface) {
            $result->setData('lang_code',$this->resolver->getLocale());
        }
        return $result;
    }
}

Then on default.phtml , you can get the current language code using

$block->getLangCode();

2
  • Thanks for your answer, where did you declared the getLangCode method? Commented Nov 25, 2020 at 13:15
  • 1
    Asc $result->setData('lang_code',$this->resolver->getLocale()); using code that why lang_code converted to a setter function getLangCode
    – Amit Bera
    Commented Nov 25, 2020 at 18:45

Your Answer

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

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