Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ContextPlugin.php
Go to the documentation of this file.
1 <?php
8 
13 {
17  protected $customerSession;
18 
22  protected $httpContext;
23 
27  protected $taxHelper;
28 
32  protected $taxCalculation;
33 
39  private $moduleManager;
40 
46  private $cacheConfig;
47 
56  public function __construct(
57  \Magento\Customer\Model\Session $customerSession,
58  \Magento\Framework\App\Http\Context $httpContext,
59  \Magento\Tax\Model\Calculation\Proxy $calculation,
60  \Magento\Tax\Helper\Data $taxHelper,
61  \Magento\Framework\Module\Manager $moduleManager,
62  \Magento\PageCache\Model\Config $cacheConfig
63  ) {
64  $this->customerSession = $customerSession;
65  $this->httpContext = $httpContext;
66  $this->taxCalculation = $calculation;
67  $this->taxHelper = $taxHelper;
68  $this->moduleManager = $moduleManager;
69  $this->cacheConfig = $cacheConfig;
70  }
71 
78  public function beforeDispatch(
79  \Magento\Framework\App\ActionInterface $subject,
80  \Magento\Framework\App\RequestInterface $request
81  ) {
82  if (!$this->customerSession->isLoggedIn() ||
83  !$this->moduleManager->isEnabled('Magento_PageCache') ||
84  !$this->cacheConfig->isEnabled() ||
85  !$this->taxHelper->isCatalogPriceDisplayAffectedByTax()) {
86  return;
87  }
88 
89  $defaultBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
90  $defaultShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
91  $customerTaxClassId = $this->customerSession->getCustomerTaxClassId();
92 
93  if (!empty($defaultBillingAddress) || !empty($defaultShippingAddress)) {
94  $taxRates = $this->taxCalculation->getTaxRates(
95  $defaultBillingAddress,
96  $defaultShippingAddress,
97  $customerTaxClassId
98  );
99  $this->httpContext->setValue(
100  'tax_rates',
101  $taxRates,
102  0
103  );
104  }
105  }
106 }
beforeDispatch(\Magento\Framework\App\ActionInterface $subject, \Magento\Framework\App\RequestInterface $request)
$moduleManager
Definition: products.php:75
__construct(\Magento\Customer\Model\Session $customerSession, \Magento\Framework\App\Http\Context $httpContext, \Magento\Tax\Model\Calculation\Proxy $calculation, \Magento\Tax\Helper\Data $taxHelper, \Magento\Framework\Module\Manager $moduleManager, \Magento\PageCache\Model\Config $cacheConfig)