Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

How to clear customers session and empty their cart?


chandrika

Recommended Posts

Posted

The reason I want to do this, is because I want to automate emptying customers carts after they have ordered when they have checked out using paypal without clicking return to mysite, so that final stage of the order process isnt done, their cart remains full and they are not notified. (only needs doing when customer doesnt have paypal a/c, usually it is done)

 

I see how to empty the cart using

tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $_POST['cid'] . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $_POST['cid'] . "'");

 

But am not sure how to clear their session data that also contains what was in the shopping cart when they ordered.

 

I have the specific customers_id in a variable...

 

would this work

 

tep_session_unregister('$customer_id');

 

I have also seen this

$cart->reset(true);

 

But not sure how to make that specific to a particular customers_id as I guess it relies on that customer being logged in.

Posted

The code for this is at the bottom of checkout_process.php :

$cart->reset(true);

// unregister session variables used during checkout
tep_session_unregister('sendto');
tep_session_unregister('billto');
tep_session_unregister('shipping');
tep_session_unregister('payment');
tep_session_unregister('comments');

If you are doing this outside the session, you could use something like

$whos_online_query = tep_db_query("select customer_id, full_name, ip_address, time_entry, time_last_click, last_page_url, session_id from " . TABLE_WHOS_ONLINE . " where customer_id = ''" . (int)$customer_id . "");

to get the relevant session_id and then delete it from the sessions table (where it is called a sesskey). You also need to delete from customers_basket separately. If you are not logged in as the customer, I don't think that it is worth it to try to manipulate the cart object.

 

Deleting the session row will log out the customer as well as clearing the other session variables. You should only do this when you know the customer completed a PayPal order without returning to your site.

Always back up before making changes.

Posted

@@ecartz Thankyou, that was so helpful. I had written a script that showed me when a customer had paid but not returned from paypal to complete the order process, so had not been notified etc.

 

Now I have been able to put a button by each such order that checks whether the customers last click was since the order, to make sure they are not online. Then if they are not, they are emailed an order confirmation, their cart is emptied, session deleted and order status history updated to show notification has been sent. If their last click was after the order time, the times are shown instead and links to manually update and email them are shown, so a choice can be made. Otherwise if they havent been back the email, session and cart are done by the script

 

This was getting to be a real pain, as customers without a paypal account, rarely click back to the site and I didnt like that they were not emailed even though they had paid. Plus of course their carts were not being emptied. I was doing it manually, but it was taking too much time.

 

