Example #1
0
 /**
  * Method to item to cart
  */
 function addToCart()
 {
     $app = JFactory::getApplication();
     $post = $app->input->getArray($_POST);
     $files = $app->input->get($_FILES);
     $product_id = $post['product_id'];
     // get attributes
     $attributes = array();
     foreach ($post as $key => $value) {
         if (substr($key, 0, 10) == 'attribute_') {
             $attributes[] = $value;
         }
     }
     sort($attributes);
     $attributes_csv = implode(',', $attributes);
     $product_qty = $post['quantity'];
     // Integrity checks on quantity being added
     if ($product_qty < 0) {
         $product_qty = '1';
     }
     // check product if available
     $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv);
     if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) {
         $messagetype = 'notice';
         $message = JText::_(JText::sprintf('COM_CITRUSCART_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty));
         $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $message, $messagetype);
         return;
     }
     // chec if product for sale
     $product = JTable::getInstance('Products', 'CitruscartTable');
     $product->load(array('product_id' => $product_id));
     // if product notforsale, fail
     if ($product->product_notforsale) {
         $messagetype = 'notice';
         $message = JText::_('COM_CITRUSCART_PRODUCT_NOT_FOR_SALE_NOTICE');
         $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $message, $messagetype);
         return;
     }
     $session = JFactory::getSession();
     $cart_id = $session->get('user_id', '', 'citruscart_pos');
     // userid from session
     $id_type = "user_id";
     // create cart object out of item properties
     $item = new JObject();
     $item->user_id = $cart_id;
     //TODO: need to determine what user
     $item->product_id = (int) $product_id;
     $item->product_qty = (int) $product_qty;
     $item->product_attributes = $attributes_csv;
     $item->vendor_id = '0';
     // vendors only in enterprise version
     $canAddToCart = Citruscart::getClass('CitruscartHelperCarts', 'helpers.carts')->canAddItem($item, $cart_id, $id_type);
     // onAfterCreateItemForAddToCart: plugin can add values to the item before it is being validated /added
     // once the extra field(s) have been set, they will get automatically saved
     $results = JFactory::getApplication()->triggerEvent("onAfterCreateItemForAddToCart", array($item, $post, $files));
     foreach ($results as $result) {
         foreach ($result as $key => $value) {
             $item->set($key, $value);
         }
     }
     // no matter what, fire this validation plugin event for plugins that extend the checkout workflow
     $results = array();
     $results = JFactory::getApplication()->triggerEvent("onBeforeAddToCart", array($item, $post));
     for ($i = 0; $i < count($results); $i++) {
         $result = $results[$i];
         if (!empty($result->error)) {
             $messagetype = 'notice';
             $message = JText::_(JText::sprintf('COM_CITRUSCART_NOT_AVAILABLE_QUANTITY_NOTICE', $availableQuantity->product_name, $product_qty));
             $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&tmpl=component', $result->message, 'notice');
             return;
         }
     }
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables');
     $table = JTable::getInstance('Carts', 'CitruscartTable');
     // first, determine if this product+attribute+vendor(+additonal_keys) exists in the cart
     // if so, update quantity
     // otherwise, add as new item
     // return the cart object with cart_id (to be used by plugins, etc)
     $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, $post, null);
     if (!empty($additionalKeyValues)) {
         $keynames = array_merge($keynames, $additionalKeyValues);
     }
     if ($table->load($keynames)) {
         $table->product_qty = $table->product_qty + $item->product_qty;
     } else {
         foreach ($item as $key => $value) {
             if (property_exists($table, $key)) {
                 $table->set($key, $value);
             }
         }
     }
     // Now for Eavs!!
     $eavs = CitruscartHelperEav::getAttributes('products', $item->product_id, false, array(1, 2));
     if (count($eavs)) {
         foreach ($eavs as $eav) {
             // Search for user edtable fields & user submitted value
             if (in_array($eav->editable_by, array(1, 2)) !== false && array_key_exists($eav->eavattribute_alias, $item)) {
                 $key = $eav->eavattribute_alias;
                 $table->set($key, $item->{$key});
             }
         }
     }
     $date = JFactory::getDate();
     $table->last_updated = $date->toSql();
     $table->session_id = $session->getId();
     if (!$table->save()) {
         JError::raiseNotice('updateCart', $table->getError());
     } else {
         $this->fixQuantities($item->user_id);
     }
     $this->setRedirect('index.php?option=com_citruscart&view=pos&task=addproducts&added=1&tmpl=component', JText::_('COM_CITRUSCART_SUCCESSFULLY_ADDED_ITEM_TO_CART'), 'success');
 }
