Skip to content

Commit de122d9

Browse files
ENGCOM-5629: fixed disabled guest checkout issue in case of downloadable product #23972
- Merge Pull Request #23972 from rani-webkul/magento2:23971 - Merged commits: 1. 548ea7b 2. 1cf1389 3. 65cf71b 4. a1dd986 5. c3e1ce0 6. caf9ef7 7. 98dd6c0 8. 9f37b2f 9. 993a1e5 10. 575d9e5 11. c343492 12. 4d045cd 13. 05a3348 14. 9e9bc69 15. 4380ff6 16. f4eb33e 17. 11f21bf 18. 59e7f33 19. 49e2fa9 20. 5389a71 21. 938a68c 22. 08e3875 23. 4c50949 24. af4b740 25. 592915d 26. 4dac91c 27. 4db0172 28. f1e11ea 29. 88778bd 30. 63ad4a3 31. 88953ac 32. 11e34b3
2 parents 8f42112 + 11e34b3 commit de122d9

File tree

5 files changed

+194
-45
lines changed

5 files changed

+194
-45
lines changed

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

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,140 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Downloadable\Observer;
78

9+
use Magento\Downloadable\Model\Link;
10+
use Magento\Downloadable\Model\Product\Type;
11+
use Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory as LinkCollectionFactory;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
use Magento\Framework\Event\Observer;
814
use Magento\Framework\Event\ObserverInterface;
15+
use Magento\Quote\Api\Data\CartItemInterface;
16+
use Magento\Quote\Model\Quote;
917
use Magento\Store\Model\ScopeInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
1019

