DepersonalizePlugin.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Tax\Model\Layout;
  8. use Magento\PageCache\Model\DepersonalizeChecker;
  9. /**
  10. * Class DepersonalizePlugin
  11. */
  12. class DepersonalizePlugin
  13. {
  14. /**
  15. * @var DepersonalizeChecker
  16. */
  17. protected $depersonalizeChecker;
  18. /**
  19. * @var \Magento\Customer\Model\Session
  20. */
  21. protected $customerSession;
  22. /**
  23. * @var array
  24. */
  25. protected $defaultTaxShippingAddress;
  26. /**
  27. * @var array
  28. */
  29. protected $defaultTaxBillingAddress;
  30. /**
  31. * @var int
  32. */
  33. protected $customerTaxClassId;
  34. /**
  35. * @param DepersonalizeChecker $depersonalizeChecker
  36. * @param \Magento\Customer\Model\Session $customerSession
  37. */
  38. public function __construct(
  39. DepersonalizeChecker $depersonalizeChecker,
  40. \Magento\Customer\Model\Session $customerSession
  41. ) {
  42. $this->customerSession = $customerSession;
  43. $this->depersonalizeChecker = $depersonalizeChecker;
  44. }
  45. /**
  46. * Before generate Xml
  47. *
  48. * @param \Magento\Framework\View\LayoutInterface $subject
  49. * @return array
  50. */
  51. public function beforeGenerateXml(\Magento\Framework\View\LayoutInterface $subject)
  52. {
  53. if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
  54. $this->defaultTaxBillingAddress = $this->customerSession->getDefaultTaxBillingAddress();
  55. $this->defaultTaxShippingAddress = $this->customerSession->getDefaultTaxShippingAddress();
  56. $this->customerTaxClassId = $this->customerSession->getCustomerTaxClassId();
  57. }
  58. return [];
  59. }
  60. /**
  61. * After generate Xml
  62. *
  63. * @param \Magento\Framework\View\LayoutInterface $subject
  64. * @param \Magento\Framework\View\LayoutInterface $result
  65. * @return \Magento\Framework\View\LayoutInterface
  66. */
  67. public function afterGenerateXml(\Magento\Framework\View\LayoutInterface $subject, $result)
  68. {
  69. if ($this->depersonalizeChecker->checkIfDepersonalize($subject)) {
  70. $this->customerSession->setDefaultTaxBillingAddress($this->defaultTaxBillingAddress);
  71. $this->customerSession->setDefaultTaxShippingAddress($this->defaultTaxShippingAddress);
  72. $this->customerSession->setCustomerTaxClassId($this->customerTaxClassId);
  73. }
  74. return $result;
  75. }
  76. }