Skip to content

Commit

Permalink
fix for #1818 for magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Sep 8, 2023
1 parent f806789 commit 835f4ec
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 33 deletions.
11 changes: 10 additions & 1 deletion Block/Adminhtml/Customer/Edit/Tabs/View/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

namespace Ebizmarts\MailChimp\Block\Adminhtml\Customer\Edit\Tabs\View;
use Magento\Store\Model\StoreManagerInterface;

class Customer extends \Magento\Backend\Block\Template
{
Expand All @@ -28,24 +29,31 @@ class Customer extends \Magento\Backend\Block\Template
* @var \Magento\Backend\Model\Session
*/
protected $session;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
StoreManagerInterface $storeManager,
array $data
) {

parent::__construct($context, $data);
$this->helper = $helper;
$this->subscriberFactory = $subscriberFactory;
$this->session =$context->getBackendSession();
$this->storeManager = $storeManager;
}

public function getInterest()
Expand All @@ -54,7 +62,8 @@ public function getInterest()
$customerData = $this->session->getCustomerData();
$email = $customerData['account']['email'];
$storeId = $customerData['account']['store_id'];
$subscriber->loadBySubscriberEmail($email,$storeId);
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
$subscriber->loadBySubscriberEmail($email,$websiteId);
return $this->helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
}
13 changes: 10 additions & 3 deletions Block/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @file: Success.php
*/
namespace Ebizmarts\MailChimp\Block\Checkout;
use Magento\Store\Model\StoreManagerInterface;

class Success extends \Magento\Framework\View\Element\Template
{
Expand All @@ -34,14 +35,18 @@ class Success extends \Magento\Framework\View\Element\Template
* @var \Magento\Framework\View\Element\Template\Context
*/
protected $_context;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Success constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
* @param StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
Expand All @@ -50,15 +55,16 @@ public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
StoreManagerInterface $storeManager,
array $data
) {

parent::__construct($context, $data);
$this->_checkoutSession = $checkoutSession;
$this->_helper = $helper;
$this->_subscriberFactory = $subscriberFactory;
$this->_interestGroupFactory= $interestGroupFactory;
$this->_context = $context;
$this->storeManager = $storeManager;
}

public function getInterest()
Expand All @@ -68,7 +74,8 @@ public function getInterest()
* @var $subscriber \Magento\Newsletter\Model\Subscriber
*/
$subscriber = $this->_subscriberFactory->create();
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());
$websiteId = (int)$this->storeManager->getStore($order->getStoreId())->getWebsiteId();
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $websiteId);

return $this->_helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
Expand Down
11 changes: 10 additions & 1 deletion Block/Newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;

