-
Notifications
You must be signed in to change notification settings - Fork 111
/
Save.php
116 lines (109 loc) · 4.42 KB
/
Save.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 11/27/17 8:14 PM
* @file: Save.php
*/
namespace Ebizmarts\MailChimp\Model\Plugin\Newsletter;
class Save
{
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $helper;
/**
* @var \Magento\Customer\Model\Session
*/
protected $customerSession;
/**
* @var \Magento\Newsletter\Model\SubscriberFactory
*/
protected $subscriberFactory;
/**
* @var \Magento\Framework\App\Request\Http
*/
protected $request;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory
*/
protected $interestGroupFactory;
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
protected $serializer;
/**
* Save constructor.
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
* @param \Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory
* @param \Magento\Framework\App\Request\Http $request
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
*/
public function __construct(
\Ebizmarts\MailChimp\Helper\Data $helper,
\Magento\Customer\Model\Session $customerSession,
\Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
\Ebizmarts\MailChimp\Model\MailChimpInterestGroupFactory $interestGroupFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Serialize\Serializer\Json $serializer
) {
$this->helper = $helper;
$this->customerSession = $customerSession;
$this->subscriberFactory = $subscriberFactory;
$this->request = $request;
$this->interestGroupFactory = $interestGroupFactory;
$this->serializer = $serializer;
}
public function afterExecute()
{
$params = $this->request->getParams();
$subscriber = $this->subscriberFactory->create();
$interestGroup = $this->interestGroupFactory->create();
/**
* @var $customer \Magento\Customer\Model\Customer
*/
$customer = $this->customerSession->getCustomer();
$email = $customer->getEmail();
try {
$subscriber->loadByCustomerId($this->customerSession->getCustomerId());
if ($subscriber->getEmail()==$email) {
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->serializer->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
$interestGroup->setStoreId($subscriber->getStoreId());
$interestGroup->setUpdatedAt($this->helper->getGmtDate());
$interestGroup->getResource()->save($interestGroup);
$listId = $this->helper->getGeneralList($subscriber->getStoreId());
$this->_updateSubscriber($listId, $subscriber->getId(), $this->helper->getGmtDate(), null, 1);
} else {
$this->subscriberFactory->create()->subscribe($email);
$subscriber->loadByEmail($email);
$interestGroup->getBySubscriberIdStoreId($subscriber->getSubscriberId(), $subscriber->getStoreId());
$interestGroup->setGroupdata($this->serializer->serialize($params));
$interestGroup->setSubscriberId($subscriber->getSubscriberId());
$interestGroup->setStoreId($subscriber->getStoreId());
$interestGroup->setUpdatedAt($this->helper->getGmtDate());
$interestGroup->getResource()->save($interestGroup);
}
} catch (\Exception $e) {
}
}
protected function _updateSubscriber($listId, $entityId, $sync_delta = null, $sync_error = null, $sync_modified = null)
{
$this->helper->saveEcommerceData(
$listId,
$entityId,
\Ebizmarts\MailChimp\Helper\Data::IS_SUBSCRIBER,
$sync_delta,
$sync_error,
$sync_modified
);
}
}