Exemple #1
0
 /**
  * Verifies the fields in a submitted form.
  * Then adds the item to the users cart
  *
  * @return unknown_type
  */
 function addToCart()
 {
     $input = JFactory::getApplication()->input;
     JSession::checkToken() or jexit('Invalid Token');
     $product_id = $input->getInt('product_id');
     $product_qty = $input->getInt('product_qty');
     $filter_category = $input->getInt('filter_category');
     Citruscart::load("CitruscartHelperRoute", 'helpers.route');
     $router = new CitruscartHelperRoute();
     if (!($itemid = $router->product($product_id, $filter_category, true))) {
         $itemid = $router->category(1, true);
         if (!$itemid) {
             $itemid = $input->getInt('Itemid', 0);
         }
     }
     // set the default redirect URL
     $redirect = "index.php?option=com_citruscart&view=products&task=view&id={$product_id}&filter_category={$filter_category}&Itemid=" . $itemid;
     $redirect = JRoute::_($redirect, false);
     Citruscart::load('CitruscartHelperBase', 'helpers._base');
     $helper = CitruscartHelperBase::getInstance();
     if (!Citruscart::getInstance()->get('shop_enabled', '1')) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_SHOP_DISABLED');
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     // convert elements to array that can be binded
     $values = $input->getArray($_POST);
     if (isset($values['elements'])) {
         $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $values['elements']));
         unset($values['elements']);
         // convert elements to array that can be binded
         $values = array_merge(CitruscartHelperBase::elementsToArray($elements), $values);
         $intpu->set($values, 'POST');
     }
     $files = $input->files->get('files');
     $attributes = array();
     foreach ($values as $key => $value) {
         if (substr($key, 0, 10) == 'attribute_') {
             $attributes[] = $value;
         }
     }
     sort($attributes);
     $attributes_csv = implode(',', $attributes);
     // Integrity checks on quantity being added
     if ($product_qty < 0) {
         $product_qty = '1';
     }
     // using a helper file to determine the product's information related to inventory
     $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv);
     if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) {
         $this->messagetype = 'notice';
         $this->message = JText::_(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $availableQuantity->product_name, $product_qty));
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     // do the item's charges recur? does the cart already have a subscription in it?  if so, fail with notice
     $product = JTable::getInstance('Products', 'CitruscartTable');
     $product->load(array('product_id' => $product_id), true, false);
     // if product notforsale, fail
     if ($product->product_notforsale) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_PRODUCT_NOT_FOR_SALE');
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     $user = JFactory::getUser();
     $cart_id = $user->id;
     $id_type = "user_id";
     if (empty($user->id)) {
         $session = JFactory::getSession();
         $cart_id = $session->getId();
         $id_type = "session";
     }
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $carthelper = new CitruscartHelperCarts();
     $cart_recurs = $carthelper->hasRecurringItem($cart_id, $id_type);
     if ($product->product_recurs && $cart_recurs) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_CART_ALREADY_RECURS');
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     if ($product->product_recurs) {
         $product_qty = '1';
     }
     // create cart object out of item properties
     $item = new JObject();
     $item->user_id = JFactory::getUser()->id;
     $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
     // if ther is another product_url, put it into the cartitem_params, to allow custom redirect
     if (array_key_exists('product_url', $values)) {
         $params = new DSCParameter(trim(@$item->cartitem_params));
         $params->set('product_url', $values['product_url']);
         $item->cartitem_params = trim($params->__toString());
     }
     // 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
     $dispatcher = JDispatcher::getInstance();
     $results = JFactory::getApplication()->triggerEvent("onAfterCreateItemForAddToCart", array($item, $values, $files));
     foreach ($results as $result) {
         foreach ($result as $key => $value) {
             $item->set($key, $value);
         }
     }
     // does the user/cart match all dependencies?
     $canAddToCart = $carthelper->canAddItem($item, $cart_id, $id_type);
     if (!$canAddToCart) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_CANNOT_ADD_ITEM_TO_CART') . " - " . $carthelper->getError();
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     // no matter what, fire this validation plugin event for plugins that extend the checkout workflow
     $results = array();
     $dispatcher = JDispatcher::getInstance();
     $results = JFactory::getApplication()->triggerEvent("onBeforeAddToCart", array(&$item, $values));
     for ($i = 0; $i < count($results); $i++) {
         $result = $results[$i];
         if (!empty($result->error)) {
             $this->messagetype = 'notice';
             $this->message = $result->message;
             $this->setRedirect($redirect, $this->message, $this->messagetype);
             return;
         }
     }
     // if here, add to cart
     // After login, session_id is changed by Joomla, so store this for reference
     $session = JFactory::getSession();
     $session->set('old_sessionid', $session->getId());
     // add the item to the cart
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $cart_helper = new CitruscartHelperCarts();
     $cartitem = $cart_helper->addItem($item);
     // fire plugin event
     $dispatcher = JDispatcher::getInstance();
     JFactory::getApplication()->triggerEvent('onAfterAddToCart', array($cartitem, $values));
     // get the 'success' redirect url
     switch (Citruscart::getInstance()->get('addtocartaction', 'redirect')) {
         case "checkout":
             // if a base64_encoded url is present as return, use that as the return url
             // otherwise return == the product view page
             $returnUrl = base64_encode($redirect);
             if ($return_url = $input->getBase64('return')) {
                 $return_url = base64_decode($return_url);
                 if (JURI::isInternal($return_url)) {
                     $returnUrl = base64_encode($return_url);
                 }
             }
             // if a base64_encoded url is present as redirect, redirect there,
             // otherwise redirect to the checkout
             $itemid_checkout = $router->findItemid(array('view' => 'checkout'));
             $itemid_opc = $router->findItemid(array('view' => 'opc'));
             $checkout_view = "checkout";
             $itemid = null;
             if ($itemid_opc) {
                 $itemid = $itemid_opc;
                 $checkout_view = "opc";
             } elseif ($itemid_checkout) {
                 $itemid = $itemid_checkout;
             }
             if (!$itemid) {
                 $itemid = $input->getInt('Itemid', 0);
             }
             $redirect = JRoute::_("index.php?option=com_citruscart&view=" . $checkout_view . "&Itemid=" . $itemid, false);
             if ($redirect_url = $input->getBase64('redirect')) {
                 $redirect_url = base64_decode($redirect_url);
                 if (JURI::isInternal($redirect_url)) {
                     $redirect = $redirect_url;
                 }
             }
             if (strpos($redirect, '?') === false) {
                 $redirect .= "?return=" . $returnUrl;
             } else {
                 $redirect .= "&return=" . $returnUrl;
             }
             break;
         case "0":
         case "none":
             // redirects back to product page
             break;
         case "samepage":
             // redirects back to the page it came from (category, content, etc)
             // Take only the url without the base domain (index.php?option.....)
             if ($return_url = $input->getBase64('return')) {
                 $return_url = base64_decode($return_url);
                 $uri = JURI::getInstance();
                 $uri->parse($return_url);
                 $redirect = $uri->__toString(array('path', 'query', 'fragment'));
                 $redirect = JRoute::_($redirect, false);
             }
             break;
         case "lightbox":
         case "redirect":
         default:
             // if a base64_encoded url is present as return, use that as the return url
             // otherwise return == the product view page
             $returnUrl = base64_encode($redirect);
             if ($return_url = $input->getBase64('return')) {
                 $return_url = base64_decode($return_url);
                 if (JURI::isInternal($return_url)) {
                     $returnUrl = base64_encode($return_url);
                 }
             }
             // if a base64_encoded url is present as redirect, redirect there,
             // otherwise redirect to the cart
             $itemid = $router->findItemid(array('view' => 'carts'));
             if (!$itemid) {
                 $itemid = $input->getInt('Itemid', 0);
             }
             $redirect = JRoute::_("index.php?option=com_citruscart&view=carts&Itemid=" . $itemid, false);
             if ($redirect_url = $input->getBase64('redirect')) {
                 $redirect_url = base64_decode($redirect_url);
                 if (JURI::isInternal($redirect_url)) {
                     $redirect = $redirect_url;
                 }
             }
             //$returnUrl = base64_encode( $redirect );
             //$itemid = $router->findItemid( array('view'=>'checkout') );
             //$redirect = JRoute::_( "index.php?option=com_citruscart&view=carts&Itemid=".$itemid, false );
             if (strpos($redirect, '?') === false) {
                 $redirect .= "?return=" . $returnUrl;
             } else {
                 $redirect .= "&return=" . $returnUrl;
             }
             break;
     }
     $this->messagetype = 'message';
     $this->message = JText::_('COM_CITRUSCART_ITEM_ADDED_TO_YOUR_CART');
     $this->setRedirect($redirect, $this->message, $this->messagetype);
     return;
 }
 /**
  * Verifies the fields in a submitted form.
  * Then adds the item to the users cart
  *
  * @return unknown_type
  */
 function addChildrenToCart()
 {
     $input = JFactory::getApplication()->input;
     JSession::checkToken() or jexit('Invalid Token');
     $product_id = $input->getInt('product_id');
     $quantities = $input->get('quantities', array(0), 'request', 'array');
     $filter_category = $input->getInt('filter_category');
     Citruscart::load("CitruscartHelperRoute", 'helpers.route');
     $router = new CitruscartHelperRoute();
     if (!($itemid = $router->product($product_id, $filter_category, true))) {
         $itemid = $router->category(1, true);
     }
     // set the default redirect URL
     $redirect = "index.php?option=com_citruscart&view=products&task=view&id=" . $product_id . "&filter_category=" . $filter_category . "&Itemid=" . $itemid;
     $redirect = JRoute::_($redirect, false);
     Citruscart::load('CitruscartHelperBase', 'helpers._base');
     $helper = CitruscartHelperBase::getInstance();
     if (!Citruscart::getInstance()->get('shop_enabled', '1')) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_CITRUSCART_SHOP_DISABLED');
         $this->setRedirect($redirect, $this->message, $this->messagetype);
         return;
     }
     $items = array();
     // this will collect the items to add to the cart
     // convert elements to array that can be binded
     $values = $input->getArray($_POST);
     $attributes_csv = '';
     $user = JFactory::getUser();
     $cart_id = $user->id;
     $id_type = "user_id";
     if (empty($user->id)) {
         $session = JFactory::getSession();
         $cart_id = $session->getId();
         $id_type = "session";
     }
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $carthelper = new CitruscartHelperCarts();
     $cart_recurs = $carthelper->hasRecurringItem($cart_id, $id_type);
     // TODO get the children
     // loop thru each child,
     // get the list
     JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/models');
     $model = JModelLegacy::getInstance('ProductRelations', 'CitruscartModel');
     $model->setState('filter_product', $product_id);
     $model->setState('filter_relation', 'parent');
     if ($children = $model->getList()) {
         foreach ($children as $child) {
             $product_qty = $quantities[$child->product_id_to];
             // Integrity checks on quantity being added
             if ($product_qty < 0) {
                 $product_qty = '1';
             }
             // using a helper file to determine the product's information related to inventory
             $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($child->product_id_to, $attributes_csv);
             if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) {
                 $this->messagetype = 'notice';
                 $this->message = JText::_(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $availableQuantity->product_name, $product_qty));
                 $this->setRedirect($redirect, $this->message, $this->messagetype);
                 return;
             }
             // do the item's charges recur? does the cart already have a subscription in it?  if so, fail with notice
             $product = JTable::getInstance('Products', 'CitruscartTable');
             $product->load(array('product_id' => $child->product_id_to));
             // if product notforsale, fail
             if ($product->product_notforsale) {
                 $this->messagetype = 'notice';
                 $this->message = JText::_('COM_CITRUSCART_PRODUCT_NOT_FOR_SALE');
                 $this->setRedirect($redirect, $this->message, $this->messagetype);
                 return;
             }
             if ($product->product_recurs && $cart_recurs) {
                 $this->messagetype = 'notice';
                 $this->message = JText::_('COM_CITRUSCART_CART_ALREADY_RECURS');
                 $this->setRedirect($redirect, $this->message, $this->messagetype);
                 return;
             }
             if ($product->product_recurs) {
                 $product_qty = '1';
             }
             // create cart object out of item properties
             $item = new JObject();
             $item->user_id = JFactory::getUser()->id;
             $item->product_id = (int) $child->product_id_to;
             $item->product_qty = (int) $product_qty;
             $item->product_attributes = $attributes_csv;
             $item->vendor_id = '0';
             // vendors only in enterprise version
             // does the user/cart match all dependencies?
             $canAddToCart = $carthelper->canAddItem($item, $cart_id, $id_type);
             if (!$canAddToCart) {
                 $this->messagetype = 'notice';
                 $this->message = JText::_('COM_CITRUSCART_CANNOT_ADD_ITEM_TO_CART') . " - " . $carthelper->getError();
                 $this->setRedirect($redirect, $this->message, $this->messagetype);
                 return;
             }
             // no matter what, fire this validation plugin event for plugins that extend the checkout workflow
             $results = array();
             $results = JFactory::getApplication()->triggerEvent("onBeforeAddToCart", array($item, $values));
             for ($i = 0; $i < count($results); $i++) {
                 $result = $results[$i];
                 if (!empty($result->error)) {
                     $this->messagetype = 'notice';
                     $this->message = $result->message;
                     $this->setRedirect($redirect, $this->message, $this->messagetype);
                     return;
                 }
             }
             // if here, add to cart
             $items[] = $item;
         }
     }
     if (!empty($items)) {
         Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
         foreach ($items as $item) {
             // add the item to the cart
             $cart_helper = new CitruscartHelperCarts();
             $cartitem = $cart_helper->addItem($item);
             // fire plugin event
             JFactory::getApplication()->triggerEvent('onAfterAddToCart', array($cartitem, $values));
         }
         $this->messagetype = 'message';
         $this->message = JText::_('COM_CITRUSCART_ITEMS_ADDED_TO_YOUR_CART');
     }
     // After login, session_id is changed by Joomla, so store this for reference
     $session = JFactory::getSession();
     $session->set('old_sessionid', $session->getId());
     // get the 'success' redirect url
     // TODO Enable redirect via base64_encoded urls?
     switch (Citruscart::getInstance()->get('addtocartaction', 'redirect')) {
         case "redirect":
             $returnUrl = base64_encode($redirect);
             $itemid = $router->findItemid(array('view' => 'checkout'));
             $redirect = JRoute::_("index.php?option=com_citruscart&view=carts&Itemid=" . $itemid, false);
             if (strpos($redirect, '?') === false) {
                 $redirect .= "?return=" . $returnUrl;
             } else {
                 $redirect .= "&return=" . $returnUrl;
             }
             break;
         case "0":
         case "none":
             break;
         case "lightbox":
         default:
             // TODO Figure out how to get the lightbox to display even after a redirect
             break;
     }
     $this->setRedirect($redirect, $this->message, $this->messagetype);
     return;
 }
 /**
  * 
  * Enter description here ...
  * @param unknown_type $values
  * @param unknown_type $files
  * @return return_type
  */
 function addToCart($values = array(), $files = array())
 {
     // create cart object out of item properties
     $item = new JObject();
     $item->user_id = $this->user_id;
     $item->product_id = (int) $this->product_id;
     $item->product_qty = !empty($this->product_quantity) ? $this->product_quantity : '1';
     $item->product_attributes = $this->product_attributes;
     $item->vendor_id = $this->vendor_id;
     $item->cartitem_params = $this->wishlistitem_params;
     // 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
     $dispatcher = JDispatcher::getInstance();
     $results = JFactory::getApplication()->triggerEvent("onAfterCreateItemForAddToCart", array($item, $values, $files));
     foreach ($results as $result) {
         foreach ($result as $key => $value) {
             $item->set($key, $value);
         }
     }
     if (!$this->isAvailable()) {
         return false;
     }
     Citruscart::load('CitruscartHelperProduct', 'helpers.product');
     $product_helper = new CitruscartHelperProduct();
     $availableQuantity = $product_helper->getAvailableQuantity($this->product_id, $this->product_attributes);
     if ($availableQuantity->product_check_inventory && $item->product_qty > $availableQuantity->quantity) {
         $this->setError(JText::_(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY", $availableQuantity->product_name, $item->product_qty)));
         return false;
     }
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $carthelper = new CitruscartHelperCarts();
     // does the user/cart match all dependencies?
     $canAddToCart = $carthelper->canAddItem($item, $this->user_id, 'user_id');
     if (!$canAddToCart) {
         $this->setError(JText::_('COM_CITRUSCART_CANNOT_ADD_ITEM_TO_CART') . " - " . $carthelper->getError());
         return false;
     }
     // no matter what, fire this validation plugin event for plugins that extend the checkout workflow
     $results = array();
     $dispatcher = JDispatcher::getInstance();
     $results = JFactory::getApplication()->triggerEvent("onBeforeAddToCart", array(&$item, $values));
     for ($i = 0; $i < count($results); $i++) {
         $result = $results[$i];
         if (!empty($result->error)) {
             $this->setError(JText::_('COM_CITRUSCART_CANNOT_ADD_ITEM_TO_CART') . " - " . $result->message);
             return false;
         }
     }
     // if here, add to cart
     // After login, session_id is changed by Joomla, so store this for reference
     $session = JFactory::getSession();
     $session->set('old_sessionid', $session->getId());
     // add the item to the cart
     $cartitem = $carthelper->addItem($item);
     // fire plugin event
     $dispatcher = JDispatcher::getInstance();
     JFactory::getApplication()->triggerEvent('onAfterAddToCart', array($cartitem, $values));
     return $cartitem;
 }
Exemple #4
0
 /**
  * Adds an item to a User's shopping cart
  * whether in the session or the db
  *
  */
 function addToCart()
 {
     $input = JFactory::getApplication()->input;
     if (!Citruscart::getInstance()->get('shop_enabled', '1')) {
         return false;
     }
     // saving the session id which will use to update the cart
     $session = JFactory::getSession();
     // After login, session_id is changed by Joomla, so store this for reference
     $session->set('old_sessionid', $session->getId());
     $response = array();
     $response['msg'] = '';
     $response['error'] = '';
     // get elements from post
     $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $input->getString('elements')));
     // convert elements to array that can be binded
     Citruscart::load('CitruscartHelperBase', 'helpers._base');
     $values = CitruscartHelperBase::elementsToArray($elements);
     $product_id = !empty($values['product_id']) ? $values['product_id'] : $input->getInt('product_id');
     $product_qty = !empty($values['product_qty']) ? $values['product_qty'] : '1';
     $attributes = array();
     foreach ($values as $key => $value) {
         if (substr($key, 0, 10) == 'attribute_') {
             $attributes[] = $value;
         }
     }
     $attributes_csv = implode(',', $attributes);
     // Integrity checks on quantity being added
     if ($product_qty < 0) {
         $product_qty = '1';
     }
     // using a helper file to determine the product's information related to inventory
     $availableQuantity = Citruscart::getClass('CitruscartHelperProduct', 'helpers.product')->getAvailableQuantity($product_id, $attributes_csv);
     if ($availableQuantity->product_check_inventory && $product_qty > $availableQuantity->quantity) {
         JFactory::getApplication()->enqueueMessage(JText::sprintf("COM_CITRUSCART_NOT_AVAILABLE_QUANTITY_NOTICE", $availableQuantity->product_name, $product_qty));
         $product_qty = $availableQuantity->quantity;
     }
     // create cart object out of item properties
     $item = new JObject();
     $item->user_id = JFactory::getUser()->id;
     $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
     // 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, $values));
     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, $values));
     for ($i = 0; $i < count($results); $i++) {
         $result = $results[$i];
         if (!empty($result->error)) {
             Citruscart::load('CitruscartHelperBase', 'helpers._base');
             $helper = CitruscartHelperBase::getInstance();
             $response['msg'] = $helper->generateMessage($result->message);
             $response['error'] = '1';
             echo json_encode($response);
             return;
         } else {
             // if here, all is OK
             $response['error'] = '0';
         }
     }
     // add the item to the cart
     Citruscart::load('CitruscartHelperCarts', 'helpers.carts');
     $cart_helper = new CitruscartHelperCarts();
     $cartitem = $cart_helper->addItem($item);
     // fire plugin event
     JFactory::getApplication()->triggerEvent('onAfterAddToCart', array($cartitem, $values));
     // update the cart module, if it is enabled
     $this->displayCart();
 }