0

I have created one custom module, but from backend, I can not get that proper value at the front end,

I don't understand what I do a mistake here, please guide me on this.

here is my code,

system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="ncode" translate="label" sortOrder="11">
            <label>NCode Technologies</label>
        </tab>
        <section id="gsttax" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>nGST TAX</label>
            <tab>ncode</tab>
            <resource>Ncode_GSTtax::gsttax_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                
                    <field id="enablesngsttax" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>GST TAX Enable</label>
                        <comment><![CDATA[Select Yes to enable GST TAX  module]]></comment>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>  

                    <field id="gstin" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>GSTIN</label>
                        <comment><![CDATA[Enter the GST Number]]></comment>
                        <validate>required-entry</validate> 
                        <depends>
                            <field id="enablesngsttax">1</field>
                        </depends> 
                    </field>

                     <field id="gstrate" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>GST Rate(Percentage)</label>
                        <comment><![CDATA[Set GST Percentage Which Your Products Fall.]]></comment>
                         <source_model>Ncode\GSTtax\Model\Label\Source\Rate</source_model>
                        <depends>
                            <field id="enablesngsttax">1</field>
                        </depends>
                    </field>  


                    <field id="gstminamount" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Minimum Amount of Product</label>
                        <comment><![CDATA[Enter the Minimum Product Amount to Apply GST Rates.]]></comment>
                        <validate>required-entry</validate> 
                        <depends>
                            <field id="enablesngsttax">1</field>
                        </depends> 
                    </field>

                    <field id="gstbusssinesorigin" translate="label" type="text" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Busssines Origin</label>
                        <comment><![CDATA[Enter the Origin Status]]></comment>
                        <validate>required-entry</validate> 
                        <depends>
                            <field id="enablesngsttax">1</field>
                        </depends> 
                    </field>
                             
            </group>
        </section>
    </system>
</config>

Block/Index.php

<?php
namespace Ncode\GSTtax\Block;
class Index extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Ncode\GSTtax\Helper\Data
    */
    protected $_postFactory;
    protected $_dataHelper;
    /**
      * @param \Magento\Framework\View\Element\Template\Context $context
      * @param \Ncode\GSTtax\Helper\Data $dataHelper
      * @param array $data
    */

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Ncode\GSTtax\Helper\Data $postFactory, 
        \Ncode\GSTtax\Helper\Data $dataHelper, 
        array $data = []
    )
    {
        $this->_postFactory = $postFactory;
        $this->_dataHelper = $dataHelper;
        parent::__construct($context, $data);
    }
    
    public function gstEnable()
    {
      return $this->_dataHelper->isModuleEnabled();
    }
    
    public function getPostCollection(){
        $post = $this->_postFactory->create();
        return $post->getCollection();
    }
     public function helperInit() {
        return $this->_dataHelper;
    }
}

Helper/Data.php

<?php
namespace Ncode\GSTtax\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{    
    const MODULE_ENABLE                     = "gsttax/general/enablesngsttax"; 
    const XML_PATH_GST_IN                   = 'gsttax/general/gstin';
    const XML_PATH_GST_AMOUNT               = 'gsttax/general/gstminamount';
    const XML_PATH_GST_ORIGIN               = 'gsttax/general/gstbusssinesorigin';

    public function getDefaultConfig($path)
    {
        return $this->scopeConfig->getValue($path, 
        \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT);
    }

    public function isModuleEnabled()
    {
       return (bool) $this->getDefaultConfig(self::MODULE_ENABLE);
    }

    public function gst_in()
    {
       return (bool) $this->getDefaultConfig(self::XML_PATH_GST_IN);
    }

    public function gst_amount()
    {
       return (bool) $this->getDefaultConfig(self::XML_PATH_GST_AMOUNT);
    }

    public function gst_origin()
    {
       return (bool) $this->getDefaultConfig(self::XML_PATH_GST_ORIGIN);
    }

}

view/frontend/templates/onepage/gsttaxview.phtml

<?php
    $helper        = $this->helperInit();
    $gstin         = $helper->gst_in();
    $gstamount     = $helper->gst_amount();
    $gstorigin     = $helper->gst_origin();
    
?>

<?php  if ($block->gstEnable()): ?>
    <?php echo $gstin; ?>
    <?php echo $gstamount; ?>
    <?php echo $gstorigin; ?>
<?php endif; ?>

**My OUTPU is like

1 1 1

** and I have entered below image data

enter image description here

0

In your case you're using 'default' scope when getting config values. In this case, you will be getting the values defined in your etc/config.xml file. In order to get the actual configured value, you need to use 'store' scope.

So in your code you should replace

return $this->scopeConfig->getValue($path, \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT);

with

return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
5
  • i have tried this but its not work
    – rocky9310
    Nov 18 at 13:20
  • Have you flushed cache after adding the changes? Nov 18 at 13:29
  • Ah, I got it! In your methods gst_in(), gst_amount(), etc. you have '(bool)' in each 'return' statement. You should remove this. Because it will convert all the values to boolean. Nov 18 at 13:31
  • yes you are right..its work now thanks
    – rocky9310
    Nov 18 at 13:34
  • Please, don't forget marking the answer as the correct one ;) Thanks! Nov 18 at 13:38

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.