Skip to content

Commit f1e11ea

Browse files
authored
#23972 fix disabled guest checkout issue
Optimize code
1 parent 4db0172 commit f1e11ea

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

app/code/Magento/Downloadable/Observer/IsAllowedGuestCheckoutObserver.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace Magento\Downloadable\Observer;
88

9+
use Magento\Downloadable\Model\Link;
910
use Magento\Downloadable\Model\Product\Type;
10-
use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory;
11+
use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory as LinkCollectionFactory;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
1213
use Magento\Framework\Event\Observer;
1314
use Magento\Framework\Event\ObserverInterface;
@@ -32,17 +33,17 @@ class IsAllowedGuestCheckoutObserver implements ObserverInterface
3233
/**
3334
* Downloadable link collection factory
3435
*
35-
* @var CollectionFactory
36+
* @var LinkCollectionFactory
3637
*/
37-
private $linksFactory;
38+
private $linkCollectionFactory;
3839

3940
/**
4041
* @param ScopeConfigInterface $scopeConfig
41-
* @param CollectionFactory $linksFactory
42+
* @param LinkCollectionFactory $linkCollectionFactory
4243
*/
43-
public function __construct(ScopeConfigInterface $scopeConfig, CollectionFactory $linksFactory) {
44+
public function __construct(ScopeConfigInterface $scopeConfig, LinkCollectionFactory $linkCollectionFactory) {
4445
$this->scopeConfig = $scopeConfig;
45-
$this->linksFactory = $linksFactory;
46+
$this->linkCollectionFactory = $linkCollectionFactory;
4647
}
4748

4849
/**
@@ -66,7 +67,7 @@ public function execute(Observer $observer)
6667

6768
foreach ($quote->getAllItems() as $item) {
6869
$product = $item->getProduct();
69-
70+
7071
if ((string)$product->getTypeId() === Type::TYPE_DOWNLOADABLE) {
7172
if ($isGuestCheckoutDisabled || !$this->checkForShareableLinks($item, $storeId)) {
7273
$result->setIsAllowed(false);
@@ -92,25 +93,36 @@ private function checkForShareableLinks(CartItemInterface $item, int $storeId):
9293

9394
if (!empty($option)) {
9495
$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;
112103
}
113104

114105
return $isSharable;
115106
}
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+
}
116128
}

0 commit comments

Comments
 (0)