3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
namespace Magento \Downloadable \Observer ;
7
8
9
+ use Magento \Downloadable \Model \Product \Type ;
10
+ use Magento \Downloadable \Model \ResourceModel \Link \CollectionFactory ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \Event \Observer ;
8
13
use Magento \Framework \Event \ObserverInterface ;
14
+ use Magento \Quote \Api \Data \CartItemInterface ;
15
+ use Magento \Quote \Model \Quote ;
9
16
use Magento \Store \Model \ScopeInterface ;
10
17
18
+ /**
19
+ * Checks if guest checkout is allowed then quote contains downloadable products.
20
+ */
11
21
class IsAllowedGuestCheckoutObserver implements ObserverInterface
12
22
{
13
23
/**
14
24
* Xml path to disable checkout
15
25
*/
16
- const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout ' ;
26
+ private const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout ' ;
17
27
18
28
/**
19
29
* Xml path to get downloadable Shareable setting
20
30
*/
21
- const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable ' ;
31
+ private const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable ' ;
22
32
23
33
/**
24
34
* Core store config
25
35
*
26
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
36
+ * @var ScopeConfigInterface
27
37
*/
28
- protected $ _scopeConfig ;
38
+ private $ scopeConfig ;
29
39
30
40
/**
31
41
* Downloadable link collection factory
32
42
*
33
- * @var \Magento\Downloadable\Model\ResourceModel\Link\ CollectionFactory
43
+ * @var CollectionFactory
34
44
*/
35
- protected $ _linksFactory ;
45
+ private $ linksFactory ;
36
46
37
47
/**
38
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
48
+ * @param ScopeConfigInterface $scopeConfig
49
+ * @param CollectionFactory $linksFactory
39
50
*/
40
51
public function __construct (
41
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig ,
42
- \ Magento \ Downloadable \ Model \ ResourceModel \ Link \ CollectionFactory $ linksFactory
52
+ ScopeConfigInterface $ scopeConfig ,
53
+ CollectionFactory $ linksFactory
43
54
) {
44
- $ this ->_scopeConfig = $ scopeConfig ;
45
- $ this ->_linksFactory = $ linksFactory ;
55
+ $ this ->scopeConfig = $ scopeConfig ;
56
+ $ this ->linksFactory = $ linksFactory ;
46
57
}
47
58
48
59
/**
49
60
* Check is allowed guest checkout if quote contain downloadable product(s)
50
61
*
51
- * @param \Magento\Framework\Event\ Observer $observer
62
+ * @param Observer $observer
52
63
* @return $this
53
64
*/
54
- public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
65
+ public function execute (Observer $ observer )
55
66
{
56
67
$ store = $ observer ->getEvent ()->getStore ();
57
68
$ result = $ observer ->getEvent ()->getResult ();
58
69
59
- /* @var $quote \Magento\Quote\Model\ Quote */
70
+ /* @var $quote Quote */
60
71
$ quote = $ observer ->getEvent ()->getQuote ();
61
72
62
73
foreach ($ quote ->getAllItems () as $ item ) {
63
74
if (($ product = $ item ->getProduct ())
64
- && $ product ->getTypeId () == \ Magento \ Downloadable \ Model \ Product \ Type::TYPE_DOWNLOADABLE
75
+ && $ product ->getTypeId () == Type::TYPE_DOWNLOADABLE
65
76
) {
66
- if ($ this ->_scopeConfig ->isSetFlag (
77
+ if ($ this ->scopeConfig ->isSetFlag (
67
78
self ::XML_PATH_DISABLE_GUEST_CHECKOUT ,
68
79
ScopeInterface::SCOPE_STORE ,
69
80
$ store
70
- ) || !$ this ->checkForShareableLinks ($ item )) {
81
+ )
82
+ || !$ this ->checkForShareableLinks ($ item , $ store )) {
71
83
$ result ->setIsAllowed (false );
72
84
break ;
73
85
}
74
86
}
75
87
}
88
+
76
89
return $ this ;
77
90
}
78
91
79
92
/**
80
93
* Check for shareable link
81
94
*
82
- * @param \Magento\Quote\Api\Data\CartItemInterface $item
95
+ * @param CartItemInterface $item
96
+ * @param int $store
83
97
* @return boolean
84
98
*/
85
- private function checkForShareableLinks ($ item)
99
+ private function checkForShareableLinks (CartItemInterface $ item, int $ store ): bool
86
100
{
87
101
$ isSharable = true ;
88
102
$ option = $ item ->getOptionByCode ('downloadable_link_ids ' );
@@ -91,17 +105,19 @@ private function checkForShareableLinks($item)
91
105
$ links = $ this ->linksFactory ->create ()->addFieldToFilter ("link_id " , ["in " => $ downloadableLinkIds ]);
92
106
foreach ($ links as $ link ) {
93
107
if (!$ link ->getIsShareable () ||
94
- ($ link ->getIsShareable () == 2 && !$ this ->_scopeConfig ->isSetFlag (
95
- self ::XML_PATH_DOWNLOADABLE_SHAREABLE ,
96
- ScopeInterface::SCOPE_STORE ,
97
- $ store
98
- )
108
+ (
109
+ $ link ->getIsShareable () == 2 && !$ this ->scopeConfig ->isSetFlag (
110
+ self ::XML_PATH_DOWNLOADABLE_SHAREABLE ,
111
+ ScopeInterface::SCOPE_STORE ,
112
+ $ store
113
+ )
99
114
)
100
115
) {
101
116
$ isSharable = false ;
102
117
}
103
118
}
104
119
}
120
+
105
121
return $ isSharable ;
106
122
}
107
123
}
0 commit comments