Skip to content

Commit

Permalink
Merge pull request #138 from klaviyo/202109_sms_consent_default_addre…
Browse files Browse the repository at this point in the history
…ss_fix

Fix to Expose SMS Consent For Logged In Users with Default Address Set
  • Loading branch information
klaviyojad authored Sep 22, 2021
2 parents 1ec861e + b9a9537 commit 688b85e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### [Unreleased]

### [3.0.9] - 2021-09-21

#### Fixed
- SMS Consent checkbox for logged in users with default address set
- URL construction works when store URL has subdirectories
- Remove reference to deprecated _learnq functionality

Expand Down Expand Up @@ -125,7 +129,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CSP now uses report-only mode


[Unreleased]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.8...HEAD
[Unreleased]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.9...HEAD
[3.0.9]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.8...3.0.9
[3.0.8]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.7...3.0.8
[3.0.7]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.6...3.0.7
[3.0.6]: https://github.com/klaviyo/magento2-klaviyo/compare/3.0.5...3.0.6
Expand Down
58 changes: 51 additions & 7 deletions Plugin/CheckoutLayoutPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,37 @@
namespace Klaviyo\Reclaim\Plugin;

use Klaviyo\Reclaim\Helper\ScopeSetting;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Session;


class CheckoutLayoutPlugin
{
public function __construct(
ScopeSetting $klaviyoScopeSetting
ScopeSetting $klaviyoScopeSetting,
Session $customerSession,
CustomerFactory $customerFactory
) {
$this->_klaviyoScopeSetting = $klaviyoScopeSetting;
$this->_customerSession = $customerSession;
$this->_customerFactory = $customerFactory;
}

/**
* Checks if logged in user has a default address set, if not returns false.
*
* @return Magento\Customer\Model\Address|false
*/
public function _getDefaultAddressIfSetForCustomer()
{
$address = false;
if ($this->_customerSession->isLoggedIn()) {
$customerData = $this->_customerSession->getCustomer()->getData();
$customerId = $customerData["entity_id"];
$customer = $this->_customerFactory->create()->load($customerId);
$address = $customer->getDefaultShippingAddress();
}
return $address;
}

public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $processor, $jsLayout)
Expand All @@ -38,14 +61,35 @@ public function afterProcess(\Magento\Checkout\Block\Checkout\LayoutProcessor $p
'id' => 'kl_sms_consent',
];

$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['kl_sms_consent'] = $smsConsentCheckbox;
$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 {

// extra un-editable field with saved phone number to display to logged in users with default address set
$smsConsentTelephone = [
'component' => 'Magento_Ui/js/form/element/abstract',
'config' =>
[
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/input',
],
'label' => 'Phone Number',
'provider' => 'checkoutProvider',
'sortOrder' => '120',
'disabled' => true,
'visible' => true,
'value' => $address->getTelephone()
];

$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['before-form']['children']['kl_sms_phone_number'] = $smsConsentTelephone;
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']['shippingAddress']['children']['before-form']['children']['kl_sms_consent'] = $smsConsentCheckbox;
}
}
// Open to ideas here, since we don't overwrite the customer-email section
// we need to distinguish if the customer is logged in or not, object manager is an easy way to do so
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if (!$customerSession->isLoggedIn() && $this->_klaviyoScopeSetting->getConsentAtCheckoutEmailIsActive())
if (!$this->_customerSession->isLoggedIn() && $this->_klaviyoScopeSetting->getConsentAtCheckoutEmailIsActive())
{
$emailConsentCheckbox = [
'component' => 'Magento_Ui/js/form/element/abstract',
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "klaviyo/magento2-extension",
"description": "Klaviyo extension for Magento 2. Allows pushing newsletters to Klaviyo's platform and more.",
"type": "magento2-module",
"version": "3.0.8",
"version": "3.0.9",
"autoload": {
"files": [
"registration.php"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Klaviyo_Reclaim" setup_version="3.0.8" schema_version="3.0.8">
<module name="Klaviyo_Reclaim" setup_version="3.0.9" schema_version="3.0.9">
<sequence>
<module name="Magento_Customer"/>
<module name="Magento_Checkout"/>
Expand Down

0 comments on commit 688b85e

Please sign in to comment.