AddPaymentWeeeItem.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Weee\Observer;
  7. use Magento\Framework\Event\Observer;
  8. use Magento\Framework\Event\ObserverInterface;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Magento\Weee\Helper\Data;
  11. /**
  12. * Add Weee item to Payment Cart amount.
  13. */
  14. class AddPaymentWeeeItem implements ObserverInterface
  15. {
  16. /**
  17. * @var Data
  18. */
  19. private $weeeData;
  20. /**
  21. * @var StoreManagerInterface
  22. */
  23. private $storeManager;
  24. /**
  25. * @param Data $weeeData
  26. * @param StoreManagerInterface $storeManager
  27. */
  28. public function __construct(
  29. Data $weeeData,
  30. StoreManagerInterface $storeManager
  31. ) {
  32. $this->weeeData = $weeeData;
  33. $this->storeManager = $storeManager;
  34. }
  35. /**
  36. * Add FPT amount as custom item to payment cart totals.
  37. *
  38. * @param Observer $observer
  39. * @return void
  40. */
  41. public function execute(Observer $observer)
  42. {
  43. if ($this->shouldBeAddedAsCustomItem() === false) {
  44. return;
  45. }
  46. /** @var \Magento\Payment\Model\Cart $cart */
  47. $cart = $observer->getEvent()->getCart();
  48. $salesEntity = $cart->getSalesModel();
  49. $totalWeee = 0;
  50. foreach ($salesEntity->getAllItems() as $item) {
  51. $originalItem = $item->getOriginalItem();
  52. if (!$originalItem->getParentItem()) {
  53. $totalWeee += $this->weeeData->getBaseWeeeTaxAppliedRowAmount($originalItem);
  54. }
  55. }
  56. if ($totalWeee > 0.0001) {
  57. $cart->addCustomItem(__('FPT'), 1, $totalWeee);
  58. }
  59. }
  60. /**
  61. * Checks if FPT should be added to payment cart as custom item or not.
  62. *
  63. * @return bool
  64. */
  65. private function shouldBeAddedAsCustomItem()
  66. {
  67. $storeId = $this->storeManager->getStore()->getId();
  68. return $this->weeeData->isEnabled($storeId) && $this->weeeData->includeInSubtotal($storeId) === false;
  69. }
  70. }