Skip to content

Commit

Permalink
Add depend config for email marketing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacker committed Dec 5, 2022
1 parent 4e83826 commit 146f3b5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
33 changes: 24 additions & 9 deletions Observer/Customer/ModelSaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Mageplaza\Smtp\Helper\EmailMarketing;

/**
* Class ModelSaveBefore
Expand All @@ -37,29 +38,43 @@ class ModelSaveBefore implements ObserverInterface
*/
protected $customerFactory;

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

/**
* ModelSaveBefore constructor.
*
* @param CustomerFactory $customerFactory
* @param EmailMarketing $helperEmailMarketing
*/
public function __construct(CustomerFactory $customerFactory)
{
public function __construct(
CustomerFactory $customerFactory,
EmailMarketing $helperEmailMarketing
) {
$this->customerFactory = $customerFactory;
$this->helperEmailMarketing = $helperEmailMarketing;
}

/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$dataObject = $observer->getEvent()->getDataObject();
if ($this->helperEmailMarketing->isEnableEmailMarketing() &&
$this->helperEmailMarketing->getSecretKey() &&
$this->helperEmailMarketing->getAppID()
) {
$dataObject = $observer->getEvent()->getDataObject();

if (!$dataObject->getId()) {
//isObjectNew can't use on this case
$dataObject->setIsNewRecord(true);
} elseif ($dataObject instanceof Customer) {
$customOrigObject = $this->customerFactory->create()->load($dataObject->getId());
$dataObject->setCustomOrigObject($customOrigObject);
if (!$dataObject->getId()) {
//isObjectNew can't use on this case
$dataObject->setIsNewRecord(true);
} elseif ($dataObject instanceof Customer) {
$customOrigObject = $this->customerFactory->create()->load($dataObject->getId());
$dataObject->setCustomOrigObject($customOrigObject);
}
}
}
}
42 changes: 28 additions & 14 deletions Plugin/AccountManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Mageplaza\Smtp\Helper\EmailMarketing;

/**
* Class AccountManagement
Expand All @@ -45,18 +46,26 @@ class AccountManagement
*/
protected $cartRepository;

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

/**
* AccountManagement constructor.
*
* @param CheckoutSession $checkoutSession
* @param CartRepositoryInterface $cartRepository
* @param EmailMarketing $helperEmailMarketing
*/
public function __construct(
CheckoutSession $checkoutSession,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
EmailMarketing $helperEmailMarketing
) {
$this->checkoutSession = $checkoutSession;
$this->cartRepository = $cartRepository;
$this->helperEmailMarketing = $helperEmailMarketing;
}

/**
Expand All @@ -71,22 +80,27 @@ public function __construct(
*/
public function afterIsEmailAvailable(CustomerAccountManagement $subject, $result, $customerEmail)
{
$cartId = $this->checkoutSession->getQuote()->getId();
if ($this->helperEmailMarketing->isEnableEmailMarketing() &&
$this->helperEmailMarketing->getSecretKey() &&
$this->helperEmailMarketing->getAppID()
) {
$cartId = $this->checkoutSession->getQuote()->getId();

if (!$cartId) {
return $result;
}

/** @var Quote $quote */
$quote = $this->cartRepository->get($cartId);
$quote->setCustomerEmail($customerEmail);
if (!$cartId) {
return $result;
}

try {
$this->cartRepository->save($quote);
/** @var Quote $quote */
$quote = $this->cartRepository->get($cartId);
$quote->setCustomerEmail($customerEmail);

return $result;
} catch (Exception $e) {
return $result;
try {
$this->cartRepository->save($quote);
} catch (Exception $e) {
return $result;
}
}

return $result;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"mageplaza/module-core": "^1.4.12"
},
"type": "magento2-module",
"version": "4.7.3",
"version": "4.7.4",
"license": "proprietary",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion etc/adminhtml/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<add id="Mageplaza_Smtp::configuration" title="Configuration" module="Mageplaza_Smtp" sortOrder="100" action="adminhtml/system_config/edit/section/smtp" resource="Mageplaza_Smtp::configuration" parent="Mageplaza_Smtp::smtp"/>
<add id="Mageplaza_Smtp::abandoned_cart" title="Abandoned Carts" module="Mageplaza_Smtp" sortOrder="80" parent="Mageplaza_Smtp::smtp" action="adminhtml/smtp/abandonedcart" resource="Mageplaza_Smtp::abandoned_cart"/>

<add id="Avada_Smtp::email_marketing" title="Marketing Automation" module="Mageplaza_Smtp" sortOrder="50" parent="Magento_Backend::marketing" resource="Mageplaza_Smtp::email_marketing_menu"/>
<add id="Avada_Smtp::email_marketing" title="Marketing Automation" module="Mageplaza_Smtp" sortOrder="50" parent="Magento_Backend::marketing" resource="Mageplaza_Smtp::email_marketing_menu" dependsOnConfig="email_marketing/general/enabled"/>
<add id="Avada_Smtp::email_marketing_automation" title="Automation" module="Mageplaza_Smtp" sortOrder="10" action="adminhtml/smtp/marketing/type/automation" resource="Mageplaza_Smtp::email_marketing_menu" parent="Avada_Smtp::email_marketing" />
<add id="Avada_Smtp::email_marketing_newsletters" title="Campaign" module="Mageplaza_Smtp" sortOrder="20" action="adminhtml/smtp/marketing/type/newsletters" resource="Mageplaza_Smtp::email_marketing_menu" parent="Avada_Smtp::email_marketing" />
<add id="Avada_Smtp::email_marketing_sms" title="SMS" module="Mageplaza_Smtp" sortOrder="30" action="adminhtml/smtp/marketing/type/sms" resource="Mageplaza_Smtp::email_marketing_menu" parent="Avada_Smtp::email_marketing" />
Expand Down

0 comments on commit 146f3b5

Please sign in to comment.