SetHasDownloadableProductsObserver.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. class SetHasDownloadableProductsObserver implements ObserverInterface
  9. {
  10. /**
  11. * @var \Magento\Checkout\Model\Session
  12. */
  13. protected $_checkoutSession;
  14. /**
  15. * @param \Magento\Checkout\Model\Session $checkoutSession
  16. */
  17. public function __construct(
  18. \Magento\Checkout\Model\Session $checkoutSession
  19. ) {
  20. $this->_checkoutSession = $checkoutSession;
  21. }
  22. /**
  23. * Set checkout session flag if order has downloadable product(s)
  24. *
  25. * @param \Magento\Framework\Event\Observer $observer
  26. * @return $this
  27. */
  28. public function execute(\Magento\Framework\Event\Observer $observer)
  29. {
  30. if (!$this->_checkoutSession->getHasDownloadableProducts()) {
  31. $order = $observer->getEvent()->getOrder();
  32. foreach ($order->getAllItems() as $item) {
  33. /* @var $item \Magento\Sales\Model\Order\Item */
  34. if ($item->getProductType() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
  35. || $item->getRealProductType() == \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE
  36. || $item->getProductOptionByCode(
  37. 'is_downloadable'
  38. )
  39. ) {
  40. $this->_checkoutSession->setHasDownloadableProducts(true);
  41. break;
  42. }
  43. }
  44. }
  45. return $this;
  46. }
  47. }