SetCanApplyMsrpObserver.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\Msrp\Observer\Frontend\Quote;
  7. use Magento\Framework\Event\ObserverInterface;
  8. /**
  9. * Class SetCanApplyMsrp
  10. */
  11. class SetCanApplyMsrpObserver implements ObserverInterface
  12. {
  13. /**
  14. * @var \Magento\Msrp\Model\Config
  15. */
  16. protected $config;
  17. /**
  18. * @var \Magento\Msrp\Model\Quote\Address\CanApplyMsrp
  19. */
  20. protected $canApplyMsrp;
  21. /**
  22. * @var \Magento\Msrp\Model\Quote\Msrp
  23. */
  24. protected $msrp;
  25. /**
  26. * @param \Magento\Msrp\Model\Config $config
  27. * @param \Magento\Msrp\Model\Quote\Address\CanApplyMsrp $canApplyMsrp
  28. * @param \Magento\Msrp\Model\Quote\Msrp $msrp
  29. */
  30. public function __construct(
  31. \Magento\Msrp\Model\Config $config,
  32. \Magento\Msrp\Model\Quote\Address\CanApplyMsrp $canApplyMsrp,
  33. \Magento\Msrp\Model\Quote\Msrp $msrp
  34. ) {
  35. $this->config = $config;
  36. $this->canApplyMsrp = $canApplyMsrp;
  37. $this->msrp = $msrp;
  38. }
  39. /**
  40. * Set Quote information about MSRP price enabled
  41. *
  42. * @param \Magento\Framework\Event\Observer $observer
  43. * @return void
  44. */
  45. public function execute(\Magento\Framework\Event\Observer $observer)
  46. {
  47. /** @var $quote \Magento\Quote\Model\Quote */
  48. $quote = $observer->getEvent()->getQuote();
  49. $canApplyMsrp = false;
  50. if ($this->config->isEnabled()) {
  51. foreach ($quote->getAllAddresses() as $address) {
  52. if ($this->canApplyMsrp->isCanApplyMsrp($address)) {
  53. $canApplyMsrp = true;
  54. break;
  55. }
  56. }
  57. }
  58. $this->msrp->setCanApplyMsrp($quote->getId(), $canApplyMsrp);
  59. }
  60. }