0

In the EU, you have go to label if a price is incl. or excl. tax in the shop frontend. If you run different shops where you have got both settings, it could be realized with different custom translation strings which are store view specific. Magento has the information, if a price is incl or excl tax, but the question is how to code it. In Magento1, I solved it with the following code, but so far I have no idea how to do it with Magento2:

<?php $tax_display_type = Mage::getStoreConfig(
               'tax/display/type',
               Mage::app()->getStore()
           );?>
   <?php if ($tax_display_type == 2): ?>
    <?php echo $this->__('Incl. Tax') ?> 
<?php else: ?>
    <?php echo $this->__('Excl. Tax') ?> 
<?php endif;?>

Any ideas? Thank you very much!

1 Answer 1

0

app/code/SR/MagentoCommunity/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\PriceBox">
        <plugin name="sr_text_added_product_render_amount"
                type="SR\MagentoCommunity\Plugin\Pricing\Render\PriceBox" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Pricing/Render/PriceBox.php

<?php
namespace SR\MagentoCommunity\Plugin\Pricing\Render;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class PriceBox
{
    /**
     * @var ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * PriceBox constructor.
     *
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * @param \Magento\Framework\Pricing\Render\PriceBox $subject
     * @param string $html
     * @return string
     */
    public function afterRenderAmount(
        \Magento\Framework\Pricing\Render\PriceBox $subject,
        $html
    ) {
        $taxDisplayType = $this->scopeConfig->getValue('tax/display/type', ScopeInterface::SCOPE_STORE);
        if ($taxDisplayType == 2) {
            $html = __('Incl. Tax ') . $html;
        } else {
            $html = __('Excl. Tax ') . $html;
        }

        return $html;
    }
}
1
  • Thank you very much for your reply! Can you be more specific about how the procedure around? I added the code to the site but it does not show up. Sorry for the pretty basic question, I am new to Magento2. I run php bin/magento setup:upgrade and php bin/magento cache:clean but its not showing up. Am I missing something? Commented Aug 19, 2019 at 15:07

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.