From e0c3c3df0fbac389f205efdee6b21007a71f3ba8 Mon Sep 17 00:00:00 2001 From: klaviyojad Date: Thu, 7 Oct 2021 15:37:44 -0400 Subject: [PATCH] code changes for email consent for logged in users --- Plugin/CheckoutLayoutPlugin.php | 35 +++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/Plugin/CheckoutLayoutPlugin.php b/Plugin/CheckoutLayoutPlugin.php index 74f013f2..9955f46c 100644 --- a/Plugin/CheckoutLayoutPlugin.php +++ b/Plugin/CheckoutLayoutPlugin.php @@ -39,6 +39,8 @@ public function _getDefaultAddressIfSetForCustomer() public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout) { + $address = $this->_getDefaultAddressIfSetForCustomer(); + if ($this->_klaviyoScopeSetting->getConsentAtCheckoutSMSIsActive()) { $smsConsentCheckbox = [ @@ -61,8 +63,6 @@ public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $p 'id' => 'kl_sms_consent', ]; - $address = $this->_getDefaultAddressIfSetForCustomer(); - if (!$address) $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['kl_sms_consent'] = $smsConsentCheckbox; else { @@ -89,7 +89,7 @@ public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $p } } - if (!$this->_customerSession->isLoggedIn() && $this->_klaviyoScopeSetting->getConsentAtCheckoutEmailIsActive()) + if ($this->_klaviyoScopeSetting->getConsentAtCheckoutEmailIsActive()) { $emailConsentCheckbox = [ 'component' => 'Magento_Ui/js/form/element/abstract', @@ -110,7 +110,34 @@ public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $p 'id' => 'kl_email_consent', ]; - $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['kl_email_consent'] = $emailConsentCheckbox; + // extra un-editable field with saved email address to display to logged in users + $emailConsentField = [ + 'component' => 'Magento_Ui/js/form/element/abstract', + 'config' => + [ + 'customScope' => 'shippingAddress', + 'template' => 'ui/form/field', + 'elementTmpl' => 'ui/form/element/input', + ], + 'label' => 'Email Address', + 'provider' => 'checkoutProvider', + 'sortOrder' => '0', + 'disabled' => true, + 'visible' => true, + 'value' => $this->_customerSession->getCustomer()->getEmail() + ]; + + if (!$address) { + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['kl_email_consent'] = $emailConsentCheckbox; + if($this->_customerSession->isLoggedIn()) + { + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['kl_email'] = $emailConsentField; + } + } + else { + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['before-form']['children']['kl_email_consent'] = $emailConsentCheckbox; + $jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['before-form']['children']['kl_email'] = $emailConsentField; + } }