0

Magento 2 Error Invalid Method Interceptor::loadByCustomer

enter image description here

{"0":"M\u00e9todo inv\u00e1lido Vendor\Module\Model\Subscriber\Interceptor::loadByCustomer","1":"#1 Vendor\Module\Observer\Login->execute() called at [vendor/magento/framework/Event/Invoker/InvokerDefault.php:88]\n#2 Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod() called at [vendor/magento/framework/Event/Invoker/InvokerDefault.php:74]\n#3 Magento\Framework\Event\Invoker\InvokerDefault->dispatch()

Code:

<?php
namespace Vendor\Module\Observer;

use Magento\Customer\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Newsletter\Model\Subscriber;
use Magento\Store\Model\StoreManagerInterface;

class Login implements ObserverInterface
{
    /**
     * @var Session
     */
    private $session;
    /**
     * @var Subscriber
     */
    private $subscriber;
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;

    public function __construct(
        Session $session,
        Subscriber $subscriber,
        StoreManagerInterface $storeManager
    )
    {
        $this->session = $session;
        $this->subscriber = $subscriber;
        $this->storeManager = $storeManager;
    }

    public function execute(Observer $observer)
    {
        if($this->session->isLoggedIn()) {
                $checkSubscriber = $this->subscriber->loadByCustomer($this->session->getCustomerId(), $this->storeManager->getStore()->getWebsiteId());
                if($checkSubscriber->getSubscriberStatus() === 1) {
                    ...
                }
        }
    }
}

1 Answer 1

1

This is the solution, change your code:

Change loadByCustomer to loadByCustomerId

<?php

declare(strict_types=1);

namespace Vendor\Module\Observer;

use Magento\Customer\Model\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Newsletter\Model\Subscriber;

use Psr\Log\LoggerInterface;

class Login implements ObserverInterface
{
    /**
     * @var Session
     */
    private $session;

    /**
     * @var Subscriber
     */
    private $subscriber;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @param Session $session
     * @param Subscriber $subscriber
     * @param LoggerInterface $logger
     */
    public function __construct(
        Session $session,
        Subscriber $subscriber,
        LoggerInterface $logger
    )
    {
        $this->session = $session;
        $this->subscriber = $subscriber;
        $this->logger = $logger;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        if($this->session->isLoggedIn()) {
                $customerId = $this->session->getCustomerId();
                if ($customerId === null) {
                    $this->logger->warning(__('Something went wrong customerId.'));
                } else {
                    try {
                        $subscriber = $this->subscriber->loadByCustomerId((int)$customerId);
                        if($subscriber->getSubscriberStatus() === 1) {
                        ...
                        }
                    } catch (\Exception $e) {
                        ...
                    }
                }
        }
    }

}

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.