Skip to content

Commit

Permalink
NAM-33982 collect email sms consent (#25)
Browse files Browse the repository at this point in the history
* NAM-33982 add email and sms consent option

* NAM-33982 change text add default label

* NAM-33982 fix wrong name

* NAM-33982 check if customer exists

* NAM-33982 remove sms attribute from customer form
catch customer not found exception
  • Loading branch information
ronshenk authored Mar 14, 2024
1 parent ebe5b48 commit 8c29d75
Show file tree
Hide file tree
Showing 17 changed files with 743 additions and 5 deletions.
62 changes: 62 additions & 0 deletions Block/Adminhtml/Settings/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public function __construct(
$this->is_fpt_enabled = $configHelper->getWithFixedProductTax();
$this->is_cart_auto_coupon_enabled = $configHelper->isCartAutoCouponEnabled();
$this->is_aw_points_enabled = $configHelper->isAheadworksRewardPointsEnabled();
$this->email_consent_enabled = $configHelper->getValue(ConfigHelper::EMAIL_CONSENT_ENABLED);
$this->email_consent_checkbox_position = $configHelper->getValue(ConfigHelper::EMAIL_CONSENT_CHECKBOX_POSITION);
$this->email_consent_checkbox_lable_value = $configHelper->getValue(ConfigHelper::EMAIL_CONSENT_CHECKBOX_LABEL_VALUE);
$this->sms_consent_enabled = $configHelper->getValue(ConfigHelper::SMS_CONSENT_ENABLED);
$this->sms_consent_checkbox_position = $configHelper->getValue(ConfigHelper::SMS_CONSENT_CHECKBOX_POSITION);
$this->sms_consent_checkbox_lable_value = $configHelper->getValue(ConfigHelper::SMS_CONSENT_CHECKBOX_LABEL_VALUE);
$aw_service = $rewardPointsFactory->create();
if ($aw_service) {
$this->is_aw_points_plugin_exists = true;
Expand Down Expand Up @@ -132,4 +138,60 @@ public function getCartAutoCouponEnabled()
{
return $this->is_cart_auto_coupon_enabled;
}

/**
* @return int|mixed
*/
public function getEmailConsentEnabled()
{
return $this->email_consent_enabled;
}

/**
* @return int|mixed
*/
public function getEmailConsentCheckboxPosition()
{
return $this->email_consent_checkbox_position;
}

/**
* @return int|mixed
*/
public function getEmailConsentCheckboxLabelValue()
{
return $this->email_consent_checkbox_lable_value;
}

/**
* @return int|mixed
*/
public function getSMSConsentEnabled()
{
return $this->sms_consent_enabled;
}

/**
* @return int|mixed
*/
public function getSMSConsentCheckboxPosition()
{
return $this->sms_consent_checkbox_position;
}

/**
* @return mixed
*/
public function getSMSConsentCheckboxLabelValue()
{
return $this->sms_consent_checkbox_lable_value;
}

/**
* @return array
*/
public function getFormFieldPositions()
{
return range(0 , 1000, 10);
}
}
24 changes: 24 additions & 0 deletions Controller/Adminhtml/Settings/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ private function saveSettings($data)
$this->configHelper->setCustomerAddressType($data['customer_address']);
}

if (isset($data['email_consent'])) {
$this->configHelper->setValue(ConfigHelper::EMAIL_CONSENT_ENABLED, $data['email_consent'] == 1);
}

if (isset($data['email_consent_checkbox_position'])) {
$this->configHelper->setValue(ConfigHelper::EMAIL_CONSENT_CHECKBOX_POSITION, $data['email_consent_checkbox_position']);
}

if (isset($data['email_consent_checkbox_lable_value'])) {
$this->configHelper->setValue(ConfigHelper::EMAIL_CONSENT_CHECKBOX_LABEL_VALUE, $data['email_consent_checkbox_lable_value']);
}

if (isset($data['sms_consent'])) {
$this->configHelper->setValue(ConfigHelper::SMS_CONSENT_ENABLED, $data['sms_consent'] == 1);
}

if (isset($data['sms_consent_checkbox_position'])) {
$this->configHelper->setValue(ConfigHelper::SMS_CONSENT_CHECKBOX_POSITION, $data['sms_consent_checkbox_position']);
}

if (isset($data['sms_consent_checkbox_lable_value'])) {
$this->configHelper->setValue(ConfigHelper::SMS_CONSENT_CHECKBOX_LABEL_VALUE, $data['sms_consent_checkbox_lable_value']);
}

return true;
}
}
52 changes: 52 additions & 0 deletions Helper/ConfigHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ class ConfigHelper
const ENABLE_AHEADWORKS_REWARD_POINTS = 'remarkety/mgconnector/aheadworks_reward_points';
const CUSTOMER_ADDRESS_TO_USE = 'remarkety/mgconnector/customer_address_to_use';
const CART_AUTO_COUPON_ENABLED = 'remarkety/mgconnector/cart_auto_coupon_enabled';
const EMAIL_CONSENT_ENABLED = 'remarkety/mgconnector/email_consent_enabled';
const EMAIL_CONSENT_CHECKBOX_POSITION = 'remarkety/mgconnector/email_consent_checkbox_position';
const EMAIL_CONSENT_CHECKBOX_LABEL_VALUE = 'remarkety/mgconnector/email_consent_checkbox_lable_value';
const SMS_CONSENT_ENABLED = 'remarkety/mgconnector/sms_consent_enabled';
const SMS_CONSENT_CHECKBOX_POSITION = 'remarkety/mgconnector/sms_consent_checkbox_position';
const SMS_CONSENT_CHECKBOX_LABEL_VALUE = 'remarkety/mgconnector/sms_consent_checkbox_lable_value';
/**
* should we get marketing consent saved in shopper attribute, if set to 0 then we don't get the data
* added this setting just in case we see performance degradation in production stores
*/
const GET_SHOPPER_MARKETING_CONSENT = 'remarkety/mgconnector/get_shopper_marketing_consent';

