Code:
<?php
/**
* stems from:
* @version $ Id: recover_cart_sales.php 2.12 17.09.2006 22:25 Andrew Berezin $
*/
function rcs_delete_entry($custID) {
// Delete Entry Begin
if ($_GET['action'] == 'delete') {
$customer = $db->Execute("SELECT customers_firstname, customers_lastname
FROM " . TABLE_CUSTOMERS . "
WHERE customers_id ='" . (int)$custID. "' LIMIT 1");
$db->Execute("DELETE FROM " . TABLE_CUSTOMERS_BASKET . " WHERE customers_id='" . (int)$custID . "'");
$db->Execute("DELETE FROM " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " WHERE customers_id='" . (int)$custID . "'");
$messageStack->add(MESSAGE_STACK_CART_CUSTOMER . $customer->fields['customers_firstname'] . ' ' . $customer->fields['customers_lastname'] . ' (ID ' . $custID . ')' . MESSAGE_STACK_DELETE_SUCCESS, 'success');
}
// Delete Entry End
}
// Set Contacted Begin
function rcs_set_contacted($custID, $displayMsgs = TRUE) {
if (!is_array($custID)) $custID = array($custID);
foreach($custID as $customer) {
$customer = $db->Execute("SELECT customers_firstname, customers_lastname
FROM " . TABLE_CUSTOMERS . "
WHERE customers_id ='" . (int)$customer . "' LIMIT 1");
// See if a record for this customer already exists; if not create one and if so update it
$donequery = $db->Execute("SELECT * FROM ". TABLE_SCART ." WHERE customers_id = '" . (int)$customer . "'");
if ($donequery->RecordCount() == 0)
$db->Execute("INSERT INTO " . TABLE_SCART . " (customers_id, dateadded, datemodified) VALUES ('" . (int)$customer . "', '" . date('Ymd') . "', '" . date('Ymd') . "')");
else
$db->Execute("UPDATE " . TABLE_SCART . " SET datemodified = '" . date('Ymd') . "' WHERE customers_id = " . (int)$customer);
if ($displayMsgs) $messageStack->add(MESSAGE_STACK_CUSTOMER . $customer->fields['customers_firstname'] . ' ' . $customer->fields['customers_lastname'] . ' (ID ' . $customer . ')' . MESSAGE_STACK_SETCONACTED, 'success');
}
}
// Set Contacted End
//This function already in admin/includes/functions/extra_functions/functions_recover_cart_sales.php
// function zen_cart_date_short($raw_date) {
// if ($raw_date <= 0)
// return false;
// $year = substr($raw_date, 0, 4);
// $month = (int)substr($raw_date, 4, 2);
// $day = (int)substr($raw_date, 6, 2);
// if (@date('Y', mktime(0, 0, 0, $month, $day, $year)) == $year) {
// return date(DATE_FORMAT, mktime(0, 0, 0, $month, $day, $year));
// } else {
// return ereg_replace('2037' . '$', $year, date(DATE_FORMAT, mktime(0, 0, 0, $month, $day, 2037)));
// }
//}
//This function already in admin/includes/functions/extra_functions/functions_recover_cart_sales.php
// This will return a list of active customers
//function zen_GetCustomerOnline() {
// global $db;
// $members = array();
// Set expiration time, default is 1200 secs (20 mins)
$xx_mins_ago = (time() - 1200);
$whos_online_query = $db->Execute("SELECT customer_id
FROM " . TABLE_WHOS_ONLINE . "
WHERE time_last_click > '" . $xx_mins_ago . "'");
while (!$whos_online_query->EOF) {
if ($whos_online_query->fields['customer_id'] != 0) {
$members[] = $whos_online_query->fields['customer_id'];
}
$whos_online_query->MoveNext();
}
return $members;
//
function rcs_send_recover_cart_emails($emails_array, $tdate, $messageHTML = '', $messageTEXT = '', $subject = '', $from = '') {
global $db, $currencies;
$file = DIR_FS_SQL_CACHE . '/' . 'RecoverCartSales-' . date('Ymd-His') . '-log.html';
$fp = @fopen($file, 'a');
@fwrite($fp, '<table><tr><td>Recover Cart Sales - LOG</td></tr><tr><td>' . date('M-d-Y h:i:s') . '</td></tr></table><hr>');
@fwrite($fp, '<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td class="pageHeading" align="left" colspan=6>'. HEADING_EMAIL_SENT.'</td>
</tr>
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent" align="left" colspan="1" width="15%">'.TABLE_HEADING_CUSTOMER.'</td>
<td class="dataTableHeadingContent" align="left" colspan="1" width="30%"> </td>
<td class="dataTableHeadingContent" align="left" colspan="1" width="25%"> </td>
<td class="dataTableHeadingContent" align="left" colspan="1" width="10%"> </td>
<td class="dataTableHeadingContent" align="left" colspan="1" width="10%"> </td>
<td class="dataTableHeadingContent" align="left" colspan="1" width="10%"> </td>
</tr>
<tr class="dataTableHeadingRow">
<td class="dataTableHeadingContent" align="left" colspan="1" width="15%">'. TABLE_HEADING_MODEL.'</td>
<td class="dataTableHeadingContent" align="left" colspan="2" width="55%">'. TABLE_HEADING_DESCRIPTION.'</td>
<td class="dataTableHeadingContent" align="center" colspan="1" width="10%"> '. TABLE_HEADING_QUANTY.'</td>
<td class="dataTableHeadingContent" align="right" colspan="1" width="10%">'. TABLE_HEADING_PRICE.'</td>
<td class="dataTableHeadingContent" align="right" colspan="1" width="10%">'. TABLE_HEADING_TOTAL.'</td>
</tr>');
@fclose($fp);
$messageHTML = ($messageHTML == '') ? zen_db_prepare_input($_POST['message_html']) : $messageHTML;
$messageTEXT = ($messageTEXT == '') ? zen_db_prepare_input($_POST['message']) : $messageTEXT;
$from = ($from == '') ? zen_db_prepare_input($_POST['from']) : $from; // STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS
$subject = ($subject == '') ? zen_db_prepare_input($_POST['subject']) : $subject; // EMAIL_TEXT_SUBJECT
foreach ($emails_array as $cid) {
$mline = '';
$cline = '';
$lastcid = 0;
$tprice = 0;
$basket = $db->Execute("SELECT cb.products_id,
cb.customers_basket_quantity,
cb.customers_basket_date_added,
cus.customers_firstname fname,
cus.customers_lastname lname,
cus.customers_email_address email
FROM " . TABLE_CUSTOMERS_BASKET . " cb,
" . TABLE_CUSTOMERS . " cus
WHERE cb.customers_id = cus.customers_id AND
cus.customers_id ='" . $cid . "'
ORDER BY cb.customers_basket_date_added DESC ");
while (!$basket->EOF) {
// set new cline and curcus
if ($lastcid != $cid) {
if ($lastcid != "") {
$cline .= "
<tr>
<td class='dataTableContent' align='right' colspan='6'><b>" . TABLE_CART_TOTAL . "</b>" . $currencies->format($tprice) . "</td>
</tr>
<tr>
<td colspan='6' align='right'><a href=" . zen_href_link(FILENAME_RECOVER_CART_SALES, "action=delete&customer_id=" . $cid . "&tdate=" . $tdate) . ">" . zen_image_button('button_delete.gif', IMAGE_DELETE) . "</a></td>
</tr>\n";
echo $cline;
}
$cline .= "<tr> <td class='dataTableContent' align='left' colspan='6'><a href='" . zen_href_link(FILENAME_CUSTOMERS, 'search=' . $basket->fields['lname']) . "'>" . $basket->fields['fname'] . " " . $basket->fields['lname'] . "</a>".$customer."</td></tr>";
$tprice = 0;
}
$lastcid = $cid;
$products = $db->Execute("SELECT p.products_model model,
pd.products_name name
FROM " . TABLE_PRODUCTS . " p,
" . TABLE_PRODUCTS_DESCRIPTION . " pd
WHERE p.products_id = '" . $basket->fields['products_id'] . "'
AND pd.products_id = p.products_id
AND pd.language_id = " . (int)$_SESSION['languages_id']);
$sprice = zen_get_products_actual_price($basket->fields['products_id']);
$tprice += $basket->fields['customers_basket_quantity'] * $sprice;
$cline .= "<tr class='dataTableRow'>
<td class='dataTableContent' align='left' width='15%'>" . $products->fields['model'] . "</td>
<td class='dataTableContent' align='left' colspan='2' width='55%'><a href='" . zen_href_link(FILENAME_CATEGORIES, 'action=new_product_preview&read=only&pID=' . $basket->fields['products_id'] . '&origin=' . FILENAME_RECOVER_CART_SALES . '?page=' . $_GET['page']) . "'>" . $products->fields['name'] . "</a></td>
<td class='dataTableContent' align='center' width='10%'>" . $basket->fields['customers_basket_quantity'] . "</td>
<td class='dataTableContent' align='right' width='10%'>" . $currencies->format($sprice) . "</td>
<td class='dataTableContent' align='right' width='10%'>" . $currencies->format($basket->fields['customers_basket_quantity'] * $sprice) . "</td>
</tr>";
$mline .= $basket->fields['customers_basket_quantity'] . ' x ' . $products->fields['name'] . "\n";
$mline .= ' <blockquote><a href="' . zen_catalog_href_link(FILENAME_PRODUCT_INFO, 'products_id='. $basket->fields['products_id']) . '">' . zen_catalog_href_link(FILENAME_PRODUCT_INFO, 'products_id='. $basket->fields['products_id']) . "</a></blockquote>\n\n";
$basket->MoveNext();
}
$cline .= "</td></tr>";
// E-mail Processing - Requires EMAIL_* defines in the
// includes/languages/english/recover_cart_sales.php file
$email = '';
if (RCS_EMAIL_FRIENDLY == 'true'){
$email .= EMAIL_TEXT_SALUTATION . $basket->fields['fname'] . ' ' . $basket->fields['lname'] . ",";
} else {
$email .= STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n";
}
$cquery = $db->Execute("SELECT * FROM " . TABLE_ORDERS . " WHERE customers_id = '" . $cid . "'" );
if ($cquery->RecordCount() < 1) {
$email .= sprintf(EMAIL_TEXT_NEWCUST_INTRO, $mline);
} else {
$email .= sprintf(EMAIL_TEXT_CURCUST_INTRO, $mline);
}
$email .= EMAIL_TEXT_BODY_HEADER . $mline . EMAIL_TEXT_BODY_FOOTER;
if( EMAIL_USE_HTML == 'true' )
$email .= '<a href="' . zen_catalog_href_link(FILENAME_DEFAULT) . '">' . STORE_OWNER . "\n" . zen_catalog_href_link(FILENAME_DEFAULT) . '</a>';
else
$email .= STORE_OWNER . "\n" . zen_catalog_href_link(FILENAME_DEFAULT);
$email .= "\n\n";
$email .= "\n" . EMAIL_SEPARATOR . "\n\n";
$email .= EMAIL_TEXT_LOGIN;
if( EMAIL_USE_HTML == 'true' )
$email .= ' <a href="' . zen_catalog_href_link(FILENAME_LOGIN, '', 'SSL') . '">' . zen_catalog_href_link(FILENAME_LOGIN, '', 'SSL') . '</a>';
else
$email .= ' (' . zen_catalog_href_link(FILENAME_LOGIN, '', 'SSL') . ')';
$custname = $basket->fields['fname']." ".$basket->fields['lname'];
$outEmailAddr = '"' . $custname . '" <' . $basket->fields['email'] . '>';
if( zen_not_null(RCS_EMAIL_COPIES_TO) )
$outEmailAddr .= ', ' . RCS_EMAIL_COPIES_TO;
$html_msg['EMAIL_MESSAGE_HTML'] = $email;
$email = strip_tags($email . "\n\n" . zen_db_prepare_input($messageTEXT));
zen_mail('', $outEmailAddr, $subject, $email, '', $from, $html_msg);
// LOGGING INSTEAD OF EMAIL
$fp = @fopen(DIR_FS_SQL_CACHE . '/RCSmessages-'.time().'.txt', 'a');
@fwrite($fp, date("Y-m-d h:m:s") . ' - RCS Message to: ' . $outEmailAddr . "\n\tSubject: " . $subject . "\n\tFrom: " . $from . "\n\t Message: " . $email . "\n\n");
@fclose($fp);
$mline = "";
// See if a record for this customer already exists; if not create one and if so update it
$donequery = $db->Execute("SELECT * FROM ". TABLE_SCART ." WHERE customers_id = '" . $cid . "'");
if ($donequery->RecordCount() == 0)
$db->Execute("INSERT INTO " . TABLE_SCART . " (customers_id, dateadded, datemodified) VALUES ('" . $cid . "', '" . date('Ymd') . "', '" . date('Ymd') . "')");
else
$db->Execute("UPDATE " . TABLE_SCART . " SET datemodified = '" . date('Ymd') . "' WHERE customers_id = " . $cid );
$fp = @fopen($file, 'a');
@fwrite($fp, $cline . "\n\n");
@fclose($fp);
// echo $cline;
// $cline = "";
}
$fp = @fopen($file, 'a');
@fwrite($fp, ' <tr><td colspan=8 align="right" class="dataTableContent"><b>' . TABLE_CART_TOTAL . $currencies->format($tprice) . '</td> </tr>
<tr><td colspan=6 align="right"><a href="'. zen_href_link(FILENAME_RECOVER_CART_SALES, "action=delete&customer_id=" . $cid . "&tdate=" . $tdate).'">'. zen_image_button('button_delete.gif', IMAGE_DELETE).'</a></td></tr>
<tr><td colspan=6 align=center><a href="'. zen_href_link(FILENAME_RECOVER_CART_SALES).'">'. TEXT_RETURN.'</a></td></tr>
</table>');
@fwrite($fp, '<div>Finished: '. date('Y-m-d h:m:s') . '</div>');
@fclose($fp);
return $cline;
}
function doUnattendedRCS($tdate = '') {
global $db;
$tdate = ($tdate == '') ? RCS_BASE_DAYS : $tdate;
$custArray = array();
$cust_ses_ids = zen_GetCustomerOnline();
$basket = $db->Execute("SELECT cb.customers_id,
cb.products_id,
cb.customers_basket_quantity,
cb.customers_basket_date_added,
cus.customers_firstname,
cus.customers_lastname,
cus.customers_telephone,
cus.customers_email_address,
sc.datemodified
FROM " . TABLE_CUSTOMERS_BASKET . " cb
LEFT JOIN " . TABLE_CUSTOMERS . " cus ON (cb.customers_id = cus.customers_id)
LEFT JOIN " . TABLE_SCART . " sc ON (cb.customers_id = sc.customers_id)
WHERE cb.customers_basket_date_added >= '" . date('Ymd', time()-$tdate*24*60*60) . "'
AND cb.customers_id NOT IN ('" . implode(", ", $cust_ses_ids) . "')
ORDER BY cb.customers_id ASC, cb.customers_basket_date_added DESC");
$curcus = 0;
$skip = false;
while (!$basket->EOF) {
// If this is a new customer, create the appropriate HTML
if ($curcus != $basket->fields['customers_id']) {
$curcus = $basket->fields['customers_id'];
$checked = 1; // assume we'll send an email
$skip = false;
$sentdate = "";
$customer = $basket->fields['customers_firstname'] . " " . $basket->fields['customers_lastname'];
if ((time()-(RCS_EMAIL_TTL*24*60*60)) <= strtotime($basket->fields['datemodified'])) {
$sentdate = $basket->fields['datemodified'];
$checked = 0;
}
// See if the customer has purchased from us before
// Customers are identified by either their customer ID or name or email address
// If the customer has an order with items that match the current order, assume
// order completed, bail on this entry!
$orec = $db->Execute('SELECT orders_id, orders_status
FROM ' . TABLE_ORDERS . '
WHERE (customers_id = ' . (int)$curcus . '
OR customers_email_address like "' . $basket->fields['customers_email_address'] .'"
OR customers_name like "' . $basket->fields['customers_firstname'] . ' ' . $basket->fields['customers_lastname'] . '")
AND date_purchased >= "' . $basket->fields['customers_basket_date_added'] . '"' );
if ($orec->RecordCount() > 0) {
// We have a matching order; assume current customer but not for this order
// Now, look to see if one of the orders matches this current order's items
while(!$orec->EOF) {
$ccquery = $db->Execute('SELECT products_id
FROM ' . TABLE_ORDERS_PRODUCTS . '
WHERE orders_id = ' . (int)$orec->fields['orders_id'] . '
AND products_id = ' . (int)(int)$basket->fields['products_id'] );
if( $ccquery->RecordCount() > 0 ) {
if( $orec->fields['orders_status'] > RCS_PENDING_SALE_STATUS )
$checked = 0;
// OK, we have a matching order; see if we should just skip this or show the status
if( RCS_SKIP_MATCHED_CARTS == 'true' && !$checked ) {
$skip = true; // reset flag & break us out of the while loop!
break;
}
}
$orec->MoveNext();
}
if( $skip )
continue; // got a matched cart, skip to next one
}
// flag this customer to be auto-processed
if ($checked) $custArray[] = $curcus;
}
$basket->MoveNext();
}
if (sizeof($custArray) > 0) {
echo 'Preparing to send emails to ' . sizeof($custArray) . ' recipients ...' . "\n";
rcs_send_recover_cart_emails($custArray, RCS_BASE_DAYS, ' ', ' ', EMAIL_TEXT_SUBJECT, STORE_OWNER_EMAIL_ADDRESS);
// rcs_set_contacted($custArray, FALSE);
} else {
echo 'No recipients to process.' . "\n";
}
}
define('RCS_TEXT_MESSAGE_SAMPLE', 'Please pardon the interruption. We noticed that you began a shopping session at our store, but did not complete your checkout. If you encountered a technical problem, please telephone us at the number below so we can help you finish your purchase or answer any questions you may have about our products. ' . "\n\n" . 'If you abandoned your shopping cart in favor of better prices elsewhere, please let us know so we can have an opportunity to match or better those prices.' . "\n\n" . 'If you left your shopping cart for a more opportune time, we invite you to come back and complete your shopping session. We understand that sometimes it\'s not convenient to complete a purchase because of distractions or not having payment details handy. If that\'s the case for you, please consider this just a friendly reminder that your shopping cart is still sitting here waiting for you. ' . "\n\n" . 'Once again, please pardon us for sending you this email. We simply want to make sure your shopping experience with us is as efficient as possible. We will not be bothering you about this particular shopping session again.' . "\n\n" . 'Have a great day!');
Bookmarks