Skip to content

Commit 9f37b2f

Browse files
author
Rani Priya
authored
item wise check added for shareable links
1 parent 98dd6c0 commit 9f37b2f

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Magento\Framework\Event\ObserverInterface;
99
use Magento\Store\Model\ScopeInterface;
1010

11-
/**
12-
* Check is allowed guest checkout for downloadable items.
13-
*/
1411
class IsAllowedGuestCheckoutObserver implements ObserverInterface
1512
{
1613
/**
@@ -21,7 +18,7 @@ class IsAllowedGuestCheckoutObserver implements ObserverInterface
2118
/**
2219
* Xml path to get downloadable Shareable setting
2320
*/
24-
private const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable';
21+
const XML_PATH_DOWNLOADABLE_SHAREABLE = 'catalog/downloadable/shareable';
2522

2623
/**
2724
* Core store config
@@ -30,13 +27,22 @@ class IsAllowedGuestCheckoutObserver implements ObserverInterface
3027
*/
3128
protected $_scopeConfig;
3229

30+
/**
31+
* Downloadable link collection factory
32+
*
33+
* @var \Magento\Downloadable\Model\ResourceModel\Link\CollectionFactory
34+
*/
35+
protected $_linksFactory;
36+
3337
/**
3438
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
3539
*/
3640
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
3843
) {
3944
$this->_scopeConfig = $scopeConfig;
45+
$this->_linksFactory = $linksFactory;
4046
}
4147

4248
/**
@@ -50,31 +56,52 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5056
$store = $observer->getEvent()->getStore();
5157
$result = $observer->getEvent()->getResult();
5258

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-
6659
/* @var $quote \Magento\Quote\Model\Quote */
6760
$quote = $observer->getEvent()->getQuote();
6861

6962
foreach ($quote->getAllItems() as $item) {
7063
if (($product = $item->getProduct())
7164
&& $product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
7265
) {
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+
}
7574
}
7675
}
77-
7876
return $this;
7977
}
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+
}
80107
}

0 commit comments

Comments
 (0)