class Newsletter extends \Magento\Framework\View\Element\Template
{
Expand All @@ -34,13 +35,18 @@ class Newsletter extends \Magento\Framework\View\Element\Template
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @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 StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
Expand All @@ -49,20 +55,23 @@ public function __construct(
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Helper\Data $helper,
CustomerRepositoryInterface $customerRepository,
StoreManagerInterface $storeManager,
array $data
) {
parent::__construct($context, $data);
$this->_helper = $helper;
$this->subscriberFactory = $subscriberFactory;
$this->customerSession = $customerSession;
$this->customerRepository = $customerRepository;
$this->storeManager = $storeManager;
}

public function getInterest()
{
$customer = $this->getCurrentCustomer();
$subscriber = $this->subscriberFactory->create();
$subscriber->loadByCustomer($customer->getId(),$customer->getStoreId());
$websiteId = (int)$this->storeManager->getStore($customer->getStoreId())->getWebsiteId();
$subscriber->loadByCustomer($customer->getId(),$websiteId);
return $this->_helper->getSubscriberInterest($subscriber->getSubscriberId(), $subscriber->getStoreId());
}
public function getFormUrl()
Expand Down
16 changes: 12 additions & 4 deletions Controller/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ebizmarts\MailChimp\Controller\Checkout;

use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;
use Magento\Store\Model\StoreManagerInterface;

class Success extends \Magento\Framework\App\Action\Action
{
Expand Down Expand Up @@ -41,6 +42,10 @@ class Success extends \Magento\Framework\App\Action\Action
* @var SyncHelper
*/
private $syncHelper;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Framework\App\Action\Context $context
Expand All @@ -50,6 +55,7 @@ class Success extends \Magento\Framework\App\Action\Action
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
* @param SyncHelper $syncHelper
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
Expand All @@ -58,30 +64,32 @@ public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
SyncHelper $syncHelper
SyncHelper $syncHelper,
StoreManagerInterface $storeManager
) {

$this->_pageFactory =$pageFactory;
$this->_helper = $helper;
$this->_checkoutSession = $checkoutSession;
$this->_subscriberFactory = $subscriberFactory;
$this->_interestGroupFactory= $interestGroupFactory;
$this->syncHelper = $syncHelper;
$this->storeManager = $storeManager;
parent::__construct($context);
}

public function execute()
{
$params = $this->getRequest()->getParams();
$order = $this->_checkoutSession->getLastRealOrder();
$websiteId = (int)$this->storeManager->getStore($order->getStoreId())->getWebsiteId();
/**
* @var $subscriber \Magento\Newsletter\Model\Subscriber
* @var $interestGroup \Ebizmarts\MailChimp\Model\MailChimpInterestGroup
*/
$subscriber = $this->_subscriberFactory->create();
$interestGroup = $this->_interestGroupFactory->create();
try {
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $websiteId);
if ($subscriber->getEmail()==$order->getCustomerEmail()) {
if ($subscriber->getStatus()==\Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED) {
$subscriber->subscribe($subscriber->getEmail());
Expand All @@ -96,7 +104,7 @@ public function execute()
$this->_updateSubscriber($listId, $subscriber->getId(), $this->_helper->getGmtDate(), '', 1);
} else {
$this->_subscriberFactory->create()->subscribe($order->getCustomerEmail());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $order->getStoreId());
$subscriber->loadBySubscriberEmail($order->getCustomerEmail(), $websiteId);
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->_helper->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
5 changes: 3 additions & 2 deletions Cron/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ protected function _profile($data)
protected function _processMerges(\Magento\Customer\Model\Customer $customer, $data)
{
$mapFields = $this->_helper->getMapFields($customer->getStoreId());
$websiteId = (int)$this->storeManager->getStore($customer->getStoreId())->getWebsiteId();
foreach($data['merges'] as $key=> $value) {
if (!empty($value)) {
if ($key=='GROUPINGS') {
Expand All @@ -284,7 +285,7 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
}
$serializedGroups = $this->_helper->serialize($groups);
$subscriber = $this->_subscriberFactory->create();
$subscriber->loadByCustomer($customer->getId());
$subscriber->loadByCustomer($customer->getId(),$websiteId);
$interestGroup = $this->interestGroupFactory->create();
if ($subscriber->getEmail()==$customer->getEmail()) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
Expand All @@ -296,7 +297,7 @@ protected function _processMerges(\Magento\Customer\Model\Customer $customer, $d
$listId = $this->_helper->getGeneralList($subscriber->getStoreId());
} else {
$this->_subscriberFactory->create()->subscribe($customer->getEmail());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
$subscriber->loadBySubscriberEmail($customer->getEmail(), $websiteId);
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($serializedGroups);
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
14 changes: 11 additions & 3 deletions Model/Api/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Magento\Directory\Model\CountryFactory;
use Magento\Framework\Exception\State\ExpiredException;
use Magento\Store\Model\StoreManagerInterface;
use Symfony\Component\Config\Definition\Exception\Exception;
use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;

Expand Down Expand Up @@ -58,6 +59,10 @@ class Customer
* @var SyncHelper
*/
private $syncHelper;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Ebizmarts\MailChimp\Helper\Data $helper
Expand All @@ -68,6 +73,7 @@ class Customer
* @param CountryFactory $countryFactory
* @param \Magento\Customer\Model\Address $address
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
Expand All @@ -77,9 +83,9 @@ public function __construct(
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollection,
\Magento\Directory\Model\CountryFactory $countryFactory,
\Magento\Customer\Model\Address $address,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
StoreManagerInterface $storeManager
) {

$this->_helper = $helper;
$this->_collection = $collection;
$this->_orderCollection = $orderCollection;
Expand All @@ -90,6 +96,7 @@ public function __construct(
$this->_countryFactory = $countryFactory;
$this->subscriberFactory = $subscriberFactory;
$this->syncHelper = $syncHelper;
$this->storeManager = $storeManager;
}
public function sendCustomers($storeId)
{
Expand Down Expand Up @@ -185,7 +192,8 @@ protected function buildSubscriberData(\Magento\Customer\Model\Customer $custome
protected function isSubscriber(\Magento\Customer\Model\Customer $customer)
{
$subscriber = $this->subscriberFactory->create();
$subscriber->loadBySubscriberEmail($customer->getEmail(), $customer->getStoreId());
$websiteId = (int)$this->storeManager->getStore($customer->getStoreId())->getWebsiteId();
$subscriber->loadBySubscriberEmail($customer->getEmail(), $websiteId);
if ($subscriber->getEmail() == $customer->getEmail()) {
if ($subscriber->getStatus() === \Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED) {
return true;
Expand Down
16 changes: 12 additions & 4 deletions Model/Plugin/Newsletter/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Ebizmarts\MailChimp\Model\Plugin\Newsletter;

use Ebizmarts\MailChimp\Helper\Sync as SyncHelper;
use Magento\Store\Model\StoreManagerInterface;

class Save
{
Expand Down Expand Up @@ -41,6 +42,10 @@ class Save
* @var \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory
*/
protected $interestGroupFactory;
/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param \Ebizmarts\MailChimp\Helper\Data $helper
Expand All @@ -49,22 +54,24 @@ class Save
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
* @param \Magento\Framework\App\Request\Http $request
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
SyncHelper $syncHelper,
\Magento\Customer\Model\Session $customerSession,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
\Magento\Framework\App\Request\Http $request
\Magento\Framework\App\Request\Http $request,
StoreManagerInterface $storeManager
) {

$this->helper = $helper;
$this->syncHelper = $syncHelper;
$this->customerSession = $customerSession;
$this->subscriberFactory = $subscriberFactory;
$this->request = $request;
$this->interestGroupFactory = $interestGroupFactory;
$this->storeManager = $storeManager;
}
public function afterExecute()
{
Expand All @@ -78,9 +85,10 @@ public function afterExecute()
$customer = $this->customerSession->getCustomer();

$email = $customer->getEmail();
$websiteId = (int)$this->storeManager->getStore($customer->getStoreId())->getWebsiteId();

try {
$subscriber->loadByCustomer($customer->getId(), $customer->getStoreId());
$subscriber->loadByCustomer($customer->getId(), $websiteId);
if ($subscriber->getEmail()==$email) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
Expand All @@ -92,7 +100,7 @@ public function afterExecute()
$this->_updateSubscriber($listId, $subscriber->getId(), $this->helper->getGmtDate(), null, 1);
} else {
$this->subscriberFactory->create()->subscribe($email);
$subscriber->loadBySubscriberEmail($email, $customer->getStoreId());
$subscriber->loadBySubscriberEmail($email, $websiteId);
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->helper->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
Expand Down
Loading

0 comments on commit 835f4ec

Please sign in to comment.