1

I create two tax rule name rule1 and disability tax ,check below images enter image description here

Now I want to display these two tax rule names on the product page. any help will be appreciated. enter image description here

1
  • do you need to display all the rule name in the product detail page ?
    – Hiren
    Feb 6, 2020 at 9:48

1 Answer 1

1

Create Module with the following code, this will give you exactly that you are looking for :D

=> app/code/Vendor/Module/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

=> app/code/Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.0.1">
    </module>
</config>

=> app/code/Vendor/Module/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">
    <body>
        <referenceContainer name="content">
            <referenceContainer name="product.info.main">
                <block class="Vendor\Module\Block\TexRuleNames" name="tex.rule.names" template="Vendor_Module::product/view/text_rule_names.phtml" after="product.info.price" />
            </referenceContainer>
        </referenceContainer>
    </body>
</page>

=> app/code/Vendor/Module/Block/TexRuleNames.php

<?php
namespace Vendor\Module\Block;

class TexRuleNames extends \Magento\Framework\View\Element\Template
{
    public function __construct
    (
        \Magento\Tax\Model\TaxRuleCollection $taxRuleCollection,
        \Magento\Framework\View\Element\Template\Context $context
    )
    {
        $this->_taxRuleCollection = $taxRuleCollection;

        parent::__construct($context);
    }

    public function getTextRuleNames()
    {
        return $this->_taxRuleCollection->getColumnValues('code');
    }
}

=> app/code/Vendor/Module/view/frontend/templates/product/view/text_rule_names.phtml

<?php $TextRuleName = $block->getTextRuleNames(); ?>

<div class="text-rules">
    <?php foreach ($TextRuleName as $rule): ?>
        <h1><?php echo $rule; ?></h1>
    <?php endforeach; ?>
</div>
5
  • where exactly you want this in detail page?
    – Hiren
    Feb 6, 2020 at 11:06
  • hi @irshad, i have updated my answer with all required file.
    – Hiren
    Feb 6, 2020 at 11:30
  • what errors are you facing ?
    – Hiren
    Feb 6, 2020 at 12:45
  • hi @irshad, check line number 12 in Vendor\Module\Block\TexRuleNames here i added $ twice in $taxRuleCollection, correct it in your code
    – Hiren
    Feb 6, 2020 at 13:07
  • hi @irshad, working now?
    – Hiren
    Feb 6, 2020 at 13:47

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.