Skip to content

Commit

Permalink
Merge branch '2.4-develop' into 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
JackerNgo committed Oct 29, 2020
2 parents 741140a + 2e50958 commit 77d4760
Show file tree
Hide file tree
Showing 27 changed files with 409 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Block/Script.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getCurrentOrder()
*/
public function toHtml()
{
if (!$this->helperEmailMarketing->isEnableAbandonedCart()) {
if (!$this->helperEmailMarketing->isEnableEmailMarketing()) {
return '';
}

Expand Down
4 changes: 2 additions & 2 deletions Controller/AbandonedCart/Recover.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public function execute()
*/
public function recover($token)
{
if (!$this->helperData->isEnableAbandonedCart($this->storeManager->getStore()->getId())) {
throw new LocalizedException(__('SMTP abandoned cart is disabled.'));
if (!$this->helperData->isEnableEmailMarketing($this->storeManager->getStore()->getId())) {
throw new LocalizedException(__('Marketing Automation is disabled.'));
}

$token = explode('_', $token);
Expand Down
28 changes: 9 additions & 19 deletions Controller/Adminhtml/Smtp/Sync/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Mageplaza\Smtp\Helper\EmailMarketing;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Customer\Model\Attribute;
use Zend_Db_Expr;

/**
Expand Down Expand Up @@ -59,31 +58,23 @@ class Customer extends Action
*/
protected $customerCollectionFactory;

/**
* @var Attribute
*/
protected $customerAttribute;

/**
* Customer constructor.
*
* @param Context $context
* @param EmailMarketing $helperEmailMarketing
* @param CustomerFactory $customerFactory
* @param CustomerCollectionFactory $customerCollectionFactory
* @param Attribute $customerAttribute
*/
public function __construct(
Context $context,
EmailMarketing $helperEmailMarketing,
CustomerFactory $customerFactory,
CustomerCollectionFactory $customerCollectionFactory,
Attribute $customerAttribute
CustomerCollectionFactory $customerCollectionFactory
) {
$this->helperEmailMarketing = $helperEmailMarketing;
$this->customerFactory = $customerFactory;
$this->customerCollectionFactory = $customerCollectionFactory;
$this->customerAttribute = $customerAttribute;
parent::__construct($context);
}

Expand All @@ -94,11 +85,7 @@ public function execute()
{
$result = [];
try {
$attribute = 'mp_smtp_is_synced';
$attribute = $this->customerAttribute->loadByCode('customer', $attribute);
if (!$attribute->getId()) {
throw new LocalizedException(__('%1 not found.', $attribute));
}
$attribute = $this->helperEmailMarketing->getSyncedAttribute();

$customerCollection = $this->customerCollectionFactory->create();
$ids = $this->getRequest()->getParam('ids');
Expand Down Expand Up @@ -128,7 +115,8 @@ public function execute()
$result['total'] = count($ids);
$response = $this->helperEmailMarketing->syncCustomers($data);
if (isset($response['success'])) {
$this->insertData($customerCollection->getConnection(), $attributeData);
$table = $customerCollection->getTable('customer_entity_int');
$this->insertData($customerCollection->getConnection(), $attributeData, $table);
}

} catch (Exception $e) {
Expand All @@ -140,15 +128,17 @@ public function execute()
}

/**
* @param AdapterInterface $connection
* @param array $data
* @param string $table
*
* @throws Exception
*/
public function insertData($connection, $data)
public function insertData($connection, $data, $table)
{
$connection->beginTransaction();
try {
$connection->insertMultiple('customer_entity_int', $data);
$connection->insertMultiple($table, $data);
$connection->commit();
} catch (Exception $e) {
$connection->rollBack();
Expand Down
16 changes: 15 additions & 1 deletion Controller/Adminhtml/Smtp/Sync/EstimateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Exception;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\LocalizedException;
use Mageplaza\Smtp\Helper\EmailMarketing;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;

Expand All @@ -45,17 +46,25 @@ class EstimateCustomer extends Action
*/
protected $customerCollectionFactory;

/**
* @var EmailMarketing
*/
protected $emailMarketing;

/**
* EstimateCustomer constructor.
*
* @param Context $context
* @param CustomerCollectionFactory $customerCollectionFactory
* @param EmailMarketing $emailMarketing
*/
public function __construct(
Context $context,
CustomerCollectionFactory $customerCollectionFactory
CustomerCollectionFactory $customerCollectionFactory,
EmailMarketing $emailMarketing
) {
$this->customerCollectionFactory = $customerCollectionFactory;
$this->emailMarketing = $emailMarketing;

parent::__construct($context);
}
Expand All @@ -66,6 +75,11 @@ public function __construct(
public function execute()
{
try {

if (!$this->emailMarketing->getAppID() || !$this->emailMarketing->getSecretKey()) {
throw new LocalizedException(__('App ID or Secret Key is empty'));
}

$attribute = 'mp_smtp_is_synced';
$customerCollection = $this->customerCollectionFactory->create();
$storeId = $this->getRequest()->getParam('storeId');
Expand Down
11 changes: 6 additions & 5 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
class Data extends AbstractData
{
const CONFIG_MODULE_PATH = 'smtp';
const CONFIG_GROUP_SMTP = 'configuration_option';
const EMAIL_MARKETING = 'email_marketing';
const CONFIG_GROUP_SMTP = 'configuration_option';
const DEVELOP_GROUP_SMTP = 'developer';

/**
Expand Down Expand Up @@ -128,20 +129,20 @@ public function isTestEmail()
*
* @return mixed
*/
public function getAbandonedCartConfig($code = '', $storeId = null)
public function getEmailMarketingConfig($code = '', $storeId = null)
{
$code = ($code !== '') ? '/' . $code : '';

return $this->getConfigValue(static::CONFIG_MODULE_PATH . '/abandoned_cart' . $code, $storeId);
return $this->getConfigValue(static::EMAIL_MARKETING . '/general' . $code, $storeId);
}

/**
* @param null $storeId
*
* @return bool
*/
public function isEnableAbandonedCart($storeId = null)
public function isEnableEmailMarketing($storeId = null)
{
return $this->isEnabled($storeId) && $this->getAbandonedCartConfig('enabled');
return $this->getEmailMarketingConfig('enabled', $storeId);
}
}
53 changes: 50 additions & 3 deletions Helper/EmailMarketing.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@
use Magento\Quote\Model\ResourceModel\Quote as ResourceQuote;
use Magento\Shipping\Helper\Data as ShippingHelper;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Customer\Model\Attribute;

/**
* Class EmailMarketing
* @package Mageplaza\Smtp\Helper
*/
class EmailMarketing extends Data
{
const IS_SYNCED_ATTRIBUTE = 'mp_smtp_is_synced';

const APP_URL = 'https://app.avada.io/webhook/checkout/create';
const CUSTOMER_URL = 'https://app.avada.io/webhook/customer/create';
const ORDER_URL = 'https://app.avada.io/webhook/order/processing';
Expand Down Expand Up @@ -118,6 +121,11 @@ class EmailMarketing extends Data
*/
protected $shippingHelper;

/**
* @var Attribute
*/
protected $customerAttribute;

/**
* @var string
*/
Expand All @@ -128,6 +136,11 @@ class EmailMarketing extends Data
*/
protected $storeId = '';

/**
* @var bool
*/
protected $isSyncCustomer = false;

/**
* EmailMarketing constructor.
*
Expand All @@ -145,6 +158,7 @@ class EmailMarketing extends Data
* @param ProductRepository $productRepository
* @param ResourceQuote $resourceQuote
* @param ShippingHelper $shippingHelper
* @param Attribute $customerAttribute
*/
public function __construct(
Context $context,
Expand All @@ -160,7 +174,8 @@ public function __construct(
Curl $curl,
ProductRepository $productRepository,
ResourceQuote $resourceQuote,
ShippingHelper $shippingHelper
ShippingHelper $shippingHelper,
Attribute $customerAttribute
) {
parent::__construct($context, $objectManager, $storeManager);

Expand All @@ -175,6 +190,7 @@ public function __construct(
$this->resourceQuote = $resourceQuote;
$this->productRepository = $productRepository;
$this->shippingHelper = $shippingHelper;
$this->customerAttribute = $customerAttribute;
}

/**
Expand Down Expand Up @@ -272,7 +288,7 @@ public function getCustomerName(Quote $quote)
*/
public function getAppID($storeId = null)
{
return $this->getAbandonedCartConfig('app_id', $storeId);
return $this->getEmailMarketingConfig('app_id', $storeId);
}

/**
Expand All @@ -282,7 +298,7 @@ public function getAppID($storeId = null)
*/
public function getSecretKey($storeId = null)
{
$secretKey = $this->getAbandonedCartConfig('secret_key', $storeId);
$secretKey = $this->getEmailMarketingConfig('secret_key', $storeId);

return $this->encryptor->decrypt($secretKey);
}
Expand Down Expand Up @@ -834,4 +850,35 @@ public function syncCustomers($data)
{
return $this->sendRequest($data, self::SYNC_CUSTOMER_URL);
}

/**
* @return Attribute|string
* @throws LocalizedException
*/
public function getSyncedAttribute()
{
$attribute = self::IS_SYNCED_ATTRIBUTE;
$attribute = $this->customerAttribute->loadByCode('customer', $attribute);
if (!$attribute->getId()) {
throw new LocalizedException(__('%1 not found.', $attribute));
}

return $attribute;
}

/**
* @param boolean $value
*/
public function setIsSyncedCustomer($value)
{
$this->isSyncCustomer = $value;
}

/**
* @return boolean
*/
public function isSyncedCustomer()
{
return $this->isSyncCustomer;
}
}
27 changes: 24 additions & 3 deletions Observer/Customer/CustomerSaveCommitAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Mageplaza\Smtp\Helper\EmailMarketing;
use Magento\Customer\Model\Customer;
use Psr\Log\LoggerInterface;
use Magento\Customer\Model\ResourceModel\Customer as ResourceCustomer;

/**
* Class CustomerSaveCommitAfter
Expand All @@ -39,6 +40,11 @@ class CustomerSaveCommitAfter implements ObserverInterface
*/
protected $helperEmailMarketing;

/**
* @var ResourceCustomer
*/
protected $resourceCustomer;

/**
* @var LoggerInterface
*/
Expand All @@ -49,13 +55,16 @@ class CustomerSaveCommitAfter implements ObserverInterface
*
* @param EmailMarketing $helperEmailMarketing
* @param LoggerInterface $logger
* @param ResourceCustomer $resourceCustomer
*/
public function __construct(
EmailMarketing $helperEmailMarketing,
LoggerInterface $logger
LoggerInterface $logger,
ResourceCustomer $resourceCustomer
) {
$this->helperEmailMarketing = $helperEmailMarketing;
$this->logger = $logger;
$this->resourceCustomer = $resourceCustomer;
}

/**
Expand All @@ -67,15 +76,27 @@ public function execute(Observer $observer)
* @var Customer $customer
*/
$customer = $observer->getEvent()->getDataObject();
if ($this->helperEmailMarketing->isEnableAbandonedCart() &&
if ($this->helperEmailMarketing->isEnableEmailMarketing() &&
$this->helperEmailMarketing->getSecretKey() &&
$this->helperEmailMarketing->getAppID() &&
$customer->getIsNewRecord()
) {
try {
$data = $this->helperEmailMarketing->getCustomerData($customer);

$this->helperEmailMarketing->syncCustomer($data);
$result = $this->helperEmailMarketing->syncCustomer($data);
if (!empty($result['success'])) {
$this->helperEmailMarketing->setIsSyncedCustomer(true);
$table = $this->resourceCustomer->getTable('customer_entity_int');
$connection = $this->resourceCustomer->getConnection();
$attribute = $this->helperEmailMarketing->getSyncedAttribute();
$data = [
'attribute_id' => $attribute->getId(),
'entity_id' => $customer->getId(),
'value' => 1
];
$connection->insert($table, $data);
}
} catch (Exception $e) {
$this->logger->critical($e->getMessage());
}
Expand Down
Loading

0 comments on commit 77d4760

Please sign in to comment.