6
6
7
7
namespace Magento \Downloadable \Observer ;
8
8
9
+ use Magento \Downloadable \Model \Link ;
9
10
use Magento \Downloadable \Model \Product \Type ;
10
- use Magento \Downloadable \Model \ResourceModel \Link \CollectionFactory ;
11
+ use Magento \Downloadable \Model \ResourceModel \Link \CollectionFactory as LinkCollectionFactory ;
11
12
use Magento \Framework \App \Config \ScopeConfigInterface ;
12
13
use Magento \Framework \Event \Observer ;
13
14
use Magento \Framework \Event \ObserverInterface ;
@@ -32,17 +33,17 @@ class IsAllowedGuestCheckoutObserver implements ObserverInterface
32
33
/**
33
34
* Downloadable link collection factory
34
35
*
35
- * @var CollectionFactory
36
+ * @var LinkCollectionFactory
36
37
*/
37
- private $ linksFactory ;
38
+ private $ linkCollectionFactory ;
38
39
39
40
/**
40
41
* @param ScopeConfigInterface $scopeConfig
41
- * @param CollectionFactory $linksFactory
42
+ * @param LinkCollectionFactory $linkCollectionFactory
42
43
*/
43
- public function __construct (ScopeConfigInterface $ scopeConfig , CollectionFactory $ linksFactory ) {
44
+ public function __construct (ScopeConfigInterface $ scopeConfig , LinkCollectionFactory $ linkCollectionFactory ) {
44
45
$ this ->scopeConfig = $ scopeConfig ;
45
- $ this ->linksFactory = $ linksFactory ;
46
+ $ this ->linkCollectionFactory = $ linkCollectionFactory ;
46
47
}
47
48
48
49
/**
@@ -66,7 +67,7 @@ public function execute(Observer $observer)
66
67
67
68
foreach ($ quote ->getAllItems () as $ item ) {
68
69
$ product = $ item ->getProduct ();
69
-
70
+
70
71
if ((string )$ product ->getTypeId () === Type::TYPE_DOWNLOADABLE ) {
71
72
if ($ isGuestCheckoutDisabled || !$ this ->checkForShareableLinks ($ item , $ storeId )) {
72
73
$ result ->setIsAllowed (false );
@@ -92,25 +93,36 @@ private function checkForShareableLinks(CartItemInterface $item, int $storeId):
92
93
93
94
if (!empty ($ option )) {
94
95
$ downloadableLinkIds = explode (', ' , $ option ->getValue ());
95
- $ links = $ this ->linksFactory ->create ()->addFieldToFilter ("link_id " , ["in " => $ downloadableLinkIds ]);
96
-
97
- $ configDownloadableSharable = $ this ->scopeConfig ->isSetFlag (
98
- self ::XML_PATH_DOWNLOADABLE_SHAREABLE ,
99
- ScopeInterface::SCOPE_STORE ,
100
- $ storeId
101
- );
102
-
103
- foreach ($ links as $ link ) {
104
- if (!$ link ->getIsShareable () ||
105
- //Use config default value and it's disabled in config
106
- ((int )$ link ->getIsShareable () === 2 && !$ configDownloadableSharable )
107
- ) {
108
- $ isSharable = false ;
109
- break ;
110
- }
111
- }
96
+
97
+ $ linkCollection = $ this ->linkCollectionFactory ->create ();
98
+ $ linkCollection ->addFieldToFilter ('link_id ' , ['in ' => $ downloadableLinkIds ]);
99
+ $ linkCollection ->addFieldToFilter ('is_shareable ' , ['in ' => $ this ->getNotSharableValues ($ storeId )]);
100
+
101
+ // We don't have not sharable links
102
+ $ isSharable = $ linkCollection ->getSize () === 0 ;
112
103
}
113
104
114
105
return $ isSharable ;
115
106
}
107
+
108
+ /**
109
+ * @param int $storeId
110
+ * @return array
111
+ */
112
+ private function getNotSharableValues (int $ storeId ): array
113
+ {
114
+ $ configIsSharable = $ this ->scopeConfig ->isSetFlag (
115
+ self ::XML_PATH_DOWNLOADABLE_SHAREABLE ,
116
+ ScopeInterface::SCOPE_STORE ,
117
+ $ storeId
118
+ );
119
+
120
+ $ notShareableValues = [Link::LINK_SHAREABLE_NO ];
121
+
122
+ if (!$ configIsSharable ) {
123
+ $ notShareableValues [] = Link::LINK_SHAREABLE_CONFIG ;
124
+ }
125
+
126
+ return $ notShareableValues ;
127
+ }
116
128
}
0 commit comments