My script is not pretty, but if anyone wants it as a basis for doing similar, it is below....(checking for which customers have not come back from paypal depends on the paypal payment standard module setting for "Paypal acknowledged order status" being set to "Processing" "4" (they could be changed in the script according to osc setup in the order status table) and the "preparing order status" to something else.

 

 

<?php

require('includes/application_top.php');
require(DIR_WS_CLASSES . 'currencies.php');
include(DIR_WS_CLASSES . 'order.php');
//set var
$orders_to_confirm = "no";
//if customer id is posted
if ( isset($_POST["cid"]) ) {

//get time of order
$order_time_query = tep_db_query("select date_purchased, customers_name, customers_email_address from orders where orders_id = '" . $_POST["oid"] . "'");
while ($order_time1 = tep_db_fetch_array($order_time_query)){
$order_time = $order_time1['date_purchased'];
$order_time = strtotime($order_time);
$order_time = date("Y-m-d G:i:s",$order_time);
$cust_name = $order_time1['customers_name'];
$cust_email = $order_time1['customers_email_address'];

}
//get customers session id
$whos_online_query = tep_db_query("select time_last_click, session_id from whos_online where customer_id = '" . $_POST["cid"] . "'");
while($whos_online = tep_db_fetch_array($whos_online_query)) {
$sess_id = $whos_online['session_id'];
$time_last = $whos_online['time_last_click'];
$time_last = date("Y-m-d G:i:s",$time_last);

//check if time of last click is before or at time of order, as if so customer may be back on the site
if ($time_last <= $order_time){
//delete session and empty cart
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $_POST['cid'] . "'");
tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $_POST['cid'] . "'");
tep_db_query("delete from sessions where sesskey = '" . $sess_id . "'");
echo "<strong>Customers session deleted and cart emptied</strong><br />";
//send email
$email_orderconfirm = "Thankyou for your order at XXXXXX, to view your order details please visit <a href='https://www. xxxx .co.uk/account_history.php'>https://www. xxxxx .co.uk/account_history.php</a> \n\nYou will be notified when your goods are shipped.";
tep_mail($cust_name, $cust_email, "XXXXXXX : Order ".$_POST['oid']." Confirmed", $email_orderconfirm, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
//update status
tep_db_query("update orders_status_history set customer_notified='1' where orders_id = '".$_POST['oid']."' AND comments LIKE '%PayPal IPN Verified%'");
echo "<br /><strong>Customers emailed and status updated.</strong><br />";

echo "<br />Check for more un-notified orders ... <a href='http://www. XXXXXX .co.uk/admin/check_notify_order.php'><strong>Check Here</strong></a><br /><br />";

}//endif
else { echo "<strong>Customer may be back online so nothing has been done.</strong><br /><br />Time of order was ".$order_time."<br />Time of last click was ".$time_last."<br />Order ID was ".$_POST["oid"]."<br />Login is ".$cust_email."<br /><br />";
echo $_POST['oid']." is processing. <a href='http://www. XXXXX .co.uk/admin/orders.php?page=1&oID=".$_POST['oid']."&action=edit'>Order</a>  <a href='http://www. XXXXX .co.uk/'>Website</a><br /><br />";
//next line is text that can be copied and pasted manually into the comments in order edit link
echo "Thankyou for your order at XXXXXX, to view your order details please visit
https://www. XXXXX.co.uk/account_history.php<br /><br />You will be notified when your goods are shipped.<br /><a href='http://www. XXXXX .co.uk/admin/orders.php'>Orders</a><br /><hr /><br />";
}

}//endwhile

unset($_POST["cid"]);
unset($_POST["oid"]);
exit();
}//end if customer id is posted
echo "<strong>ORDER CONFIRMATION STATUS...</strong><br /><hr /><br />";
//check for orders processing 4
$check_status_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where orders_status = '4'");
 while($check_status = tep_db_fetch_array($check_status_query)) {

 $myoid=$check_status['orders_id'];


//check whether the customer has been notified
$check_cust_notified_query = tep_db_query("select customer_notified, orders_status_id, comments from orders_status_history where orders_id = '".$myoid."'");
while($check_cust_notified = tep_db_fetch_array($check_cust_notified_query)) {

$notified=$check_cust_notified['customer_notified'];
$order_status_id=$check_cust_notified['orders_status_id'];
$comments=$check_cust_notified['comments'];
 $pos = strpos($comments,"PayPal IPN Verified [Completed");
 if($pos === false) {
 //check if notified
 if ($notified=="1")
 //echo "<font color='#FF0000'>".$myoid." customer notified</font><br />";
 $is_notified= "isnotified";

 }

 else {
 // string needle found in haystack

 }

}//end while check_cust_notified_query
if (!$is_notified){
echo $myoid." is processing. <a href='http://www. XXXXXX.co.uk/admin/orders.php?page=1&oID=".$myoid."&action=edit'>Order</a>  <a href='http://www. XXXXXX .co.uk/'>Website</a><br />";
echo "<br /><font color='#FF0000'>NOT NOTIFIED</font><br />";

//empty customers cart
$custid_query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id = '".$check_status['orders_id']."'");
 while($custid = tep_db_fetch_array($custid_query)) {
$cid=$custid['customers_id'];
 echo "<br />Customer ID is ".$cid;
 ?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Cust ID : <input type="text" size="20" name="cid" value="<?php echo $cid; ?>"/>
Order ID : <input type="text" size="20" name="oid" value="<?php echo $check_status['orders_id']; ?>"/>
<input type="submit" name="empty" value="Email, Empty Cart & Delete Session"/>
</form>
	 <?php
 echo "<hr />";
 $orders_to_confirm = "yes";
}//end while cust id
///end empty cart

}//end if
unset($is_notified);

}//end while check_status_query
if ($orders_to_confirm == "no"){echo "<br />All orders are properly confirmed and customers notified :)";}

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

Posted

Just in case anyone does ever try the script I put, he above code has a small problem if the non return has logged off and so to fix that, the following code addresses that...i couldnt see an option to just edit the above post, so thought i better put it here.

 

<?php

