0

My observer :

<?php
namespace Gta\EnablePaymentMethod\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodEnable implements ObserverInterface {
  
    protected $_customerSession;
    public function __construct(
       \Magento\Customer\Model\Session $customerSession
    ) {
       $this->_customerSession = $customerSession;
    }


    protected $_storeManager;
    public function __construct(
       \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
       $this->_storeManager = $storeManager;
    }



    public function execute(Observer $observer) {

      $website_id = $this->_storeManager->getStore()->getWebsiteId();
      if($website_id == '1')
      {

      $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
      if ($payment_method_code == 'paypal_express') {
        $result = $observer->getEvent()->getResult();
        $result->setData('is_available', false);
       if ($this->_customerSession->isLoggedIn()) {
           $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
           if ($customerGroupId == 9) {
               $result->setData('is_available', true);
            }
        }
      }
    }
    }
}

Getting Fatal error:

Fatal error: Cannot redeclare Gta\EnablePaymentMethod\Observer\PaymentMethodEnable::__construct() in /var/www/html/test/app/code/Gta/EnablePaymentMethod/Observer/PaymentMethodEnable.php on line 15

How to solve the error?

Note : Without website id condition observer working, i want to add website Id.

2
  • you have redeclare constuct function so Oct 29, 2020 at 5:34
  • Any help to correct my code
    – zus
    Oct 29, 2020 at 5:34

3 Answers 3

1

Replace with my code

<?php
namespace Gta\EnablePaymentMethod\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class PaymentMethodEnable implements ObserverInterface {
  
    protected $_customerSession;

    protected $_storeManager;



    public function __construct(
       \Magento\Customer\Model\Session $customerSession,
       \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
       $this->_customerSession = $customerSession;
       $this->_storeManager = $storeManager;
    }




    public function execute(Observer $observer) {

      $website_id = $this->_storeManager->getStore()->getWebsiteId();
      if($website_id == '1')
      {

      $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
      if ($payment_method_code == 'paypal_express') {
        $result = $observer->getEvent()->getResult();
        $result->setData('is_available', false);
       if ($this->_customerSession->isLoggedIn()) {
           $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
           if ($customerGroupId == 9) {
               $result->setData('is_available', true);
            }
        }
      }
    }
    }
}

and run again compile command

5
  • After enable and disable the module, what is the command to run, shall i run all the commands like an upgrade, compiled, static content deploy?
    – zus
    Oct 29, 2020 at 6:02
  • Here how can i add Guest Customer condition, the guest customer also not eligible to pay PayPal.
    – zus
    Oct 29, 2020 at 6:24
  • @zus can you post another question here is write about website id related code Oct 29, 2020 at 6:36
  • We put condition only for a customer group 9, so that guest customer also not able to pay via PayPal. Thank You
    – zus
    Oct 29, 2020 at 6:55
  • Any help on this magento.stackexchange.com/q/339482/57334 thanks
    – zus
    Jun 14, 2021 at 8:08
1

Try This.

<?php
namespace Gta\EnablePaymentMethod\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodEnable implements ObserverInterface {
  
    protected $_customerSession;
    protected $_storeManager;

    public function __construct(
       \Magento\Store\Model\StoreManagerInterface $storeManager,
       \Magento\Customer\Model\Session $customerSession
    ) {
       $this->_storeManager = $storeManager;
       $this->_customerSession = $customerSession;
    }



    public function execute(Observer $observer) {

      $website_id = $this->_storeManager->getStore()->getWebsiteId();
      if($website_id == '1')
      {

      $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
      if ($payment_method_code == 'paypal_express') {
        $result = $observer->getEvent()->getResult();
        $result->setData('is_available', false);
       if ($this->_customerSession->isLoggedIn()) {
           $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
           if ($customerGroupId == 9) {
               $result->setData('is_available', true);
            }
        }
      }
    }
    }
}
4
  • I am getting this error, 1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Gta\EnablePaymentMethod\Observer\PaymentMethodEnable
    – zus
    Oct 29, 2020 at 5:47
  • Share full error log please Oct 29, 2020 at 5:49
  • Compile command solve the issue
    – zus
    Oct 29, 2020 at 5:56
  • ok @zus Thanks, Oct 29, 2020 at 5:58
1

You've two __construct with same name. I don't know why would you need that, you can simple put both dependency injections in one constructor like shown below:

public function __construct(
       \Magento\Store\Model\StoreManagerInterface $storeManager,
       \Magento\Customer\Model\Session $customerSession
    ) {
       $this->_storeManager = $storeManager;
       $this->_customerSession = $customerSession;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.