forked from vinvin27/superabandonedcart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch_campaings.php
executable file
·239 lines (179 loc) · 8.26 KB
/
launch_campaings.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
// Set true for debug $days = 0
define('DEBUG_SAC',false);
include(dirname(__FILE__).'/../../config/config.inc.php');
include(_PS_ROOT_DIR_.'/init.php');
include_once(dirname(__FILE__).'/classes/Campaign.php');
include_once(dirname(__FILE__).'/classes/CampaignHistory.php');
// Check security key
if ( Tools::getValue('secure_key') != Configuration::get('SUPER_AC_SECURE_KEY') )
die('NOSECUREKEY');
class LaunchCampaign
{
public function sendCampaign()
{
// get abandoned cart :
$sql = "SELECT * FROM (
SELECT
CONCAT(LEFT(c.`firstname`, 1), '. ', c.`lastname`) `customer`, a.id_cart total, ca.name carrier, c.id_customer, a.id_cart, a.date_upd,a.date_add,
IF (IFNULL(o.id_order, 'Non ordered') = 'Non ordered', IF(TIME_TO_SEC(TIMEDIFF('".date('Y-m-d H:i:s')."', a.`date_add`)) > 86000, 'Abandoned cart', 'Non ordered'), o.id_order) id_order, IF(o.id_order, 1, 0) badge_success, IF(o.id_order, 0, 1) badge_danger, IF(co.id_guest, 1, 0) id_guest
FROM `"._DB_PREFIX_."cart` a
JOIN `"._DB_PREFIX_."customer` c ON (c.id_customer = a.id_customer)
LEFT JOIN `"._DB_PREFIX_."currency` cu ON (cu.id_currency = a.id_currency)
LEFT JOIN `"._DB_PREFIX_."carrier` ca ON (ca.id_carrier = a.id_carrier)
LEFT JOIN `"._DB_PREFIX_."orders` o ON (o.id_cart = a.id_cart)
LEFT JOIN `"._DB_PREFIX_."connections` co ON (a.id_guest = co.id_guest AND TIME_TO_SEC(TIMEDIFF('".date('Y-m-d H:i:s')."', co.`date_add`)) < 1800)
WHERE a.date_add > (NOW() - INTERVAL 60 DAY) ORDER BY a.id_cart DESC
) AS toto WHERE id_order='Abandoned cart'";
$currency = Context::getContext()->currency->sign;
$defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT')));
$abandoned_carts = Db::getInstance()->ExecuteS($sql);
// get all available campaigns
$sqlCampaigns = 'SELECT * FROM `'._DB_PREFIX_.'campaign` WHERE active=1';
$allCampaigns = Db::getInstance()->ExecuteS($sqlCampaigns);
if( !$allCampaigns || empty($allCampaigns) )
die('NO CAMPAIGN');
// loop on all abandoned carts
foreach( $abandoned_carts as $abncart ) {
if( Cart::getNbProducts((int)$abncart['id_cart']) > 0 ) {
$emailsSent = 0;
// loop on all available campaigns
foreach( $allCampaigns as $camp ) {
if( DEBUG_SAC )
echo 'IdCustomer : '.$abncart['id_customer'].' - IdCart : '.$abncart['id_cart'].'<br/>';
$cartIsOnCampaign = $this->checkIfCartIsOnCampaign( $abncart['date_add'] , $camp['execution_time_day'] , $camp['execution_time_hour']);
if( $cartIsOnCampaign ){
if( DEBUG_SAC )
echo 'Cart on campaign</br>';
$id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
$customer = new Customer($abncart['id_customer']);
$cart = new Cart($abncart['id_cart']);
$products = $cart->getProducts();
$tpl_vars = array(
'{firstname}' => $customer->firstname,
'{lastname}' => $customer->lastname,
'{campaign_name}' => $camp['name'],
'{track_url}' => $this->getBaseURL().'?id_cart='.(int)$abncart['id_cart'].'&id_customer='.(int)$abncart['id_customer'],
'{track_request}' => '?id_cart='.(int)$abncart['id_cart'].'&id_customer='.(int)$abncart['id_customer']
);
$campM = new Campaign($camp['id_campaign']);
if( $campM->voucher_amount && $campM->voucher_day && $campM->voucher_amount_type ) {
$campM->clean_old_reduction($campM->voucher_prefix);
$customerVoucher = $campM->registerDiscount($customer->id,$campM->voucher_amount ,$campM->voucher_day,$campM->voucher_amount_type,$campM->voucher_prefix);
$tpl_vars['{coupon_name}'] = $customerVoucher->name;
$tpl_vars['{coupon_code}'] = $customerVoucher->code;
$tpl_vars['{coupon_value}'] = ( $camp['voucher_amount_type'] == 'percent' ? $customerVoucher->reduction_percent.'%' : Tools::displayprice($customerVoucher->reduction_amount) );
$tpl_vars['{coupon_valid_to}'] = date('d/m/Y',strtotime( $customerVoucher->date_to ));
}
if (! empty( $products ) ) {
$cart_content = $campM->getCartContentHeader();
}
else {
$cart_content = '';
}
foreach( $products as $prod ) {
$p = new Product($prod['id_product'],true,$id_lang);
$price_with_tax = (Product::getPriceStatic($p->id,true,null,2,null,false,true,1,false,null,$abncart['id_cart'],null,$null,true,true,null,false,false));
$total_with_tax = $prod['cart_quantity'] * $price_with_tax;
$images = Image::getImages((int)$id_lang, (int)$p->id);
$link = new Link();
$cart_content .= '<tr>
<td align="center" ><img src="'.Tools::getShopProtocol().$link->getImageLink($p->link_rewrite,$images[0]['id_image']).'" width="80"/></td>
<td align="center" ><a href="'.$link->getProductLink($p).'?id_cart='.(int)$abncart['id_cart'].'&id_customer='.(int)$abncart['id_customer'].'"/>'.$p->name.'</a></td>
<td align="center" >'.Tools::displayprice($price_with_tax).'</td>
<td align="center" >'.$prod['cart_quantity'].'</td>
<td align="center" >'.Tools::displayprice($total_with_tax).'</td>
</tr>';
}
$cart_content .= '</table>';
$tpl_vars['{cart_content}'] = $cart_content;
$path = _PS_ROOT_DIR_.'/modules/superabandonedcart/mails/';
// send email to customer :
$mailUser = Mail::Send(
$id_lang ,
$campM->getFileName() ,
$camp['name'] ,
$tpl_vars ,
$customer->email ,
null,
null,
null,
null,
null,
$path,
false, Context::getContext()->shop->id);
// if mail user is successfully sent :
if( $mailUser ) {
$history = new CampaignHistory();
$history->id_campaign= (int)$camp['id_campaign'];
$history->id_customer = $abncart['id_customer'];
$history->id_cart = $abncart['id_cart'];
$history->id_cart_rule = ( isset($customerVoucher->id) ? $customerVoucher->id : 0);
$history->click = 0;
$history->converted = 0;
$history->date_update = date('Y-m-d H:i:s', time());
$history->save();
// Email to admin :
Mail::Send(
$id_lang ,
$campM->getFileName() ,
Mail::l( sprintf('Email sent to %s %s for campaign %s' , $customer->lastname , $customer->firstname , $camp['name'] )) ,
$tpl_vars ,
Configuration::get('PS_SHOP_EMAIL') ,
null,
null,
null,
null,
null,
$path,
false, Context::getContext()->shop->id
);
++$emailsSent;
}
else {
PrestaShopLogger::addLog( 'Error when sending user email (tpl:'.$campM->getFileName().',customer:'.$customer->email.', campagne : '.$camp['name'] , 3 );
}
}
}
// log emailing results :
if( $emailsSent > 0 ) {
PrestaShopLogger::addLog( $emailsSent . ' emails sent for '.$camp['name'] . ' campaign' , 1 );
}
}
}
}
public function getBaseURL()
{
$this->context = Context::getContext();
return (Configuration::get('PS_SSL_ENABLED') ? 'https://' : 'http://').$this->context->shop->domain.$this->context->shop->getBaseURI();
}
public function checkIfCartIsOnCampaign( $abndate , $days , $hours )
{
// Set days = 0 for debug
if( DEBUG_SAC )
$days = 0;
// Abandoned cart date + Campaign Days and Hours
$time = strtotime( $abndate . ' + '.$days.' Days + '.$hours.' hours' );
$gAbnDate = date('Y-m-d H:i:0',$time);
// Now time ( cron should be fired every 30minutes (e.g : at 2 or 2:30 or 3 or 3:30...) so
// for 0 min check last 29mins (from 0 to 31mins) and for 30 check last 29mins (from 30 to 01);
$now = date('Y-m-d H:i:0');
$oldnow = date('Y-m-d H:i:0', time() - 60 * 29);
// Debug
/*
echo ' ADD ' . $days . ' JOUR and ' . $hours . ' HEURE '. '<br>';
echo ' NOW : ' .date('Y-m-d H:i:s') . '<br>';
echo ' CART ABONDONNE AT ' . $abndate. '<br>';;
echo ' plus campaign : ' . $gAbnDate . '<br>';
echo 'MIN TIME ' . $oldnow . '<br>';
*/
if( DEBUG_SAC )
echo $oldnow.' < '.$gAbnDate.' ---- '.$gAbnDate.' <= '.$now.'<br/><br/>';
if( strtotime($oldnow) < strtotime($gAbnDate) AND strtotime($gAbnDate) <= strtotime($now) ) {
return true;
}
return false;
}
}
$la = new LaunchCampaign();
$la->sendCampaign();