1

For example, I've to override class Magento\Store\Block\Switcher to Vendor\Module\Block\Switcher Now I need to add custom method, constructor, and protected variable in upgraded class Vendor\Module\Block\Switcher.

I've tried following code but it's not working

Add code like below in /etc/frontend/di.xml file.

<?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="Magento\Store\Block\Switcher" type="Vendor\Module\Block\Switcher\Index" />
   </config>

Add below code in my override class Vendor\Module\Block\Switcher

<?php 
namespace Vendor\Module\Block\Switcher;

class Index extends \Magento\Framework\View\Element\Template
{

  protected $httpContext; //new variable

  public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Framework\App\Http\Context $httpContext, // Inject New Class Just before Data array if you want to inject
    array $data = []
    ) {

      $this->_postDataHelper = $postDataHelper;
      $this->httpContext = $httpContext; //New object Get of Injected Class
      parent::__construct($context, $data);
    }

    /**
     * Code by devloper
     */
    public function isLoggedIn()
    {
        return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    }

    /**     
     * Code by developer
     */
    public function getBankUrl()
    {
        if (!$this->isLoggedIn()) {
            return '<li class="customer-login"><a href="javascript:void(0);">Bank sd</a></li>';
        } else {
            return '<li class=""><a href="'.$this->getUrl('rewards/referral').'">Bank</a></li>';        
        }
    }
}

?>
3

1 : Copy that block to your Module in Block Folder and override Using di.xml file in your custom module like :

A. find or add di.xml file at below location :

   /app/code/Vendor/Module/etc/frontend/di.xml

B. Add Code Like Below in di.xml file :

   <?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <preference for="Magento\Store\Block\Switcher" type="Vendor\Module\Block\Switcher" />
   </config>

2 : Change namespace According to your module on top

 namespace Magento\Store\Block\Switcher => namespace Vendor\Module\Block\Switcher

3 : Defining New Protected Variable , Injecting New Class, Defining New Function, Calling New function in Template File

    namespace Vendor\Module\Block\Switcher;
    class Index extends \Magento\Framework\View\Element\Template
    {

       protected $newVariable; //new variable

       public function __construct(
         \Magento\Framework\View\Element\Template\Context $context,
          /** All already injected classes**/,
          \Vendor\Module\Model\ModelClass $modelClassObject, // Inject New Class Just before Data array if you want to inject
          array $data = []
      ) {

          /** All Alreay Defined Variable**/
          $this->newVariable = $modelClassObject; //New object Get of Injected Class
          parent::__construct($context, $data);
       }

       /** New Function **/
       public function newFunction(){

       }
     }

4 : You Can get New Function in Associated template/view file like :

    $this->newFunction(); or $block->newFunction();
  • Please check my updated answer, I have added override class but it is not working. – Aasim Goriya Oct 4 '17 at 9:30
  • See My New Answer Below. @AasimGoriya – Sujeet Pandit Oct 4 '17 at 10:41
1

1.You have to copy whole Switcher Class Block File to your Custom Module BLock File, Donot Remove Any Thing i.e class variables,functions etc from Copied Class in your module .

  1. di.xml file according to you , Replace Vendor\Module according to yours :

       <?xml version="1.0"?>
       <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <preference for="Magento\Store\Block\Switcher" type="Vendor\Module\Block\Switcher" />
       </config>
    
  2. Answer as you Required :

    <?php                                                                 
    namespace Vendor\Module\Block;  //replace Vendor\Module with your ones
    
    class Switcher extends \Magento\Framework\View\Element\Template
    {
    
      protected $customerSession; //new variable
    
      public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Customer\Model\Session $session, // Inject Correct Session Class Just before Data array if you want to inject
        array $data = []
        ) {
    
          $this->_postDataHelper = $postDataHelper;
          $this->customerSession = $session; //New object Get of Injected Session Class
          parent::__construct($context, $data);
        }
    
        public function getBankUrl()
        {
            if ($this->customerSession->isLoggedIn()) {
                return '<li class="customer-login"><a href="javascript:void(0);">Bank sd</a></li>';
            } else {
                return '<li class=""><a href="'.$this->getUrl('rewards/referral').'">Bank</a></li>';        
            }
        }
    }
    ?>
    
  • Did you get the answer @AasimGoriya ?? – Sujeet Pandit Oct 5 '17 at 4:30
  • I have already tried this before, but it wasn't work for me. – Aasim Goriya Oct 5 '17 at 10:30
  • Please Check The code again as Class Name is 'Switcher' and in your code It is 'Index'. Copy my Above Code and Try it will work, I have tested here. @AasimGoriya – Sujeet Pandit Oct 5 '17 at 11:27

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.