/**
  * When the user logs in, their session cart should override their db-stored cart.
  * Current actions take precedence
  *
  * @param $user
  * @param $options
  * @return unknown_type
  */
 private function doLoginUser($user, $options = array())
 {
     $session = JFactory::getSession();
     $old_sessionid = $session->get('old_sessionid');
     $user['id'] = intval(JUserHelper::getUserId($user['username']));
     // Should check that Citruscart is installed first before executing
     if (!$this->_isInstalled()) {
         return;
     }
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $helper = new CitruscartHelperCarts();
     if (!empty($old_sessionid)) {
         $helper->mergeSessionCartWithUserCart($old_sessionid, $user['id']);
         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
         $wishlist_model = JModelLegacy::getInstance('WishlistItems', 'CitruscartModel');
         $wishlist_model->setUserForSessionItems($old_sessionid, $user['id']);
     } else {
         $helper->updateUserCartItemsSessionId($user['id'], $session->getId());
     }
     $this->checkUserGroup();
     return true;
 }
Exemple #2
0
 /**
  * Smartly updates the carts db table,
  * updating quantity if a product_id+product_attributes entry exists for the user
  * otherwise creating a new entry
  *
  * @deprecated as of version 6.3, will be removed in v6.4.0
  *
  * @param array
  * @param boolean
  * @param string
  */
 function updateCart($cart = array(), $sync = false, $old_sessionid = '', $new_userid = '')
 {
     $session = JFactory::getSession();
     $user = JFactory::getUser();
     if ($sync) {
         // get the cart based on session id
         if (!empty($old_sessionid)) {
             $session_id2use = $old_sessionid;
         } else {
             $session_id2use = $session->getId();
         }
         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
         $model = JModelLegacy::getInstance('Carts', 'CitruscartModel');
         $model->setState('filter_user', '0');
         $model->setState('filter_session', $session_id2use);
         $cart = $model->getList();
         $user_id = empty($new_userid) ? JFactory::getUser()->id : $new_userid;
         CitruscartHelperCarts::updateUserCartItemsSessionId($user_id, $session_id);
     }
     if (!empty($cart)) {
         JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables');
         foreach ($cart as $item) {
             $table = JTable::getInstance('Carts', 'CitruscartTable');
             $user_id = empty($new_userid) ? JFactory::getUser()->id : $new_userid;
             $item->user_id = empty($item->user_id) ? $user_id : $item->user_id;
             $keynames = array();
             $keynames['user_id'] = $item->user_id;
             if (empty($item->user_id)) {
                 $keynames['session_id'] = $session->getId();
             }
             $keynames['product_id'] = $item->product_id;
             $keynames['product_attributes'] = $item->product_attributes;
             // fire plugin event: onGetAdditionalCartKeyValues
             //this event allows plugins to extend the multiple-column primary key of the carts table
             $additionalKeyValues = CitruscartHelperCarts::getAdditionalKeyValues($item, null, null);
             if (!empty($additionalKeyValues)) {
                 $keynames = array_merge($keynames, $additionalKeyValues);
                 //$table->setKeyNames($keynames); // not necessary
             }
             if ($table->load($keynames)) {
                 if ($sync) {
                     // if syncing, the quantity as set in the session takes precedence
                     $table->product_qty = $item->product_qty;
                 } else {
                     $table->product_qty = $table->product_qty + $item->product_qty;
                 }
             } else {
                 foreach ($item as $key => $value) {
                     if (property_exists($table, $key)) {
                         $table->set($key, $value);
                     }
                 }
                 $table->session_id = $session->getId();
             }
             $date = JFactory::getDate();
             $table->last_updated = $date->toSql();
             if (!$table->save()) {
                 JError::raiseNotice('updateCart', $table->getError());
             }
         }
     }
     CitruscartHelperCarts::fixQuantities();
     return true;
 }