static function getUserCart() { global $db, $user, $router; $sessAr = expSession::get('verify_shopper'); // initialize this users cart if they have ecomm installed. $active = $db->selectValue('modstate', 'active', 'module="storeController"'); if (!expModules::controllerExists('cart') || empty($active)) { // if ecomm is turned off, no cart. return null; } else { if (isset($router->params['controller']) && $router->params['controller'] == 'order' && ($router->params['action'] == 'verifyReturnShopper' || $router->params['action'] == 'verifyAndRestoreCart' || $router->params['action'] == 'clearCart') && (!isset($sessAr['validated']) || $sessAr['validated'] != true)) { return new order(); } else { // if ecomm is turned off, no cart. //$active = ; if (empty($active)) { return null; } $order = new order(); //initialize a new order object to use the find function from. $ticket = expSession::getTicketString(); //get this users session ticket. this is how we track anonymous users. // grab the origional referrer from the session table so that we can transfer it into the cart where it will be used for reporting purposes // sessions are temporary so we can't report on the referrer in the session table itsef because it may not be there // and we can't just get the referrer ar this point becaues the user likely navigated the site a bit and we want the origional referring site $orig_referrer = $db->selectValue('sessionticket', 'referrer', "`ticket`='" . $ticket . "'"); //see if we have a LIVE and ACTIVE session w/ cart and grab it if so $sessioncart = $order->find('first', "invoice_id='' AND sessionticket_ticket='" . $ticket . "'"); //check to see if the user is logged in, and if so grab their existing cart if (!empty($user) && $user->isLoggedIn()) { $usercart = $order->find('first', "invoice_id='' AND user_id=" . $user->id); } //eDebug($sessioncart); //eDebug($usercart); //enter here if we have NO ACTIVE SESSION CART -OR- We're awaiting a potential cart retore if (empty($sessioncart->id) || $sessAr['awaiting_choice'] == true) { if (empty($usercart->id)) { // no SESSION cart was found and user is not logged in... //let's see if they have a cart_id cookie set and we'll snag that if so //they won't have any user data, since they are "logged in" once they get to //checkout, so all we're really doing here is populating a cart for return //shoppers $cookie_cart_id = isset($_COOKIE['cid']) ? $_COOKIE['cid'] : 0; //eDebug($cookie_cart_id,true); if ($cookie_cart_id) { $tmpCart = new order($cookie_cart_id); if ($tmpCart->id != $cookie_cart_id) { //cookie set, but we gots no cart in the DB so act as if we had no cookie $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'user_id' => $user->id, 'orig_referrer' => $orig_referrer, 'return_count' => $cart->setReturnCount($orig_referrer))); order::setCartCookie($cart); } else { $u = new user($tmpCart->user_id); //1) Was Not logged in if (empty($tmpCart->user_id)) { $cart = new order($cookie_cart_id); //update the session ticket and return count $cart->update(array('sessionticket_ticket' => $ticket, 'return_count' => $cart->setReturnCount($orig_referrer))); order::setCartCookie($cart); flash('message', gt('Welcome back')); } else { if (!empty($tmpCart->user_id)) { //check for is admin first if ($u->isActingAdmin() || $u->isAdmin()) { //no need to restore anything. $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'user_id' => $user->id, 'orig_referrer' => $orig_referrer)); order::setCartCookie($cart); } else { if (!empty($tmpCart->user_id) && count($tmpCart->orderitem) == 0) { //silently copy tracking data from old order and continue on $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'user_id' => $user->id, 'orig_referrer' => $orig_referrer, 'return_count' => $tmpCart->setReturnCount($orig_referrer))); order::setCartCookie($cart); flash('message', gt('Welcome back')); } else { if (!empty($tmpCart->user_id) && count($tmpCart->orderitem) > 0) { //3) Was Logged in w/ NON-?real user? account //eDebug(expUtil::right($u->username,10),true); if ($u->isTempUser()) { if (isset($sessAr['validated']) && $sessAr['validated']) { //already went through validation and we're good to go $cart = new order($sessAr['cid']); //update the session ticket and return count $cart->update(array('sessionticket_ticket' => $ticket, 'return_count' => $cart->mergeReturnCount($sessioncart->return_count), 'orig_referrer' => $sessioncart->orig_referrer)); order::setCartCookie($cart); expSession::un_set('verify_shopper'); $user = new user($cart->user_id); expSession::login($user); //Update the last login timestamp for this user. $user->updateLastLogin(); flash('message', gt('Welcome back') . ' ' . $sessAr['firstname'] . '! ' . gt('Your shopping cart has been restored - you may continue shopping or') . ' <a href="' . makelink(array("controller" => "cart", "action" => "checkout")) . '">checkout</a> ' . gt('at your convenience.')); } else { //send to verification? If user has elected to restore their cart //eDebug($_SESSION); if (isset($sessAr['awaiting_choice']) && $sessAr['awaiting_choice'] == true) { /*expSession::set('verify_shopper',array('au'=>1,'orig_path'=>$router->current_url, 'firstname'=>$u->firstname, 'cid'=>$cookie_cart_id)); redirect_to(array("controller"=>"order",'action'=>'verifyReturnShopper')); orderController::verifyReturnShopper();*/ //just give em the sessioncart $cart = $sessioncart; if (count($cart->orderitem) > 0) { //added items to current cart, so we'll assume they do not want to restore the previous at this point expSession::un_set('verify_shopper'); order::setCartCookie($cart); } else { flash('message', gt('Welcome back') . ' ' . $u->firstname . '! ' . gt('We see that you have shopped with us before.') . '<br><br><a id="submit-verify" href="' . makelink(array("controller" => "order", "action" => "verifyReturnShopper")) . '" rel="nofollow">' . gt('Click Here to Restore Your Previous Shopping Cart') . '</a><br><br><a class="exp-ecom-link" href="' . makelink(array("controller" => "order", "action" => "clearCart", "id" => $cookie_cart_id)) . '">' . gt('Click Here To Start a New Shopping Cart') . '</a>'); $sessAr['orig_path'] = $router->current_url; expSession::set('verify_shopper', $sessAr); } } else { //first time...create a default cart, issue message, set session, rinse, repeat $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'return_count' => $cart->setReturnCount($orig_referrer))); expSession::set('verify_shopper', array('au' => 1, 'orig_path' => $router->current_url, 'firstname' => $u->firstname, 'cid' => $cookie_cart_id, 'awaiting_choice' => true)); //order::setCartCookie($cart); flash('message', gt('Welcome back') . ' ' . $u->firstname . '! ' . gt('We see that you have shopped with us before.') . '<br><br><a id="submit-verify" href="' . makelink(array("controller" => "order", "action" => "verifyReturnShopper")) . '" rel="nofollow">' . gt('Click Here to Restore Your Previous Shopping Cart') . '</a><br><br><a class="exp-ecom-link" href="' . makelink(array("controller" => "order", "action" => "clearCart", "id" => $cookie_cart_id)) . '">' . gt('Click Here To Start a New Shopping Cart') . '</a>'); } } } else { //prompt to login and restore, otherwise reset and start fresh //this should be all we need to do here //redirect_to(array("controller"=>"order",'action'=>'verifyReturnShopper','au'=>'0')); $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'user_id' => $user->id, 'orig_referrer' => $orig_referrer)); order::setCartCookie($cart); flash('message', gt('Welcome back') . ' ' . $u->firstname . '! ' . gt('If you would like to pick up where you left off, click here to login and your previous shopping cart will be restored.')); } } } } } } } } else { $cart = new order(); $cart->update(array("sessionticket_ticket" => $ticket, 'user_id' => $user->id, 'orig_referrer' => $orig_referrer)); order::setCartCookie($cart); } } else { //user is logged in, so we grab their usercart and update the session ticket only //$usercart->update(array('sessionticket_ticket'=>$ticket, 'orig_referrer'=>$orig_referrer)); $usercart->update(array('sessionticket_ticket' => $ticket)); $cart = $usercart; } //enter here if we HAVE an ACTIVE session/cart, but the user is not logged in } elseif (!empty($sessioncart->id) && $user->id == 0) { // the user isn't logged in yet...the session cart will do for now. $cart = $sessioncart; // if we hit here we've found a session cart AND a usercart because the user just logged in // and had both...that means we need to merge them } elseif (!empty($sessioncart->id) && !empty($usercart->id)) { // if we hit here we've found a session cart and a usercart...that means we need to merge them // if it's not the same cart. if ($sessioncart->id == $usercart->id) { $cart = $sessioncart; } else { // if the old user cart had gone through any of the checkout process before, than we // will clean that data out now and start fresh. $usercart->cleanOrderitems(); //merge the current session cart with previously saved user cart. foreach ($sessioncart->orderitem as $orderitem) { $orderitem->merge(array('orders_id' => $usercart->id, 'user_id' => $user->id)); } //if session cart HAS coupon codes, delete usercart codes and copy new code to usercart, else leave be if (count($sessioncart->getOrderDiscounts())) { foreach ($usercart->getOrderDiscounts() as $od) { $od->delete(); } foreach ($sessioncart->getOrderDiscounts() as $sod) { $sod->orders_id = $usercart->id; $sod->save(); } } $cart = new order($usercart->id); $sessioncart->delete(); } order::setCartCookie($cart); expSession::un_set('verify_shopper'); // the user doesn't have a cart with his/her user id in it. this probably means they just // logged in so we need to update the cart with the new user id information. } elseif (!empty($sessioncart->id) && (empty($usercart->id) && $user->isLoggedIn())) { //$sessioncart->update(array('user_id'=>$user->id, 'orig_referrer'=>$orig_referrer)); $sessioncart->update(array('user_id' => $user->id)); $cart = $sessioncart; } $cart->item_count = 0; foreach ($cart->orderitem as $items) { if ($items->product->requiresShipping && !$items->product->no_shipping) { $cart->shipping_required = true; } if ($items->product->requiresBilling) { $cart->billing_required = true; } $cart->item_count += $items->quantity; } $cart->lastcat = expSession::get('last_ecomm_category'); $cart->total = $cart->getCartTotal(); //eDebug($cart,true); return $cart; } } }
public function updateHistory($section = null) { global $db, $user; // if its not already set // configurable tracking length setcookie('UserUID', expSession::getTicketString(), 86400 * TRACKING_COOKIE_EXPIRES); $cookieID = empty($_COOKIE['UserUID']) ? expSession::getTicketString() : $_COOKIE['UserUID']; // Build out the object to insert into the db. // Get our parameters. $tmpParams = array(); foreach ($this->params as $key => $value) { if ($key != 'module' && $key != 'action' && $key != 'controller' && $key != 'section') { $tmpParams[$key] = $value; } } $trackingObject->params = serialize($tmpParams); if ($this->url_type == 'page' || $this->url_type == 'base') { $trackingObject->section = $section; } else { $trackingObject->module = $_SERVER['REQUEST_METHOD'] == 'POST' ? empty($_POST['controller']) ? $_POST['module'] : $_POST['controller'] : $this->url_parts[0]; $trackingObject->action = $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST['action'] : $this->url_parts[1]; } $trackingObject->referer = empty($_SERVER['HTTP_REFERER']) ? null : $_SERVER['HTTP_REFERER']; $trackingObject->cookieUID = $cookieID; $trackingObject->user_id = $user->id; $trackingObject->timestamp = time(); $trackingObject->user_address = $_SERVER['REMOTE_ADDR']; $trackingObject->user_agent = $_SERVER['HTTP_USER_AGENT']; $trackingObject->session_id = $_COOKIE['PHPSESSID']; $db->insertObject($trackingObject, 'tracking_rawdata'); }