ContextPlugin.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\App\Action;
  7. /**
  8. * Class ContextPlugin
  9. */
  10. class ContextPlugin
  11. {
  12. /**
  13. * @var \Magento\Customer\Model\Session
  14. */
  15. protected $customerSession;
  16. /**
  17. * @var \Magento\Framework\App\Http\Context
  18. */
  19. protected $httpContext;
  20. /**
  21. * @var \Magento\Tax\Helper\Data
  22. */
  23. protected $taxHelper;
  24. /**
  25. * @var \Magento\Tax\Model\Calculation\Proxy
  26. */
  27. protected $taxCalculation;
  28. /**
  29. * Module manager
  30. *
  31. * @var \Magento\Framework\Module\Manager
  32. */
  33. private $moduleManager;
  34. /**
  35. * Cache config
  36. *
  37. * @var \Magento\PageCache\Model\Config
  38. */
  39. private $cacheConfig;
  40. /**
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Framework\App\Http\Context $httpContext
  43. * @param \Magento\Tax\Model\Calculation\Proxy $calculation
  44. * @param \Magento\Tax\Helper\Data $taxHelper
  45. * @param \Magento\Framework\Module\Manager $moduleManager
  46. * @param \Magento\PageCache\Model\Config $cacheConfig
  47. */
  48. public function __construct(
  49. \Magento\Customer\Model\Session $customerSession,
  50. \Magento\Framework\App\Http\Context $httpContext,
  51. \Magento\Tax\Model\Calculation\Proxy $calculation,
  52. \Magento\Tax\Helper\Data $taxHelper,
  53. \Magento\Framework\Module\Manager $moduleManager,
  54. \Magento\PageCache\Model\Config $cacheConfig
  55. ) {
  56. $this->customerSession = $customerSession;
  57. $this->httpContext = $httpContext;
  58. $this->taxCalculation = $calculation;
  59. $this->taxHelper = $taxHelper;
  60. $this->moduleManager = $moduleManager;
  61. $this->cacheConfig = $cacheConfig;
  62. }
  63. /**
  64. * @param \Magento\Framework\App\ActionInterface $subject
  65. * @param \Magento\Framework\App\RequestInterface $request
  66. * @return mixed
  67. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  68. */
  69. public function beforeDispatch(
  70. \Magento\Framework\App\ActionInterface $subject,
  71. \Magento\Framework\App\RequestInterface $request
  72. ) {
  73. if (!$this->customerSession->isLoggedIn() ||
  74. !$this->moduleManager->isEnabled('Magento_PageCache') ||
  75. !$this->cacheConfig->isEnabled() ||
  76. !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
  77. return;
  78. }
  79. $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
  80. $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
  81. $customerTaxClassId = $this->customerSession->getCustomerTaxClassId();
  82. if (!empty($defaultBillingAddress) || !empty($defaultShippingAddress)) {
  83. $taxRates = $this->taxCalculation->getTaxRates(
  84. $defaultBillingAddress,
  85. $defaultShippingAddress,
  86. $customerTaxClassId
  87. );
  88. $this->httpContext->setValue(
  89. 'tax_rates',
  90. $taxRates,
  91. 0
  92. );
  93. }
  94. }
  95. }