20+
/**
21+
* Checks if guest checkout is allowed then quote contains downloadable products.
22+
*/
1123
class IsAllowedGuestCheckoutObserver implements ObserverInterface
1224
{
25+
private const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
26+
27+
private const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable';
28+
1329
/**
14-
* Xml path to disable checkout
30+
* @var ScopeConfigInterface
1531
*/
16-
const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
32+
private $scopeConfig;
1733

1834
/**
19-
* Core store config
35+
* Downloadable link collection factory
2036
*
21-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
37+
* @var LinkCollectionFactory
2238
*/
23-
protected $_scopeConfig;
39+
private $linkCollectionFactory;
2440

2541
/**
26-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
42+
* @var StoreManagerInterface
43+
*/
44+
private $storeManager;
45+
46+
/**
47+
* @param ScopeConfigInterface $scopeConfig
48+
* @param LinkCollectionFactory $linkCollectionFactory
49+
* @param StoreManagerInterface $storeManager
2750
*/
2851
public function __construct(
29-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
52+
ScopeConfigInterface $scopeConfig,
53+
LinkCollectionFactory $linkCollectionFactory,
54+
StoreManagerInterface $storeManager
3055
) {
31-
$this->_scopeConfig = $scopeConfig;
56+
$this->scopeConfig = $scopeConfig;
57+
$this->linkCollectionFactory = $linkCollectionFactory;
58+
$this->storeManager = $storeManager;
3259
}
3360

3461
/**
3562
* Check is allowed guest checkout if quote contain downloadable product(s)
3663
*
37-
* @param \Magento\Framework\Event\Observer $observer
64+
* @param Observer $observer
3865
* @return $this
3966
*/
40-
public function execute(\Magento\Framework\Event\Observer $observer)
67+
public function execute(Observer $observer)
4168
{
42-
$store = $observer->getEvent()->getStore();
69+
$storeId = (int)$this->storeManager->getStore($observer->getEvent()->getStore())->getId();
4370
$result = $observer->getEvent()->getResult();
4471

45-
if (!$this->_scopeConfig->isSetFlag(
72+
/* @var $quote Quote */
73+
$quote = $observer->getEvent()->getQuote();
74+
$isGuestCheckoutDisabled = $this->scopeConfig->isSetFlag(
4675
self::XML_PATH_DISABLE_GUEST_CHECKOUT,
4776
ScopeInterface::SCOPE_STORE,
48-
$store
49-
)) {
50-
return $this;
51-
}
52-
53-
/* @var $quote \Magento\Quote\Model\Quote */
54-
$quote = $observer->getEvent()->getQuote();
77+
$storeId
78+
);
5579

5680
foreach ($quote->getAllItems() as $item) {
57-
if (($product = $item->getProduct())
58-
&& $product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
59-
) {
60-
$result->setIsAllowed(false);
61-
break;
81+
$product = $item->getProduct();
82+
83+
if ((string)$product->getTypeId() === Type::TYPE_DOWNLOADABLE) {
84+
if ($isGuestCheckoutDisabled || !$this->checkForShareableLinks($item, $storeId)) {
85+
$result->setIsAllowed(false);
86+
break;
87+
}
6288
}
6389
}
6490

6591
return $this;
6692
}
93+
94+
/**
95+
* Check for shareable link
96+
*
97+
* @param CartItemInterface $item
98+
* @param int $storeId
99+
* @return boolean
100+
*/
101+
private function checkForShareableLinks(CartItemInterface $item, int $storeId): bool
102+
{
103+
$isSharable = true;
104+
$option = $item->getOptionByCode('downloadable_link_ids');
105+
106+
if (!empty($option)) {
107+
$downloadableLinkIds = explode(',', $option->getValue());
108+
109+
$linkCollection = $this->linkCollectionFactory->create();
110+
$linkCollection->addFieldToFilter('link_id', ['in' => $downloadableLinkIds]);
111+
$linkCollection->addFieldToFilter('is_shareable', ['in' => $this->getNotSharableValues($storeId)]);
112+
113+
// We don't have not sharable links
114+
$isSharable = $linkCollection->getSize() === 0;
115+
}
116+
117+
return $isSharable;
118+
}
119+
120+
/**
121+
* Returns not sharable values depending on configuration
122+
*
123+
* @param int $storeId
124+
* @return array
125+
*/
126+
private function getNotSharableValues(int $storeId): array
127+
{
128+
$configIsSharable = $this->scopeConfig->isSetFlag(
129+
self::XML_PATH_DOWNLOADABLE_SHAREABLE,
130+
ScopeInterface::SCOPE_STORE,
131+
$storeId
132+
);
133+
134+
$notShareableValues = [Link::LINK_SHAREABLE_NO];
135+
136+
if (!$configIsSharable) {
137+
$notShareableValues[] = Link::LINK_SHAREABLE_CONFIG;
138+
}
139+
140+
return $notShareableValues;
141+
}
67142
}

app/code/Magento/Downloadable/Test/Mftf/Data/CatalogConfigData.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
<data key="scope_id">0</data>
1919
<data key="value">1</data>
2020
</entity>
21+
<entity name="EnableShareableDownloadableItems">
22+
<data key="path">catalog/downloadable/shareable</data>
23+
<data key="scope_id">0</data>
24+
<data key="value">1</data>
25+
</entity>
2126
</entities>

app/code/Magento/Downloadable/Test/Mftf/Data/LinkData.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@
3131
<data key="file">magento-logo.png</data>
3232
<data key="sample">https://static.magento.com/sites/all/themes/mag_redesign/images/magento-logo.svg</data>
3333
</entity>
34+
<entity name="downloadableLinkSharable" type="downloadable_link">
35+
<data key="title" unique="suffix">link-1</data>
36+
<data key="price">2.43</data>
37+
<data key="number_of_downloads">2</data>
38+
<data key="sample_type">url</data>
39+
<data key="sample_url">http://example.com</data>
40+
<data key="link_type">url</data>
41+
<data key="link_url">http://example.com</data>
42+
<data key="is_shareable">1</data>
43+
<data key="sort_order">1</data>
44+
</entity>
3445
<entity name="downloadableLink1" type="downloadable_link">
3546
<data key="title" unique="suffix">link-1</data>
3647
<data key="price">2.43</data>

app/code/Magento/Downloadable/Test/Mftf/Test/LinkDownloadableProductFromGuestToCustomerTest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
<before>
2121
<magentoCLI stepKey="addDownloadableDomain" command="downloadable:domains:add example.com static.magento.com"/>
2222
<magentoCLI command="config:set {{EnableGuestCheckoutWithDownloadableItems.path}} {{EnableGuestCheckoutWithDownloadableItems.value}}" stepKey="enableGuestCheckoutWithDownloadableItems" />
23+
<magentoCLI command="config:set {{EnableShareableDownloadableItems.path}} {{EnableShareableDownloadableItems.value}}" stepKey="enableShareableDownloadableItems" />
2324
<createData entity="_defaultCategory" stepKey="createCategory"/>
2425
<createData entity="DownloadableProductWithOneLink" stepKey="createProduct">
2526
<requiredEntity createDataKey="createCategory"/>
2627
</createData>
27-
<createData entity="downloadableLink1" stepKey="addDownloadableLink">
28+
<createData entity="downloadableLinkSharable" stepKey="addDownloadableLink">
2829
<requiredEntity createDataKey="createProduct"/>
2930
</createData>
3031
</before>

0 commit comments

Comments
 (0)