Example #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;
 }
 /**
  *
  * @return unknown_type
  */
 function update()
 {
     $input = JFactory::getApplication()->input;
     $model = $this->getModel(strtolower(CitruscartHelperCarts::getSuffix()));
     $this->_setModelState();
     $user = JFactory::getUser();
     $session = JFactory::getSession();
     $cids = $input->get('cid', array(0), '', 'ARRAY');
     $product_attributes = $input->get('product_attributes', array(0), '', 'ARRAY');
     $quantities = $input->get('quantities', array(0), '', 'ARRAY');
     $post = $input->getArray($_POST);
     $msg = JText::_('COM_CITRUSCART_QUANTITIES_UPDATED');
     $remove = $input->getString('remove');
     if ($remove) {
         foreach ($cids as $cart_id => $product_id) {
             //            	$keynames = explode('.', $key);
             //            	$attributekey = $keynames[0].'.'.$keynames[1];
             //            	$index = $keynames[2];
             $row = $model->getTable();
             //main cartitem keys
             $ids = array('user_id' => $user->id, 'cart_id' => $cart_id);
             // fire plugin event: onGetAdditionalCartKeyValues
             //this event allows plugins to extend the multiple-column primary key of the carts table
             $additionalKeyValues = CitruscartHelperCarts::getAdditionalKeyValues(null, $post, $index);
             if (!empty($additionalKeyValues)) {
                 $ids = array_merge($ids, $additionalKeyValues);
             }
             if (empty($user->id)) {
                 $ids['session_id'] = $session->getId();
             }
             if ($return = $row->delete(array('cart_id' => $cart_id))) {
                 $item = new JObject();
                 $item->product_id = $product_id;
                 $item->product_attributes = $product_attributes[$cart_id];
                 $item->vendor_id = '0';
                 // vendors only in enterprise version
                 // fire plugin event
                 JFactory::getApplication()->triggerEvent('onRemoveFromCart', array($item));
             }
         }
     } else {
         foreach ($quantities as $cart_id => $value) {
             $carts = JTable::getInstance('Carts', 'CitruscartTable');
             $carts->load(array('cart_id' => $cart_id));
             $product_id = $carts->product_id;
             $value = (int) $value;
             //            	$keynames = explode('.', $key);
             //            	$product_id = $keynames[0];
             //            	$attributekey = $product_id.'.'.$keynames[1];
             //            	$index = $keynames[2];
             $vals = array();
             $vals['user_id'] = $user->id;
             $vals['session_id'] = $session->getId();
             $vals['product_id'] = $product_id;
             // fire plugin event: onGetAdditionalCartKeyValues
             //this event allows plugins to extend the multiple-column primary key of the carts table
             //		        	$additionalKeyValues = CitruscartHelperCarts::getAdditionalKeyValues( null, $post, $index );
             //		        	if (!empty($additionalKeyValues))
             //		        	{
             //		        		$vals = array_merge($vals, $additionalKeyValues);
             //		        	}
             // using a helper file,To determine the product's information related to inventory
             $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $product_attributes[$cart_id]);
             if ($availableQuantity->product_check_inventory && $value > $availableQuantity->quantity) {
                 JFactory::getApplication()->enqueueMessage(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $availableQuantity->product_name, $value));
                 continue;
             }
             if ($value > 1) {
                 $product = JTable::getInstance('Products', 'CitruscartTable');
                 $product->load(array('product_id' => $product_id));
                 if ($product->quantity_restriction) {
                     $min = $product->quantity_min;
                     $max = $product->quantity_max;
                     if ($max) {
                         if ($value > $max) {
                             $msg = JText::_('COM_CITRUSCART_REACHED_MAXIMUM_QUANTITY_FOR_THIS_OBJECT') . $max;
                             $value = $max;
                         }
                     }
                     if ($min) {
                         if ($value < $min) {
                             $msg = JText::_('COM_CITRUSCART_REACHED_MAXIMUM_QUANTITY_FOR_THIS_OBJECT') . $min;
                             $value = $min;
                         }
                     }
                 }
                 if ($product->product_recurs) {
                     $value = 1;
                 }
             }
             $row = $model->getTable();
             $vals['product_attributes'] = $product_attributes[$cart_id];
             $vals['product_qty'] = $value;
             if (empty($vals['product_qty']) || $vals['product_qty'] < 1) {
                 // remove it
                 if ($return = $row->delete($cart_id)) {
                     $item = new JObject();
                     $item->product_id = $product_id;
                     $item->product_attributes = $product_attributes[$cart_id];
                     $item->vendor_id = '0';
                     // vendors only in enterprise version
                     // fire plugin event
                     JFactory::getApplication()->triggerEvent('onRemoveFromCart', array($item));
                 }
             } else {
                 $row->load($cart_id);
                 $row->product_qty = $vals['product_qty'];
                 $row->save();
             }
         }
     }
     $carthelper = new CitruscartHelperCarts();
     $carthelper->fixQuantities();
     if (empty($user->id)) {
         $carthelper->checkIntegrity($session->getId(), 'session_id');
     } else {
         $carthelper->checkIntegrity($user->id);
     }
     Citruscart::load("CitruscartHelperRoute", 'helpers.route');
     $router = new CitruscartHelperRoute();
     $redirect = JRoute::_("index.php?option=com_citruscart&view=carts&Itemid=" . $router->findItemid(array('view' => 'carts')), false);
     $this->setRedirect($redirect, $msg);
 }