Skip to content

Commit

Permalink
Merge pull request #1756 from mailchimp/Issue1311-2.4
Browse files Browse the repository at this point in the history
closes #1311 for magento 2.4
  • Loading branch information
gonzaloebiz authored Aug 2, 2023
2 parents 6b30b4b + f824b81 commit b78cd43
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Block/Adminhtml/Customer/Edit/Tabs/View/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(
\Magento\Framework\Registry $registry,
array $data
) {

parent::__construct($context, $data);
$this->helper = $helper;
$this->subscriberFactory = $subscriberFactory;
Expand All @@ -55,7 +55,7 @@ public function getInterest()
{
$subscriber = $this->subscriberFactory->create();
$customerId = $this->registry->registry(\Magento\Customer\Controller\RegistryConstants::CURRENT_CUSTOMER_ID);
$subscriber->loadByCustomerId($customerId);
$subscriber->loadByCustomer($customerId);
return $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
}
4 changes: 2 additions & 2 deletions Block/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function __construct(
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
array $data
) {

parent::__construct($context, $data);
$this->_checkoutSession = $checkoutSession;
$this->_helper = $helper;
Expand All @@ -68,7 +68,7 @@ public function getInterest()
* @var $subscriber \Magento\Newsletter\Model\Subscriber
*/
$subscriber = $this->_subscriberFactory->create();
$subscriber->loadByEmail($order->getCustomerEmail());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());

return $this->_helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
Expand Down
41 changes: 38 additions & 3 deletions Block/Newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

namespace Ebizmarts\MailChimp\Block;

use Magento\Customer\Controller\RegistryConstants;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Registry;

class Newsletter extends \Magento\Framework\View\Element\Template
{
/**
Expand All @@ -27,38 +32,68 @@ class Newsletter extends \Magento\Framework\View\Element\Template
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var Registry
*/
private $registry;

/**
* Newsletter constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param CustomerRepositoryInterface $customerRepository
* @param Registry $registry
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\Session $customerSession,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Helper\Data $helper,
CustomerRepositoryInterface $customerRepository,
Registry $registry,
array $data
) {

parent::__construct($context, $data);
$this->_helper = $helper;
$this->subscriberFactory = $subscriberFactory;
$this->customerSession = $customerSession;
$this->customerRepository = $customerRepository;
$this->registry = $registry;
}

public function getInterest()
{
$customer = $this->getCurrentCustomer();
$subscriber = $this->subscriberFactory->create();
$subscriber->loadByCustomerId($this->customerSession->getCustomerId());
$subscriber->loadByCustomer($customer->getId(),$customer->getStoreId());
// $subscriber = $this->getSubscriptionObject();
return $this->_helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
public function getFormUrl()
{
return $this->getUrl('mailchimp/accountmanage/save');
}
private function getCurrentCustomer()
{
$customerId = $this->getCurrentCustomerId();
try {
$customer = $this->customerRepository->getById($customerId);
} catch (NoSuchEntityException $e) {
return null;
}

return $customer;
}
private function getCurrentCustomerId(): int
{
return (int)$this->registry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
}

}
4 changes: 2 additions & 2 deletions Controller/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function execute()
$subscriber = $this->_subscriberFactory->create();
$interestGroup = $this->_interestGroupFactory->create();
try {
$subscriber->loadByEmail($order->getCustomerEmail());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());
if ($subscriber->getEmail()==$order->getCustomerEmail()) {
if ($subscriber->getStatus()==\Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED) {
$subscriber->subscribe($subscriber->getEmail());
Expand All @@ -96,7 +96,7 @@ public function execute()
$this->_updateSubscriber($listId, $subscriber->getId(), $this->_helper->getGmtDate(), '', 1);
} else {
$this->_subscriberFactory->create()->subscribe($order->getCustomerEmail());
$subscriber->loadByEmail($order->getCustomerEmail());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->_helper->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
4 changes: 2 additions & 2 deletions Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
}
$serializedGroups = $this->_helper->serialize($groups);
$subscriber = $this->_subscriberFactory->create();
$subscriber->loadByCustomerId($customer->getId());
$subscriber->loadByCustomer($customer->getId());
$interestGroup = $this->interestGroupFactory->create();
if ($subscriber->getEmail()==$customer->getEmail()) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
Expand All @@ -296,7 +296,7 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
$listId = $this->_helper->getGeneralList($subscriber->getStoreId());
} else {
$this->_subscriberFactory->create()->subscribe($customer->getEmail());
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($serializedGroups);
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
2 changes: 1 addition & 1 deletion Model/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected function buildSubscriberData(\Magento\Customer\Model\Customer $custome
protected function isSubscriber(\Magento\Customer\Model\Customer $customer)
{
$subscriber = $this->subscriberFactory->create();
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
if ($subscriber->getEmail() == $customer->getEmail()) {
if ($subscriber->getStatus() === \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions Model/Plugin/Newsletter/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function afterExecute()
$email = $customer->getEmail();

try {
$subscriber->loadByCustomerId($this->customerSession->getCustomerId());
$subscriber->loadByCustomer($customer->getId(), $customer->getStoreId());
if ($subscriber->getEmail()==$email) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
Expand All @@ -92,7 +92,7 @@ public function afterExecute()
$this->_updateSubscriber($listId, $subscriber->getId(), $this->helper->getGmtDate(), null, 1);
} else {
$this->subscriberFactory->create()->subscribe($email);
$subscriber->loadByEmail($email);
$subscriber->loadBySubscriberEmail($email, $customer->getStoreId());
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
6 changes: 3 additions & 3 deletions Model/Plugin/SubscriptionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function beforeUnsubscribeCustomer(
) {
if ($this->_helper->isMailChimpEnabled($storeId)) {

$subscriber = $this->_subscriberFactory->create()->loadByCustomerId($customerId);
$subscriber = $this->_subscriberFactory->create()->loadByCustomer($customerId, $storeId);
if ($subscriber->isSubscribed()) {
$api = $this->_helper->getApi($storeId);
try {
Expand Down Expand Up @@ -107,7 +107,7 @@ public function beforeSubscribeCustomer(
) {
if ($this->_helper->isMailChimpEnabled($storeId)) {

$subscriber = $this->_subscriberFactory->create()->loadByCustomerId($customerId);
$subscriber = $this->_subscriberFactory->create()->loadByCustomer($customerId, $storeId);
if (!$subscriber->isSubscribed()) {
if (!$this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_MAGENTO_MAIL, $storeId)) {
$subscriber->setImportMode(true);
Expand Down Expand Up @@ -221,4 +221,4 @@ public function beforeUnsubscribe(
}
return [$email,$storeId,$confirmCode];
}
}
}
6 changes: 3 additions & 3 deletions Observer/Adminhtml/Customer/SaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
}
$interestGroup = $this->interestGroupFactory->create();
try {
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
if ($subscriber->getEmail() == $customer->getEmail()) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
Expand All @@ -85,7 +85,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
);
} else {
$this->subscriberFactory->create()->subscribe($customer->getEmail());
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand All @@ -98,7 +98,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
$this->helper->log($params);
}
} else {
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
if ($subscriber->getEmail() == $customer->getEmail()) {
$this->syncHelper->markRegisterAsModified(
$subscriber->getId(),
Expand Down
2 changes: 1 addition & 1 deletion Observer/Customer/SaveBefore.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
);
}
$subscriber = $this->subscriberFactory->create();
$subscriber->loadByEmail($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
if ($subscriber->getEmail() == $customer->getEmail()) {
$this->syncHelper->markRegisterAsModified(
$subscriber->getId(),
Expand Down
10 changes: 7 additions & 3 deletions Observer/Subscriber/SaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ public function execute(\Magento\Framework\Event\Observer $observer)
*/
$factory = $this->_subscriberFactory->create();
$subscriber = $observer->getSubscriber();
$subscriberOld = $factory->loadByCustomerId($subscriber->getCustomerId());

if ($this->_helper->isMailChimpEnabled($subscriberOld->getStoreId())&&$subscriberOld->getEmail()&&$subscriber->getEmail()!=$subscriberOld->getEmail()) {
// $subscriberOld = $factory->loadByCustomer($subscriber->getCustomerId(), $subscriber->getStoreId());
$isCustomer = $subscriber->getCustomerId();
if ($isCustomer) {
$subscriberOld = $factory->loadBySubscriberEmail($subscriber->getCustomerId(), $subscriber->getStoreId());
}
if ($this->_helper->isMailChimpEnabled($subscriber->getStoreId())&&$isCustomer&&
$subscriberOld->getEmail()&&$subscriber->getEmail()!=$subscriberOld->getEmail()) {
$api = $this->_helper->getApi($subscriberOld->getStoreId());
$mergeVars = $this->_helper->getMergeVarsBySubscriber($subscriberOld, $subscriberOld->getEmail());
$status = 'unsubscribed';
Expand Down

0 comments on commit b78cd43

Please sign in to comment.