|
3 | 3 | * Copyright © Magento, Inc. All rights reserved. |
4 | 4 | * See COPYING.txt for license details. |
5 | 5 | */ |
| 6 | + |
6 | 7 | namespace Magento\Downloadable\Observer; |
7 | 8 |
|
| 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; |
8 | 14 | use Magento\Framework\Event\ObserverInterface; |
| 15 | +use Magento\Quote\Api\Data\CartItemInterface; |
| 16 | +use Magento\Quote\Model\Quote; |
9 | 17 | use Magento\Store\Model\ScopeInterface; |
| 18 | +use Magento\Store\Model\StoreManagerInterface; |
10 | 19 |
|
| 20 | +/** |
| 21 | + * Checks if guest checkout is allowed then quote contains downloadable products. |
| 22 | + */ |
11 | 23 | class IsAllowedGuestCheckoutObserver implements ObserverInterface |
12 | 24 | { |
| 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 | + |
13 | 29 | /** |
14 | | - * Xml path to disable checkout |
| 30 | + * @var ScopeConfigInterface |
15 | 31 | */ |
16 | | - const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout'; |
| 32 | + private $scopeConfig; |
17 | 33 |
|
18 | 34 | /** |
19 | | - * Core store config |
| 35 | + * Downloadable link collection factory |
20 | 36 | * |
21 | | - * @var \Magento\Framework\App\Config\ScopeConfigInterface |
| 37 | + * @var LinkCollectionFactory |
22 | 38 | */ |
23 | | - protected $_scopeConfig; |
| 39 | + private $linkCollectionFactory; |
24 | 40 |
|
25 | 41 | /** |
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 |
27 | 50 | */ |
28 | 51 | public function __construct( |
29 | | - \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
| 52 | + ScopeConfigInterface $scopeConfig, |
| 53 | + LinkCollectionFactory $linkCollectionFactory, |
| 54 | + StoreManagerInterface $storeManager |
30 | 55 | ) { |
31 | | - $this->_scopeConfig = $scopeConfig; |
| 56 | + $this->scopeConfig = $scopeConfig; |
| 57 | + $this->linkCollectionFactory = $linkCollectionFactory; |
| 58 | + $this->storeManager = $storeManager; |
32 | 59 | } |
33 | 60 |
|
34 | 61 | /** |
35 | 62 | * Check is allowed guest checkout if quote contain downloadable product(s) |
36 | 63 | * |
37 | | - * @param \Magento\Framework\Event\Observer $observer |
| 64 | + * @param Observer $observer |
38 | 65 | * @return $this |
39 | 66 | */ |
40 | | - public function execute(\Magento\Framework\Event\Observer $observer) |
| 67 | + public function execute(Observer $observer) |
41 | 68 | { |
42 | | - $store = $observer->getEvent()->getStore(); |
| 69 | + $storeId = (int)$this->storeManager->getStore($observer->getEvent()->getStore())->getId(); |
43 | 70 | $result = $observer->getEvent()->getResult(); |
44 | 71 |
|
45 | | - if (!$this->_scopeConfig->isSetFlag( |
| 72 | + /* @var $quote Quote */ |
| 73 | + $quote = $observer->getEvent()->getQuote(); |
| 74 | + $isGuestCheckoutDisabled = $this->scopeConfig->isSetFlag( |
46 | 75 | self::XML_PATH_DISABLE_GUEST_CHECKOUT, |
47 | 76 | 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 | + ); |
55 | 79 |
|
56 | 80 | 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 | + } |
62 | 88 | } |
63 | 89 | } |
64 | 90 |
|
65 | 91 | return $this; |
66 | 92 | } |
| 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 | + } |
67 | 142 | } |
0 commit comments