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