示例#1
0
 /**
  * Verifies the fields in a submitted form.
  * Then adds the item to the users cart
  *
  * @return unknown_type
  */
 function addChildrenToCart()
 {
     JRequest::checkToken() or jexit('Invalid Token');
     $product_id = JRequest::getInt('product_id');
     $quantities = JRequest::getVar('quantities', array(0), 'request', 'array');
     $filter_category = JRequest::getInt('filter_category');
     Tienda::load("TiendaHelperRoute", 'helpers.route');
     $router = new TiendaHelperRoute();
     if (!($itemid = $router->product($product_id, $filter_category, true))) {
         $itemid = $router->category(1, true);
     }
     // set the default redirect URL
     $redirect = "index.php?option=com_tienda&view=products&task=view&id={$product_id}&filter_category={$filter_category}&Itemid=" . $itemid;
     $redirect = JRoute::_($redirect, false);
     Tienda::load('TiendaHelperBase', 'helpers._base');
     $helper = TiendaHelperBase::getInstance();
     if (!Tienda::getInstance()->get('shop_enabled', '1')) {
         $this->messagetype = 'notice';
         $this->message = JText::_('COM_TIENDA_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 = JRequest::get('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";
     }
     Tienda::load('TiendaHelperCarts', 'helpers.carts');
     $carthelper = new TiendaHelperCarts();
     $cart_recurs = $carthelper->hasRecurringItem($cart_id, $id_type);
     // TODO get the children
     // loop thru each child,
     // get the list
     JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models');
     $model = JModel::getInstance('ProductRelations', 'TiendaModel');
     $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 = Tienda::getClass('TiendaHelperProduct', '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_TIENDA_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', 'TiendaTable');
             $product->load(array('product_id' => $child->product_id_to));
             // if product notforsale, fail
             if ($product->product_notforsale) {
                 $this->messagetype = 'notice';
                 $this->message = JText::_('COM_TIENDA_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_TIENDA_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_TIENDA_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 = $dispatcher->trigger("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)) {
         Tienda::load('TiendaHelperCarts', 'helpers.carts');
         foreach ($items as $item) {
             // add the item to the cart
             $cart_helper = new TiendaHelperCarts();
             $cartitem = $cart_helper->addItem($item);
             // fire plugin event
             $dispatcher = JDispatcher::getInstance();
             $dispatcher->trigger('onAfterAddToCart', array($cartitem, $values));
         }
         $this->messagetype = 'message';
         $this->message = JText::_('COM_TIENDA_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 (Tienda::getInstance()->get('addtocartaction', 'redirect')) {
         case "redirect":
             $returnUrl = base64_encode($redirect);
             $itemid = $router->findItemid(array('view' => 'checkout'));
             $redirect = JRoute::_("index.php?option=com_tienda&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;
 }
示例#2
0
 /**
  * Processes the payment form
  * and returns HTML to be displayed to the user
  * generally with a success/failed message
  * 
  * IMPORTANT: It is the responsibility of each payment plugin
  * to tell clear the user's cart (if the payment status warrants it) by using:
  * 
  * $this->removeOrderItemsFromCart( $order_id );
  * 
  * @param $data     array       form post data
  * @return string   HTML to display
  */
 function _postPayment($data)
 {
     // Process the payment
     $orderpayment_type = JRequest::getVar('orderpayment_type');
     if ($orderpayment_type == $this->_element) {
         $paction = JRequest::getVar('paction');
         $html = "";
         switch ($paction) {
             case "message":
                 $user = JFactory::getUser();
                 $checkout = JRequest::getInt('checkout');
                 // check if cart has recurring item and if checkout variable is set to '1'
                 Tienda::load("TiendaHelperCarts", 'helpers.carts');
                 $carts_helper = new TiendaHelperCarts();
                 if ($carts_helper->hasRecurringItem($user->id) && $checkout == '1') {
                     // check if the cart has only 1 item
                     JModel::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tienda/models');
                     $model = JModel::getInstance('Carts', 'TiendaModel');
                     $model->setState('filter_user', $user->id);
                     $items = $model->getList();
                     if (count($items) == '1') {
                         // check if the item in the cart is recurring product
                         $model = JModel::getInstance('Products', 'TiendaModel');
                         $model->setId($items[0]->product_id);
                         $product = $model->getItem();
                         if ($product->product_recurs) {
                             // get the order_id from the session set by the prePayment
                             $mainframe = JFactory::getApplication();
                             $order_id = (int) $mainframe->getUserState('tienda.order_id');
                             $order = JTable::getInstance('Orders', 'TiendaTable');
                             $order->load($order_id);
                             $items = $order->getItems();
                             // prepare payment for the recurring item Click Here to View and Print an Invoice
                             //$html = $this->_secondPrePayment($order_id);
                             $html = $this->_secondPrePayment();
                         }
                     }
                 } else {
                     $text = JText::_('TIENDA MONEYBOOKERS MESSAGE PAYMENT SUCCESS');
                     $html .= "<div align='center'><strong>" . $this->_renderHtml($text) . "<strong></div>";
                     $html .= $this->_displayArticle();
                 }
                 break;
             case "process":
                 $html = $this->_process();
                 $app = JFactory::getApplication();
                 $app->close();
                 break;
             case "cancel":
                 $text = JText::_('TIENDA MONEYBOOKERS MESSAGE CANCEL');
                 $html .= $this->_renderHtml($text);
                 break;
             default:
                 $text = JText::_('TIENDA MONEYBOOKERS MESSAGE INVALID ACTION');
                 $html .= $this->_renderHtml($text);
                 break;
         }
         return $html;
     }
 }