IsAllowedGuestCheckoutObserver.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. use Magento\Store\Model\ScopeInterface;
  9. class IsAllowedGuestCheckoutObserver implements ObserverInterface
  10. {
  11. /**
  12. * Xml path to disable checkout
  13. */
  14. const XML_PATH_DISABLE_GUEST_CHECKOUT = 'catalog/downloadable/disable_guest_checkout';
  15. /**
  16. * Core store config
  17. *
  18. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  19. */
  20. protected $_scopeConfig;
  21. /**
  22. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  23. */
  24. public function __construct(
  25. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  26. ) {
  27. $this->_scopeConfig = $scopeConfig;
  28. }
  29. /**
  30. * Check is allowed guest checkout if quote contain downloadable product(s)
  31. *
  32. * @param \Magento\Framework\Event\Observer $observer
  33. * @return $this
  34. */
  35. public function execute(\Magento\Framework\Event\Observer $observer)
  36. {
  37. $store = $observer->getEvent()->getStore();
  38. $result = $observer->getEvent()->getResult();
  39. if (!$this->_scopeConfig->isSetFlag(
  40. self::XML_PATH_DISABLE_GUEST_CHECKOUT,
  41. ScopeInterface::SCOPE_STORE,
  42. $store
  43. )) {
  44. return $this;
  45. }
  46. /* @var $quote \Magento\Quote\Model\Quote */
  47. $quote = $observer->getEvent()->getQuote();
  48. foreach ($quote->getAllItems() as $item) {
  49. if (($product = $item->getProduct())
  50. && $product->getTypeId() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
  51. ) {
  52. $result->setIsAllowed(false);
  53. break;
  54. }
  55. }
  56. return $this;
  57. }
  58. }