const ASYNC_MODE_OFF = 0;
const ASYNC_MODE_ON = 1;
Expand Down Expand Up @@ -338,4 +349,45 @@ public function customerPendingConfirmation(Customer $customer)
$customer->getWebsiteId()
);
}

public function getEmailConsentEnabled()
{
return $this->_scopeConfig->getValue(self::EMAIL_CONSENT_ENABLED);
}

public function getEmailConsentCheckBoxPosition()
{
return $this->_scopeConfig->getValue(self::EMAIL_CONSENT_CHECKBOX_POSITION);
}


public function setValue($path, $value)
{
$this->configResource->saveConfig(
$path,
$value,
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
0
);
$this->cacheTypeList->cleanType('config');
}

public function getValue($path)
{
return $this->_scopeConfig->getValue($path);
}

/**
* check if the setting to get shopper marketing consent attribute is set to disabled
* @return bool
*/
public function shouldGetShopperConsentAttribute()
{
$getShopperMarketingConsent = $this->getValue(self::GET_SHOPPER_MARKETING_CONSENT);

if ($getShopperMarketingConsent === 0 || $getShopperMarketingConsent === '0') {
return false;
}
return true;
}
}
31 changes: 30 additions & 1 deletion Model/Api/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ public function getCustomers(
$pageSize = null;

$customerData = $this->_customerCollectionFactory->create();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');

if ($customer_id !== null) {
$customerData->addFieldToFilter('entity_id', $customer_id);
Expand Down Expand Up @@ -642,6 +644,7 @@ public function getCustomers(
$customerData->setPage($pageNumber, $pageSize);
}
$pos_id_attribute_code = $this->configHelper->getPOSAttributeCode();
$should_get_shopper_consent_data = $this->configHelper->shouldGetShopperConsentAttribute();

if (!empty($pos_id_attribute_code)) {
//make sure we get the POS id attribute
Expand Down Expand Up @@ -701,6 +704,18 @@ public function getCustomers(
if ($aw_rewards_integrate) {
$customers['rewards_points'] = $this->customerRewardPointsService->getCustomerRewardPointsBalance($customer->getId());
}

if ($should_get_shopper_consent_data) {
/**
* @var CustomerInterface $customerInterface
*/
$customerInterface = $customerRepository->getById($customer->getId());
if ($customerInterface) {
$rm_sms_consent = $customerInterface->getCustomAttribute('rm_sms_consent');
$customers['rm_sms_consent'] = $rm_sms_consent ? $rm_sms_consent->getValue() : null;
}
}

$customerArray[] = $this->dataOverride->customer($customer, $customers);
}
$object = new DataObject();
Expand Down Expand Up @@ -780,6 +795,20 @@ public function getCustomerDataById($id = false)

if ($customerData->getId()) {
$customerArray = $this->mapCustomer($customerData, true);
$should_get_shopper_consent_data = $this->configHelper->shouldGetShopperConsentAttribute();
if ($should_get_shopper_consent_data) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
/**
* @var CustomerInterface $customerInterface
*/
$customerInterface = $customerRepository->getById($id);
if ($customerInterface) {
$rm_sms_consent = $customerInterface->getCustomAttribute('rm_sms_consent');
$customerArray['rm_sms_consent'] = $rm_sms_consent ? $rm_sms_consent->getValue() : null;
}
}
}
return $customerArray;
}
Expand Down Expand Up @@ -1354,7 +1383,7 @@ public function setConfig($mage_store_id, $configName, $scope, $newValue)
*/
public function getVersion()
{
return '2.8.0';
return '2.8.1';
}

/**
Expand Down
135 changes: 135 additions & 0 deletions Observer/TriggerOrderPlacedFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Remarkety\Mgconnector\Observer;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Customer\Model\Group;
use Magento\Customer\Model\ResourceModel\CustomerRepository;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Registry;
use Magento\Newsletter\Model\Subscriber;
use Magento\Sales\Model\Order;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManager;
use Psr\Log\LoggerInterface;
use Remarkety\Mgconnector\Helper\ConfigHelper;
use Remarkety\Mgconnector\Model\QueueRepository;
use Remarkety\Mgconnector\Serializer;
use Remarkety\Mgconnector\Serializer\AddressSerializer;
use Remarkety\Mgconnector\Serializer\CustomerSerializer;
use Remarkety\Mgconnector\Serializer\OrderSerializer;
use Remarkety\Mgconnector\Serializer\ProductSerializer;

class TriggerOrderPlacedFinished extends EventMethods implements ObserverInterface
{
protected $subscriberFactory;

public function __construct(
LoggerInterface $logger,
Registry $coreRegistry,
Subscriber $subscriber,
Group $customerGroupModel,
QueueRepository $remarketyQueueRepo,
\Remarkety\Mgconnector\Model\QueueFactory $queueFactory,
Store $store,
ScopeConfigInterface $scopeConfig,
OrderSerializer $orderSerializer,
CustomerSerializer $customerSerializer,
AddressSerializer $addressSerializer,
ConfigHelper $configHelper,
ProductSerializer $productSerializer,
Http $request,
CustomerRepository $customerRepository,
CustomerRegistry $customerRegistry,
StoreManager $storeManager,
\Magento\Framework\Serialize\Serializer\Serialize $serialize,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
) {
parent::__construct(
$logger,
$coreRegistry,
$subscriber,
$customerGroupModel,
$remarketyQueueRepo,
$queueFactory,
$store,
$scopeConfig,
$orderSerializer,
$customerSerializer,
$addressSerializer,
$configHelper,
$productSerializer,
$request,
$customerRepository,
$customerRegistry,
$storeManager,
$serialize);
$this->subscriberFactory = $subscriberFactory;
}

/**
* @param Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
try {
$this->startTiming(self::class);
/**
* @var $order Order
*/
$order = $observer->getEvent()->getDataByKey('order');

$rm_email_consent = null;
$rm_sms_consent = null;
$extensionAttributes = null;
$orderPlacedParams = json_decode($this->request->getContent(), true);
if (isset($orderPlacedParams['billingAddress']['extension_attributes'])) {
$extensionAttributes = $orderPlacedParams['billingAddress']['extension_attributes'];
} else if (isset($orderPlacedParams['billingAddress']['extensionAttributes'])) {
$extensionAttributes = $orderPlacedParams['billingAddress']['extensionAttributes'];
}

if ($extensionAttributes) {
if (isset($extensionAttributes['rm_email_consent']) && $extensionAttributes['rm_email_consent']) {
$rm_email_consent = true;
}

if (isset ($extensionAttributes['rm_sms_consent']) && $extensionAttributes['rm_sms_consent']) {
$rm_sms_consent = true;
}
}

if (!empty($rm_email_consent) || !empty($rm_sms_consent)) {
$email = $order->getCustomerEmail();
$customer = null;
$subscriber = $this->subscriberFactory->create()->loadByEmail($email);
try {
$customer = $this->customerRepository->get($email); // get by email and not by id since if the customer is not logged in we will not find him
} catch (\Exception $e) {}
if (!empty($rm_email_consent) && $subscriber) {
if ($customer) {
$subscriber->setCustomerId($customer->getId());
}
$subscriber->setStoreId($order->getStoreId());
$subscriber->setEmail($email);
$subscriber->setStatus(\Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED);
$subscriber->save();
}
if (!empty($rm_sms_consent)) {
if ($customer) {
$customer->setCustomAttribute('rm_sms_consent', $rm_sms_consent);
$this->customerRepository->save($customer);
}
}
}
$this->endTiming(self::class);
} catch (\Exception $ex) {
$this->logError($ex);
}
}
}
Loading

0 comments on commit 8c29d75

Please sign in to comment.