8

How to override or Use plugin for isWebsiteScope() function from Magento\Customer\Model\Config\Share.php file,

I have used plugin for below function but I want to get current customer session in below function, Mainly check custoemr is logged in or not.

Just use plugin and set di.xml,

<!-- Override Share.php to set value of website scope -->
<type name="Magento\Customer\Model\Config\Share">
    <plugin name="Company_Modulename::Share" type="\Company\Modulename\Plugin\Config\Share\Proxy" sortOrder="1"/>
</type>

In Share.php file,

<?php
namespace Company\Modulename\Plugin\Config;

class Share
{    
    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->session = $customerSession;
    }
    /**
     * Check whether current customers sharing scope is website
     *
     * @return bool
     */
    public function afterIsWebsiteScope(\Magento\Customer\Model\Config\Share $subject)
    {
        if(!$this->session->isLoggedIn()){
            return 1;
        } else {
            return 0;
        }

    }
}

How to get current customer session in above function,

If I used current customer session using customer session model browser will throw Circular dependacy error.

  • Where can be duplicate this issue frontend or admin ? – zed Blackbeard Apr 2 '18 at 14:47
  • You cannot get the customer session using the above pattern because Magento\Customer\Model\Config\Share is a dependency of Magento\Customer\Model\Session, hence the circular dependency error -- I see what you are trying to do (i.e. change the website scope if the customer is logged in) but couldn't figure out a non-hacky way either. – Phil Birnie Apr 3 '18 at 3:55
  • @zedBlackbeard, For frontend. – Rakesh Jesadiya Apr 3 '18 at 4:49
  • 1
    @RakeshJesadiya , Lastly I used object Manager to solve this issue, Even I am open for better answer – Murtuza Zabuawala Apr 3 '18 at 4:56
  • 1
    Yes as @MurtuzaZabuawala You need to use object manager for this issue. But use object manager as dependency \Magento\Framework\ObjectManagerInterface – Prince Patel Apr 3 '18 at 5:22
4

The easiest way to resolve circular dependencies (though not always the best) is to use Proxies.

Proxy for a class is an auto-generated class (meaning, Magento will create it automatically if not found through auto-loader) which has a dependency on the Object Manager and has the same interface as the original class.

Implementation of any method of the Proxy is just an invocation of the same method of the original class. It does not have original class required in constructor, instead the object of the original class is created through the Object Manager the first time some method of the Proxy is invoked.

Here is an example of the Proxy from the core (\Magento\Backend\Model\Auth\Proxy):

/**
 * Get proxied instance
 *
 * @return \Magento\Backend\Model\Auth
 */
protected function _getSubject()
{
    if (!$this->_subject) {
        $this->_subject = true === $this->_isShared
            ? $this->_objectManager->get($this->_instanceName)
            : $this->_objectManager->create($this->_instanceName);
    }
    return $this->_subject;
}

/**
 * {@inheritdoc}
 */
public function setAuthStorage($storage)
{
    return $this->_getSubject()->setAuthStorage($storage);
}

To use a proxy, instead of reference to the class (anywhere in di.xml or in constructor dependency), reference the Proxy by appending the name of the class with \Proxy, i.e.:

<type name="Magento\Customer\Model\Config\Share">
<plugin name="Company_Modulename::Share" type="\Company\Modulename\Plugin\Config\Share\Proxy" sortOrder="1"/>

It is always worthwhile analyzing are there ways to rewrite the business logic to avoid circular dependencies before using Proxies (or for the same matter an Object Manager in constructor)

  • How to get customer session in proxy.php file using above way? Still same error coming – Rakesh Jesadiya Apr 3 '18 at 11:22
  • @RakeshJesadiya just keep the plugin the way you wrote it, but change di.xml and specify the plugin type with the \Proxy suffix (as in my example above). Do not create the Proxy class manually. – Eugene Tulika Apr 3 '18 at 12:08
  • Given same error again using above way. Circular dependency: Magento\Customer\Model\Session depends on Company\Modulename\Plugin\Config\Share and vice versa. – Rakesh Jesadiya Apr 3 '18 at 12:23
  • I have updated my questions based on your comment but not working – Rakesh Jesadiya Apr 3 '18 at 12:28
  • I see, the problem is that the method isWebsiteScope is invoked from the constructor of the Magento\Customer\Model\Session\Storage, that's why even though we used Proxy, object still need to be created at the construction time. I don't see how Object Manager helps your either (doesn't work for me). I wold rather look more deeply at what business logic you want to implement and add it on a different level. – Eugene Tulika Apr 3 '18 at 13:11
1

The only way That I found to remove circular dependency is to use objectManager directly ,

I know that is ugly way to solve , but I think this is the only way,

If some one has better solution Then I am also open for solution

But my way below is the solution :

protected $_objectManager;

public function __construct( 
    \Magento\Framework\ObjectManagerInterface $objectManager
)
{
    $this->_objectManager = $objectManager;
}

public function afterIsWebsiteScope(\Magento\Customer\Model\Config\Share $subject)
{
    $customerSession = $this->_objectManager->get('Magento\Customer\Model\Session');
    if(!$customerSession->isLoggedIn()){
        return 1;
    } else {
        return 0;
    }

}
  • 1
    Yes, I also think that this is the only way to solve circular dependency but we should use object manager as a dependency in class files. +1 from me :) – Prince Patel Apr 3 '18 at 5:26
  • Its not working yet and show same error. – Rakesh Jesadiya Apr 3 '18 at 5:33
  • @RakeshJesadiya did you remove Magento\Customer\Model\Session from constructor ?? and delete var/generation – Murtuza Zabuawala Apr 3 '18 at 5:39
  • Yes, I have tried it. remove generated and var folder from root – Rakesh Jesadiya Apr 3 '18 at 5:43
  • now error is look like,Uncaught Error: Call to a member function get() on null in /var/www/html/studentkare/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:144 – Rakesh Jesadiya Apr 3 '18 at 7:18

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.