Example #1
0
 function getCart()
 {
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables');
     JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_citruscart/models');
     // determine whether we're working with a session or db cart
     $suffix = CitruscartHelperCarts::getSuffix();
     $model = JModelLegacy::getInstance('Carts', 'CitruscartModel');
     $session = JFactory::getSession();
     $user = JFactory::getUser();
     $model->setState('filter_user', $user->id);
     if (empty($user->id)) {
         $model->setState('filter_session', $session->getId());
     }
     $list = $model->getList(false, false);
     Citruscart::load('Citruscart', 'defines');
     $config = Citruscart::getInstance();
     $show_tax = $config->get('display_prices_with_tax');
     $this->using_default_geozone = false;
     if ($show_tax) {
         Citruscart::load('CitruscartHelperUser', 'helpers.user');
         $geozones = CitruscartHelperUser::getGeoZones(JFactory::getUser()->id);
         if (empty($geozones)) {
             // use the default
             $this->using_default_geozone = true;
             $table = JTable::getInstance('Geozones', 'CitruscartTable');
             $table->load(array('geozone_id' => Citruscart::getInstance()->get('default_tax_geozone')));
             $geozones = array($table);
         }
         Citruscart::load("CitruscartHelperProduct", 'helpers.product');
         foreach ($list as &$item) {
             $taxtotal = CitruscartHelperProduct::getTaxTotal($item->product_id, $geozones);
             $item->product_price = $item->product_price + $taxtotal->tax_total;
             $item->taxtotal = $taxtotal;
         }
     }
     return $list;
 }
Example #2
0
 /**
  * Adjusts cart quantities based on availability
  *
  * @return unknown_type
  */
 function fixQuantities()
 {
     JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
     JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_citruscart/models');
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables');
     $product = JTable::getInstance('ProductQuantities', 'CitruscartTable');
     $tableProduct = JTable::getInstance('Products', 'CitruscartTable');
     $suffix = strtolower(CitruscartHelperCarts::getSuffix());
     $model = JModelLegacy::getInstance('Carts', 'CitruscartModel');
     switch ($suffix) {
         case 'sessioncarts':
         case 'carts':
         default:
             $user = JFactory::getUser();
             if (empty($user->id)) {
                 $session = JFactory::getSession();
                 $model->setState('filter_session', $session->getId());
             } else {
                 $model->setState('filter_user', $user->id);
             }
             $cart = $model->getList(false, true);
             if (!empty($cart)) {
                 foreach ($cart as $cartitem) {
                     $keynames = array();
                     $keynames['user_id'] = $cartitem->user_id;
                     if (empty($cartitem->user_id)) {
                         $keynames['session_id'] = $cartitem->session_id;
                     }
                     $keynames['product_id'] = $cartitem->product_id;
                     $keynames['product_attributes'] = $cartitem->product_attributes;
                     $tableProduct->load($cartitem->product_id, true, false);
                     if ($tableProduct->quantity_restriction) {
                         $quantity = $cartitem->product_qty;
                         $min = $tableProduct->quantity_min;
                         $max = $tableProduct->quantity_max;
                         if ($max) {
                             if ($cartitem->product_qty > $max) {
                                 $quantity = $max;
                             }
                         }
                         if ($min) {
                             if ($cartitem->product_qty < $min) {
                                 $quantity = $min;
                             }
                         }
                         // load table to adjust quantity in cart
                         $table = JTable::getInstance('Carts', 'CitruscartTable');
                         //$table->load($keynames);
                         $table->load(array('cart_id' => $cartitem->cart_id), true, false);
                         $table->product_id = $cartitem->product_id;
                         $table->product_attributes = $cartitem->product_attributes;
                         $table->user_id = $cartitem->user_id;
                         $table->session_id = $cartitem->session_id;
                         // adjust the cart quantity
                         $table->product_qty = $quantity;
                         $table->save();
                     }
                     if (empty($tableProduct->product_check_inventory)) {
                         // if this item doesn't check inventory, skip it
                         continue;
                     }
                     $product->load(array('product_id' => $cartitem->product_id, 'vendor_id' => '0', 'product_attributes' => $cartitem->product_attributes), true, false);
                     if ($cartitem->product_qty > $product->quantity) {
                         // enqueu a system message
                         JFactory::getApplication()->enqueueMessage(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $cartitem->product_name, $cartitem->product_qty));
                         // load table to adjust quantity in cart
                         $table = JTable::getInstance('Carts', 'CitruscartTable');
                         $table->load($keynames, true, false);
                         $table->product_id = $cartitem->product_id;
                         $table->product_attributes = $cartitem->product_attributes;
                         $table->user_id = $cartitem->user_id;
                         $table->session_id = $cartitem->session_id;
                         // adjust the cart quantity
                         $table->product_qty = $product->quantity;
                         $table->save();
                     }
                 }
             }
             break;
     }
 }
Example #3
0
 /**
  *
  * @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) {
             $row = $model->getTable();
             //main cartitem keys
             $ids = array('user_id' => $user->id, 'cart_id' => $cart_id);
             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
                 $item->cart_id = $cart_id;
                 // 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));
                 $msg = JText::_('COM_CITRUSCART_QUANTITY_UPDATE_FAILED');
                 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;
                         }
                     }
                     $remainder = 0;
                     if (!empty($product->quantity_step)) {
                         $remainder = $value % $product->quantity_step;
                     }
                     if (!empty($product->quantity_step) && !empty($remainder)) {
                         $msg = JText::sprintf('COM_CITRUSCART_QUANTITY_MUST_BE_IN_INCREMENTS_OF', $product->quantity_step);
                         $value = $value - $remainder > 0 ? $value - $remainder : $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);
 }