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