<?php
/*
MailBeez Automatic Trigger Email Campaigns
http://www.mailbeez.com
Copyright (c) 2010 MailBeez
inspired and in parts based on
Copyright (c) 2003 osCommerce
Released under the GNU General Public License
*/
// make path work from admin
require_once(DIR_FS_CATALOG. 'mailhive/common/classes/mailbeez.php');
// could be in language-file
// just easier to define it in the mailbee
define('MAILBEEZ_WINBACK_TEXT_TITLE', 'Winback Customer Reactivation');
define('MAILBEEZ_WINBACK_TEXT_DESCRIPTION', 'Show your customers that you miss them.<br>
<br>
If you haven\'t had a customer return to your site or make a purchase in e.g. six months or more, activate this win-back MailBeez Module that entices the customer to return.');
class winback extends mailbeez {
// class constructor
function winback() {
// call constructor
mailbeez::mailbeez();
// set some stuff:
$this->code = 'winback';
$this->module = 'winback'; // same as folder name
$this->version = '1.5'; // float value
$this->iteration = 1;
$this->title = MAILBEEZ_WINBACK_TEXT_TITLE;
$this->description = MAILBEEZ_WINBACK_TEXT_DESCRIPTION;
$this->sort_order = MAILBEEZ_WINBACK_SORT_ORDER;
$this->enabled = ((MAILBEEZ_WINBACK_STATUS == 'True') ? true : false);
$this->sender = MAILBEEZ_WINBACK_SENDER;
$this->sender_name = MAILBEEZ_WINBACK_SENDER_NAME;
$this->status_key = 'MAILBEEZ_WINBACK_STATUS';
//$this->icon = 'icon.png';
$this->documentation_key = $this->module; // leave empty if no documentation available
// $this->documentation_root = 'http:://yoursite.com/' // modify documentation root if necessary
$this->htmlBodyTemplateResource = 'body_html.tpl'; // located in folder of this module
$this->txtBodyTemplateResource = 'body_txt.tpl'; // located in folder of this module
$this->subjectTemplateResource = 'subject.tpl'; // located in folder of this module
$this->audience = array();
$this->additionalFields = array('customers_id' => '007', 'customers_email_address' => 'customer@mail.com', 'coupon_content' => 'Test coupon off', 'order_id' => '123456', 'order_date' => '12.12.2000', 'status_date' => '31.12.2000');
// list of additional fields to show in listing with testvalues used for Test-Mail
}
// class methods
function getAudience() {
global $languages_id;
//$status = MAILBEEZ_WINBACK_ORDER_STATUS_ID;
$status = 0 ;
$day_of_week = date(l,time());
$expires_date = time() + 60 * 60 * 23 ;
///Following block update daily_specials
mh_db_query("update " . TABLE_SPECIALS . "
set expires_date = DATE_ADD(now(), INTERVAL 1 DAY)
where daily_specials = '" . $day_of_week . "'");
//expired all others
mh_db_query("update " . TABLE_SPECIALS . "
set expires_date = DATE_ADD(now(), INTERVAL -1 DAY)
where daily_specials != '" . $day_of_week . "'");
// no special day get back to no expired
mh_db_query("update " . TABLE_SPECIALS . "
set expires_date = '0001-01-01'
where daily_specials ='Yes'");
///end of daily specials
//clean up and resend some old listing
$delete_track = "delete from " . TABLE_MAILBEEZ_TRACKING . "
where module ='" . $this->module . "'
and date_sent < DATE_SUB(CURRENT_DATE,INTERVAL 195 DAY) ";
mh_db_query($delete_track);
//exit if store is closed
$current_time = time();
$day_of_week = strtoupper(date(l,$current_time));
$isopen = constant('MODULE_SHIPPING_STOREHOURS_OPCL_' . $day_of_week);
if ( $isopen == 0 ) { exit;}
//coupon mail
$cp_sql = "select coupon_amount, coupon_minimum_order, coupon_code, coupon_type
from " . TABLE_COUPONS . "
where coupon_start_date <= now()
and coupon_expire_date >= now()
and coupon_type in ('F','P')
and coupon_minimum_order <2000
and coupon_active='Y' ORDER BY RAND() LIMIT 1";
$query_c = mh_db_query($cp_sql);
$coupon_content = '';
while ($cp_result = mh_db_fetch_array($query_c)) {
$coupon_minimum_order = '$'. round ($cp_result['coupon_minimum_order'],2);
$coupon_type = $cp_result['coupon_type'];
if ( $coupon_type == 'F') {
$coupon_amount = '$'. round ($cp_result['coupon_amount'], 2);
}else {
$coupon_amount = round($cp_result['coupon_amount'],4) .'%';
}
$coupon_content = 'We would like to send you ' .$coupon_amount. ' off coupon code: <b>'.$cp_result['coupon_code'] . '</b> when you order online with minimum order of '. $coupon_minimum_order .' (with restrictions).';
break;
}
//echo $coupon_content;
//eof coupon mail 022511
//free gift
if ( $coupon_content == '') {
$query_g = mh_db_query("SELECT fg.*, pd.products_name FROM (" . TABLE_CARROT . " fg, " . TABLE_PRODUCTS . " p)
LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON (pd.products_id=fg.products_id)
WHERE pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
AND p.products_id = fg.products_id
ORDER BY RAND() LIMIT 1");
while ($cp_result = mh_db_fetch_array($query_g)) {
$coupon_minimum_order = '$'. round ($cp_result['threshold'],2);
$coupon_content = 'You will get <b>'. $cp_result['products_name'] . '</b> when you order online with minimum order of '. $coupon_minimum_order .' (with restrictions).';
break;
}
}
//echo $coupon_content;
// MAILBEEZ_WINBACK_PASSED_DAYS
// MAILBEEZ_WINBACK_PASSED_DAYS_SKIP
$date_skip = $this->dbdate(MAILBEEZ_WINBACK_PASSED_DAYS_SKIP);
$date_passed = $this->dbdate(MAILBEEZ_WINBACK_PASSED_DAYS);
$query_raw = "select c.customers_firstname, c.customers_lastname, o.orders_id, o.customers_name,
o.customers_id, o.customers_email_address, o.date_purchased, s.date_added as status_date
from " . TABLE_ORDERS . " o
left join " . TABLE_CUSTOMERS . " c
on (o.customers_id = c.customers_id)
left join " . TABLE_ORDERS_STATUS_HISTORY . " s
on (o.orders_id = s.orders_id)
where o.orders_status = s.orders_status_id
and s.orders_status_id >= '" . (int)$status . "'
and s.date_added <= '" . $date_passed . "'
and s.date_added > '" . $date_skip . "'
order by o.orders_id DESC";
mh_db_query("SET OPTION SQL_BIG_SELECTS=1");
$query = mh_db_query($query_raw);
// for early check
$mb_chk = new mailbeez_mailer($this);
$mycount=0;
while ($item = mh_db_fetch_array($query)) {
// mandatory fields:
// - firstname
// - lastname
// - email_address
// - customers-id -> block
// other keys are replaced while sending: $<key>
// check if there was an order after the given timeframe
$last_order_id = $this->getLastOrderId($item['customers_id']);
if ($last_order_id > $item['orders_id']) {
// there is an order after the given timeframe
// skip
continue;
}
// early check to avoid processing when email was already sent
$chk_result = $mb_chk->check($this->module, $this->iteration, $item['customers_id'], $item['orders_id']);
if ($chk_result != false) {
// this iteration for this customer was already sent -> skip
continue;
}
$this->audience[$item['customers_id']] = array('firstname' => $item['customers_firstname'],
'lastname' => $item['customers_lastname'],
'coupon_content' => $coupon_content,
'email_address' => $item['customers_email_address'],
'customers_id' => $item['customers_id'],
'order_id' => $item['orders_id'],
'order_date' => mh_date_short($item['date_purchased']),
'status_date' => mh_date_short($item['status_date'])
);
$mycount++;
if ($mycount == 10) {break;}
}
return $this->audience;
}
// installation methods
function keys() {
return array('MAILBEEZ_WINBACK_STATUS', 'MAILBEEZ_WINBACK_ORDER_STATUS_ID', 'MAILBEEZ_WINBACK_PASSED_DAYS', 'MAILBEEZ_WINBACK_PASSED_DAYS_SKIP', 'MAILBEEZ_WINBACK_SENDER', 'MAILBEEZ_WINBACK_SENDER_NAME', 'MAILBEEZ_WINBACK_SORT_ORDER');
}
function install() {
mh_insert_config_value(array('configuration_title' => 'Send winback trigger email',
'configuration_key' => 'MAILBEEZ_WINBACK_STATUS',
'configuration_value' => 'False',
'configuration_description' => 'Do you want to send winback emails?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Set Order Status',
'configuration_key' => 'MAILBEEZ_WINBACK_ORDER_STATUS_ID',
'configuration_value' => '3',
'configuration_description' => 'Set the status of orders to identify date of last order',
'set_function' => 'mh_cfg_pull_down_order_statuses(',
'use_function' => 'mh_get_order_status_name'
));
mh_insert_config_value(array('configuration_title' => 'Set days passed',
'configuration_key' => 'MAILBEEZ_WINBACK_PASSED_DAYS',
'configuration_value' => '180',
'configuration_description' => 'number of days since last order',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'Set days to skip after',
'configuration_key' => 'MAILBEEZ_WINBACK_PASSED_DAYS_SKIP',
'configuration_value' => '200',
'configuration_description' => 'number of days after which do skip the winback email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'sender email',
'configuration_key' => 'MAILBEEZ_WINBACK_SENDER',
'configuration_value' => STORE_OWNER_EMAIL_ADDRESS,
'configuration_description' => 'sender email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'sender name',
'configuration_key' => 'MAILBEEZ_WINBACK_SENDER_NAME',
'configuration_value' => STORE_NAME,
'configuration_description' => 'sender email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'Sort order of display.',
'configuration_key' => 'MAILBEEZ_WINBACK_SORT_ORDER',
'configuration_value' => '30',
'configuration_description' => 'Sort order of display. Lowest is displayed first.',
'set_function' => ''
));
}
}
?>