How do I make a cart persistent?

Permalink
I am in the process of moving a site to C5 eCommerce. My client has customers who login and build a cart over several weeks of consideration through numerous login and logout, with browsers closing, PCs being switched off and such.

The current C5 cart appears to be destroyed at the end of a browser session. This is just about OK for an anonymous user, but will not be acceptable for my client's long term users who are used to their cart being persistent across successive logins.

Is there something wrong with my setup, or is non-persistent carts the designed behaviour?

Any tips on how to change this to be persistent?

Ideally I would like to tie a cart to a cookie for anonymous users, and to a user's account for those that login.

JohntheFish
 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
With some pointers from Ryan here is my solution. It has been working reliably on a live site for almost a month now.

Place the code below in the theme header.
$enc = Loader::helper('encryption');
Loader::model('order/model', 'core_commerce');
if($_COOKIE['savedCartID']) {
    $cookie_cart_id = $enc->decrypt($_COOKIE['savedCartID']);
    //advLog::addEntry($cookie_cart_id ,'Pcart cookie starts');
    // ignore cookie unless it is a valid order
    if (!empty($cookie_cart_id) && CoreCommerceOrder::isValidOrderID($cookie_cart_id)){
        $cookie_cart_order = new CoreCommerceOrder();
        $cookie_cart_order->load($cookie_cart_id);
        $cookie_cart_status = $cookie_cart_order->getStatus();
        //advLog::addEntry($cookie_cart_status ,'Pcart cookie status');
        // ignore cookie if it has not been checked out
        if ($cookie_cart_status > 0){
            // cookie cart has been ordered, so clear
            setcookie( 'savedCartID', '', time() - 60);


There may be a better solution using events (which Ryan also gave some tips for), but this was quick and easy