0

I have created a custom module, I have added custom module enable/disable functionality from the admin panel,

but it's not working properly, if I've disabled the module then also content display at the front-end,

here is the code that I have tried.

syetm.xml file add custom filed for module configuration system.xml

<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="10">
            <label>Share Cart</label>
        </tab>
        <section id="wpsharecart" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>Share Cart</label>
            <tab>ncode</tab>
            <resource>Ncode_Wpsharecart::wpsharecart_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General Configuration</label>
                    <field id="enablesharecart" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>WhatsApp Share Cart Enable</label>
                        <comment><![CDATA[Select Yes to enable Whatsapp Cart module on cart page.]]></comment>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                    </field>

                    <field id="button_label_text" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Button Label Text</label>  
                        <validate>required-entry</validate> 
                        <depends>
                            <field id="enablesharecart">1</field>
                        </depends>                      
                    </field>

                    <field id="cart_message_on_wp_share" translate="label" type="textarea" sortOrder="31" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Cart Message on Whatsapp Share Link</label>   
                        <validate>required-entry</validate>    
                        <depends>
                            <field id="enablesharecart">1</field>
                        </depends>                  
                    </field>

                    <field id="wp_button_position" translate="label" type="select" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Whatsapp Button Position on Cart Page</label>
                        <comment><![CDATA[select button position for display on cart page]]></comment>
                         <source_model>Ncode\Wpsharecart\Model\Label\Source\Position</source_model>
                        <depends>
                            <field id="enablesharecart">1</field>
                        </depends>
                    </field>
                    <!-- <field id="display_text" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                        <label>Display Text</label>
                        <comment>This text will display on the frontend.</comment>
                    </field> -->
            </group>
  </section>
    </system>
</config>

custom helper to check module enable / disable based on system configuration Data.php

<?php
namespace Ncode\Wpsharecart\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{    
    const MODULE_ENABLE = "wpsharecart/general/enable";   

    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);
    }
}

Ncode\Wpsharecart\Block\Index.php

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

    //  protected $_postFactory;
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Ncode\Wpsharecart\Helper\Data $dataHelper, 
        array $data = []
    )
    {
        //$this->_postFactory = $postFactory;
        $this->_dataHelper = $dataHelper;
        parent::__construct($context, $data);
    }

    public function canShowBlock()
    {
      return $this->_dataHelper->isModuleEnabled();
    }
    public function getPostCollection(){
        $post = $this->_postFactory->create();
        return $post->getCollection();
    }
}

Block to get module configuration value Ncode/Wpsharecart/view/frontend/templates/onepage/wpbutton.phtml

<?php if ($block->canShowBlock()): ?>
  <h1> My Mdule content </h1>
 <?php endif; ?>

please let me know what should I do mistakes here.

1 Answer 1

1

field id is wrong change that from enable --> enablesharecart

configuration path should match like sectionid/groupid/fieldid

3
  • from which file i need to change?
    – rocky9310
    Commented Oct 26, 2021 at 13:31
  • const MODULE_ENABLE = "wpsharecart/general/enable"; should be const MODULE_ENABLE = "wpsharecart/general/enablesharecart";
    – Pawan
    Commented Oct 26, 2021 at 13:32
  • yes..done..its work now..thanks all.
    – rocky9310
    Commented Oct 26, 2021 at 13:34

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.