From 146f3b5cea4279727e2cf8183e14c8be8c917591 Mon Sep 17 00:00:00 2001 From: Jacker Date: Mon, 5 Dec 2022 10:21:35 +0700 Subject: [PATCH] Add depend config for email marketing --- Observer/Customer/ModelSaveBefore.php | 33 +++++++++++++++------ Plugin/AccountManagement.php | 42 ++++++++++++++++++--------- composer.json | 2 +- etc/adminhtml/menu.xml | 2 +- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/Observer/Customer/ModelSaveBefore.php b/Observer/Customer/ModelSaveBefore.php index a29382c..04de17e 100644 --- a/Observer/Customer/ModelSaveBefore.php +++ b/Observer/Customer/ModelSaveBefore.php @@ -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 @@ -37,14 +38,23 @@ 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; } /** @@ -52,14 +62,19 @@ public function __construct(CustomerFactory $customerFactory) */ 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); + } } } } diff --git a/Plugin/AccountManagement.php b/Plugin/AccountManagement.php index 5d93502..c309b37 100644 --- a/Plugin/AccountManagement.php +++ b/Plugin/AccountManagement.php @@ -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 @@ -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; } /** @@ -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; } } diff --git a/composer.json b/composer.json index b995c37..bc3066c 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "mageplaza/module-core": "^1.4.12" }, "type": "magento2-module", - "version": "4.7.3", + "version": "4.7.4", "license": "proprietary", "authors": [ { diff --git a/etc/adminhtml/menu.xml b/etc/adminhtml/menu.xml index 9dea0ec..b2a58af 100644 --- a/etc/adminhtml/menu.xml +++ b/etc/adminhtml/menu.xml @@ -26,7 +26,7 @@ - +