-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAbandonedCartProcessor.php
172 lines (151 loc) · 5.37 KB
/
AbandonedCartProcessor.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Lof\Mautic\Queue\Processor;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SortOrderBuilder;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\StoreManager;
use Magento\Reports\Model\ResourceModel\Quote\CollectionFactory;
use Lof\Mautic\Model\Mautic\Contact;
use Lof\Mautic\Helper\Data;
use Lof\Mautic\Model\Mautic\AbstractApi;
use Lof\Mautic\Queue\MessageQueues\Order\Publisher;
/**
* Class AbandonedCartProcessor
*/
class AbandonedCartProcessor extends AbstractQueueProcessor
{
/**
* @var array
*/
protected $customergroups = [];
/**
* @var string|null
*/
protected $firstdate = null;
/**
* @var Publisher
*/
private $publisher;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @var StoreManager
*/
protected $_storeManager;
/**
* CategoryImport constructor.
*
* @param Contact $mauticContact
* @param Data $helperData
* @param CollectionFactory $collectionFactory
* @param Publisher $publisher
* @param StoreManager $storeManager
*/
public function __construct(
Contact $mauticContact,
Data $helperData,
CollectionFactory $collectionFactory,
Publisher $publisher,
StoreManager $storeManager
) {
parent::__construct($mauticContact, $helperData);
$this->collectionFactory = $collectionFactory;
$this->publisher = $publisher;
$this->_storeManager = $storeManager;
}
/**
* @return void
*/
public function process()
{
$allStores = $this->_storeManager->getStores();
foreach ($allStores as $storeId => $val) {
$this->_storeManager->setCurrentStore($storeId);
if ($this->helperData->isEnabled($storeId)) {
$this->_processAbandoned($storeId);
}
}
return;
}
/**
* Process abandoned cart
*
* @param int $storeId
* @return void
*/
protected function _processAbandoned($storeId)
{
$this->firstdate = $this->helperData->getConfig(Data::MODULE_FIRST_DATE, $storeId);
if (!$this->firstdate) {
$this->firstdate = $this->_getSuggestedZeroDate();
}
$this->customergroups = $this->helperData->getConfig(Data::MODULE_ABANDONED_CUSTOMER_GROUP, $storeId);
$token = $this->helperData->getConfig(Data::MODULE_ABANDONEDCART_TOKEN, $storeId);
if ($this->customergroups) {
$this->customergroups = explode(",", $this->customergroups);
} else {
$this->customergroups = [];
}
$diff = $this->helperData->getConfig(Data::MODULE_DIFF_DATE, $storeId);
if (!$diff) { //Disable feature when diff date number is empty
return;
}
$expr = sprintf('DATE_SUB(now(), %s)', $this->_getIntervalUnitSql($diff, 'DAY'));
$from = new \Zend_Db_Expr($expr);
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter('items_count', array('neq' => '0'))
->addFieldToFilter('main_table.is_active', '1')
->addFieldToFilter('main_table.store_id', array('eq' => $storeId))
->addSubtotal($storeId)
->setOrder('updated_at');
$collection->addFieldToFilter('main_table.converted_at', array(array('null' => true), $this->_getSuggestedZeroDate()))
->addFieldToFilter('main_table.updated_at', array('to' => $from, 'from' => $this->firstdate));
$collection->addFieldToFilter('main_table.customer_email', array('neq' => ''));
if (count($this->customergroups)) {
$collection->addFieldToFilter('main_table.customer_group_id', array('in' => $this->customergroups));
}
try {
foreach ($collection as $quote) {
$tokenNew = md5($token.$quote->getEntityId());
$url = $this->_storeManager->getStore($storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK) . 'mautic/cart/loadquote?id=' . $quote->getEntityId() . '&token=' . $tokenNew;
$customData = [
"email" => $quote->getCustomerEmail(),
"firstname" => $quote->getFirstname(),
"lastname" => $quote->getLastname(),
"tags" => Data::ABANDONED_CART_TAGS,
"cart_url" => $url
];
$customer = $this->helperData->getCustomerById($quote->getCustomerId());
if (!$this->helperData->isAyncApi()) {
$this->mauticContact->exportCustomer($customer, $customData);
} else {
$data = $this->mauticContact->getRequestData($customData, $customer);
$this->publisher->execute(
$this->helperData->encodeData($data)
);
}
}
} catch (\Exception $e) {
//log exception at here
}
return;
}
/**
* @param $interval
* @param $this->unit
* @return string
*/
protected function _getIntervalUnitSql($interval, $unit)
{
return sprintf('INTERVAL %d %s', $interval, $unit);
}
/**
* @return string
*/
protected function _getSuggestedZeroDate()
{
return '0000-00-00 00:00:00';
}
}