1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Wishlist\Controller\Index;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\RedirectInterface;
class Plugin
{
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var \Magento\Wishlist\Model\AuthenticationStateInterface
*/
protected $authenticationState;
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $config;
/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirector;
/**
* @var \Magento\Framework\Message\ManagerInterface
*/
private $messageManager;
/**
* @param CustomerSession $customerSession
* @param \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState
* @param ScopeConfigInterface $config
* @param RedirectInterface $redirector
* @param \Magento\Framework\Message\ManagerInterface $messageManager
*/
public function __construct(
CustomerSession $customerSession,
\Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState,
ScopeConfigInterface $config,
RedirectInterface $redirector,
\Magento\Framework\Message\ManagerInterface $messageManager
) {
$this->customerSession = $customerSession;
$this->authenticationState = $authenticationState;
$this->config = $config;
$this->redirector = $redirector;
$this->messageManager = $messageManager;
}
/**
* Perform customer authentication and wishlist feature state checks
*
* @param \Magento\Framework\App\ActionInterface $subject
* @param RequestInterface $request
* @return void
* @throws \Magento\Framework\Exception\NotFoundException
*/
public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
{
if ($this->authenticationState->isEnabled() && !$this->customerSession->authenticate()) {
$subject->getActionFlag()->set('', 'no-dispatch', true);
if (!$this->customerSession->getBeforeWishlistUrl()) {
$this->customerSession->setBeforeWishlistUrl($this->redirector->getRefererUrl());
}
$this->customerSession->setBeforeWishlistRequest($request->getParams());
$this->customerSession->setBeforeRequestParams($this->customerSession->getBeforeWishlistRequest());
$this->customerSession->setBeforeModuleName('wishlist');
$this->customerSession->setBeforeControllerName('index');
$this->customerSession->setBeforeAction('add');
if ($request->getActionName() == 'add') {
$this->messageManager->addErrorMessage(__('You must login or register to add items to your wishlist.'));
}
}
if (!$this->config->isSetFlag('wishlist/general/active')) {
throw new NotFoundException(__('Page not found.'));
}
}
}