require('includes/application_top.php');
require(DIR_WS_CLASSES . 'currencies.php');
include(DIR_WS_CLASSES . 'order.php');
//set var
$orders_to_confirm = "no";
//if customer id is posted
if ( isset($_POST["cid"]) && isset($_POST["oid"]) ) {
//get time of order
$order_time_query = tep_db_query("select date_purchased, customers_name, customers_email_address from orders where orders_id = '" . $_POST["oid"] . "'");
while ($order_time1 = tep_db_fetch_array($order_time_query)){
 $order_time = $order_time1['date_purchased'];
 $order_time = strtotime($order_time);
 $order_time = date("Y-m-d G:i:s",$order_time);
 $cust_name = $order_time1['customers_name'];
 $cust_email = $order_time1['customers_email_address'];

}
//get customers session id
$whos_online_query = tep_db_query("select time_last_click, session_id from whos_online where customer_id = '" . $_POST["cid"] . "'");
while($whos_online = tep_db_fetch_array($whos_online_query)) {
$sess_id = $whos_online['session_id'];
$time_last = $whos_online['time_last_click'];
 $time_last = date("Y-m-d G:i:s",$time_last);
/*echo "sessid is ".$sessid."<br />";
echo "timelast is ".$time_last."<br />";
echo "ordertime is is ".$order_time."<br />";
print_r($whos_online);*/
}//endwhile 
//check if time of last click is before or at time of order, as if so customer may be back on the site
if ($time_last <= $order_time || !isset($time_last)){
 //delete session and empty cart
 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET . " where customers_id = '" . $_POST['cid'] . "'");
 tep_db_query("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . " where customers_id = '" . $_POST['cid'] . "'");
 tep_db_query("delete from sessions where sesskey = '" . $sess_id . "'");
 echo "<strong>Customers session deleted and cart emptied</strong><br />";
 //send email
 $email_orderconfirm = "Thankyou for your order at XXXXX, to view your order details please visit <a href='https://www. XXXX .co.uk/account_history.php'>https://www. XXXX .co.uk/account_history.php</a> \n\nYou will be notified when your goods are shipped.";
 tep_mail($cust_name, $cust_email, "XXXXX : Order ".$_POST['oid']." Confirmed", $email_orderconfirm, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
 //update status
 //tep_db_query("update orders_status_history set customer_notified='1' where orders_id = '".$_POST['oid']."' AND comments LIKE '%PayPal IPN Verified%'");
 $timenow=date("Y-m-d G:i:s");

 tep_db_query("INSERT INTO orders_status_history (orders_id, orders_status_id, date_added,customer_notified,comments)
VALUES ('".$_POST['oid']."', '4', '".$timenow."','1','confirmed')");
 echo "<br /><strong>Customers emailed and status updated.</strong><br />";

 echo "<br />Check for more un-notified orders ... <a href='http://www. XXXX .co.uk/admin/check_notify_order.php'><strong>Check Here</strong></a><br /><br />";

 }//endif
 else { echo "<strong>Customer may be back online so nothing has been done.</strong><br /><br />Time of order was ".$order_time."<br />Time of last click  was ".$time_last."<br />Order ID was ".$_POST["oid"]."<br />Login is ".$cust_email."<br /><br />";
 echo $_POST['oid']." is processing. <a href='http://www. XXXX .co.uk/admin/orders.php?page=1&oID=".$_POST['oid']."&action=edit'>Order</a>  <a href='http://www. XXXX .co.uk/'>Website</a><br /><br />";
 echo "Thankyou for your order at XXXXX, to view your order details please visit
https://www. XXXX .co.uk/account_history.php<br /><br />You will be notified when your goods are shipped.<br /><a href='http://www. XXXX .co.uk/admin/orders.php'>Orders</a><br /><hr /><br />";
 }

//}//endwhile

unset($_POST["cid"]);
unset($_POST["oid"]);
exit();
}//end if customer id is posted
echo "<strong>ORDER CONFIRMATION STATUS...</strong><br /><hr /><br />";
//check for orders processing 4
$check_status_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where orders_status = '4'");
  while($check_status = tep_db_fetch_array($check_status_query)) {

    $myoid=$check_status['orders_id'];


  //check whether the customer has been notified
 $check_cust_notified_query = tep_db_query("select customer_notified, orders_status_id, comments  from orders_status_history where orders_id = '".$myoid."'");
  while($check_cust_notified = tep_db_fetch_array($check_cust_notified_query)) {

   $notified=$check_cust_notified['customer_notified'];
   $order_status_id=$check_cust_notified['orders_status_id'];
   $comments=$check_cust_notified['comments'];
 $pos = strpos($comments,"PayPal IPN Verified [Completed");
 if($pos === false) {
  //check if notified
  if ($notified=="1")
  //echo "<font color='#FF0000'>".$myoid." customer notified</font><br />";
  $is_notified= "isnotified";

 }

 else {
  // string needle found in haystack

 }

 }//end while check_cust_notified_query
 if ($is_notified!='isnotified'){
  echo $myoid." is processing. <a href='http://www. XXXX .co.uk/admin/orders.php?page=1&oID=".$myoid."&action=edit'>Order</a>  <a href='http://www. XXXX .co.uk/'>Website</a><br />";
  echo "<br /><font color='#FF0000'>NOT NOTIFIED</font><br />";

  //empty customers cart
$custid_query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id = '".$check_status['orders_id']."'");
  while($custid = tep_db_fetch_array($custid_query)) {
   $cid=$custid['customers_id'];
 echo "<br />Customer ID is ".$cid;
 ?>

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
Cust ID : <input type="text" size="20" name="cid" value="<?php echo $cid; ?>"/>
Order ID : <input type="text" size="20" name="oid" value="<?php echo $check_status['orders_id']; ?>"/>
<input type="submit" name="empty" value="Email, Empty Cart & Delete Session"/>
</form>  
	   <?php
 echo "<hr />";
 $orders_to_confirm = "yes";
  }//end while cust id
///end empty cart

  }//end if
   unset($is_notified);

}//end while check_status_query
if ($orders_to_confirm == "no"){echo "<br />All orders are properly confirmed and customers notified :)";}

require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

Posted

Hi

 

Not sure what version of a PayPal module you are using but what you could do is find the bit of code that sends your customers to PayPal and pop Matts chunk from the bottom of checkout_process immediately before that. (there's so many PayPal modules around that guessing where it is can be a total joke)

 

Just so long as the PayPal url/data is already formatted then $cart->reset(true) and killing off the sessions is not going to do any damage as the order is already saved in your db

 

Have a look for a PayPal IPN module - these have a file that PayPal will initiate a callback to if advised (one I've worked on recently had a file called paypal_notify.php). That file will usually just updates an order status but you can also add in the sort of code that you've created so that it will send the emails and so on

 

Bob

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...