public static function getItems() { $list = array(); $k2params = JComponentHelper::getParams('com_k2store'); if (K2StoreHelperCart::hasProducts()) { require_once JPATH_SITE . '/components/com_k2store/models/mycart.php'; $cart_model = new K2StoreModelMyCart(); $totals = $cart_model->getTotals(); $product_count = K2StoreHelperCart::countProducts(); if ($k2params->get('auto_calculate_tax', 1)) { $total = $totals['total']; } else { $total = $totals['total_without_tax']; } $list['total'] = $total; $list['product_count'] = $product_count; //$html = JText::sprintf('k2store_CART_TOTAL', $product_count, k2storePrices::number($total)); } else { $list['total'] = 0; $list['product_count'] = 0; //$html = JText::_('k2store_NO_ITEMS_IN_CART'); } return $list; }
function confirm() { $app = JFactory::getApplication(); $user = JFactory::getUser(); $lang = JFactory::getLanguage(); $db = JFactory::getDbo(); $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('k2store'); $view = $this->getView('checkout', 'html'); $model = $this->getModel('checkout'); $cart_helper = new K2StoreHelperCart(); $cart_model = $this->getModel('mycart'); $orders_model = $this->getModel('orders'); $address_model = $this->getModel('address'); $redirect_url = JRoute::_('index.php?option=com_k2store&view=checkout'); $redirect = ''; //get the payment plugin form values set in the session. if ($this->session->has('payment_values', 'k2store')) { $values = $this->session->get('payment_values', array(), 'k2store'); //backward compatibility. TODO: change the way the plugin gets its data foreach ($values as $name => $value) { $app->input->set($name, $value); } } //prepare order $order = $this->_order; $order = $this->populateOrder(false); // get the order totals $order->calculateTotals(); //set shiping address if ($user->id && $this->session->has('shipping_address_id', 'k2store')) { $shipping_address = $address_model->getAddress($this->session->get('shipping_address_id', '', 'k2store')); } elseif ($this->session->has('guest', 'k2store')) { $guest = $this->session->get('guest', array(), 'k2store'); if ($guest['shipping']) { $shipping_address = $guest['shipping']; } } else { $shipping_address = array(); } //validate shipping $showShipping = false; if ($isShippingEnabled = $cart_model->getShippingIsEnabled()) { if (empty($shipping_address)) { $redirect = $redirect_url; } $showShipping = true; if ($this->session->has('shipping_values', 'k2store')) { //set the shipping methods $shipping_values = $this->session->get('shipping_values', array(), 'k2store'); $this->setShippingMethod($shipping_values); } } else { $this->session->clear('shipping_method', 'k2store'); $this->session->clear('shipping_methods', 'k2store'); $this->session->clear('shipping_values', 'k2store'); } $view->assign('showShipping', $showShipping); //process payment plugins $showPayment = true; if ((double) $order->order_total == (double) '0.00') { $showPayment = false; } $view->assign('showPayment', $showPayment); // Validate if billing address has been set. if ($user->id && $this->session->has('billing_address_id', 'k2store')) { $billing_address = $address_model->getAddress($this->session->get('billing_address_id', '', 'k2store')); } elseif ($this->session->has('guest', 'k2store')) { $guest = $this->session->get('guest', array(), 'k2store'); $billing_address = $guest['billing']; } if (empty($billing_address)) { $redirect = $redirect_url; } // Validate if payment method has been set. if ($showPayment == true && !$this->session->has('payment_method', 'k2store')) { $redirect = $redirect_url; if (!$this->validateSelectPayment($this->session->get('payment_method', '', 'k2store'), $values)) { $redirect = $redirect_url; } } // Validate cart has products and has stock. if (!$cart_helper->hasProducts()) { $redirect = $redirect_url; } //minimum order value check if (!$this->checkMinimumOrderValue($order)) { $error_msg[] = JText::_('K2STORE_ERROR_MINIMUM_ORDER_VALUE') . K2StorePrices::number($this->params->get('global_minordervalue')); $redirect = $redirect_url; } if (!$redirect) { $order_id = time(); $values['order_id'] = $order_id; //all is well so far. If this is a guest checkout, store the billing and shipping values of the guest in the address table. if ($this->session->has('guest', 'k2store')) { $guest = $this->session->get('guest', array(), 'k2store'); if (isset($guest['billing']) && count($guest['billing'])) { $address_model->addAddress('billing', $guest['billing']); } if (isset($guest['shipping']) && count($guest['shipping'])) { $address_model->addAddress('shipping', $guest['shipping']); } } // Save the orderitems with status if (!$this->saveOrderItems($values)) { // Output error message and halt $error_msg[] = $this->getError(); } $orderpayment_type = $this->session->get('payment_method', '', 'k2store'); //trigger onK2StoreBeforePayment event if ($showPayment == true && !empty($orderpayment_type)) { $results = $dispatcher->trigger("onK2StoreBeforePayment", array($orderpayment_type, $order)); } //set a default transaction status. $transaction_status = JText::_("K2STORE_TRANSACTION_INCOMPLETE"); // in the case of orders with a value of 0.00, use custom values if ((double) $order->order_total == (double) '0.00') { $orderpayment_type = 'free'; $transaction_status = JText::_("K2STORE_TRANSACTION_COMPLETE"); } //set order values $order->user_id = $user->id; $order->ip_address = $_SERVER['REMOTE_ADDR']; //generate a unique hash $order->token = JApplication::getHash($order_id); //user email $user_email = $user->id ? $user->email : $billing_address['email']; $order->user_email = $user_email; //generate invoice number $invoice = $orders_model->createInvoiceNumber($order_id); $order->invoice_number = $invoice->number; $order->invoice_prefix = $invoice->prefix; //get the customer note $customer_note = $this->session->get('customer_note', '', 'k2store'); $order->customer_note = $customer_note; $order->customer_language = $lang->getTag(); $order->customer_group = implode(',', $user->getAuthorisedGroups()); // Save an order with an Incomplete status $order->order_id = $order_id; $order->orderpayment_type = $orderpayment_type; // this is the payment plugin selected $order->transaction_status = $transaction_status; // payment plugin updates this field onPostPayment $order->order_state_id = 5; // default incomplete order state $order->orderpayment_amount = $order->order_total; // this is the expected payment amount. payment plugin should verify actual payment amount against expected payment amount //get currency id, value and code and store it $currency = K2StoreFactory::getCurrencyObject(); $order->currency_id = $currency->getId(); $order->currency_code = $currency->getCode(); $order->currency_value = $currency->getValue($currency->getCode()); //save whether to show shipping address or not if ($showShipping) { $order->is_shippable = 1; } else { $order->is_shippable = 0; } if ($order->save()) { //set values for orderinfo table // send the order_id and orderpayment_id to the payment plugin so it knows which DB record to update upon successful payment $values["order_id"] = $order->order_id; //$values["orderinfo"] = $order->orderinfo; $values["orderpayment_id"] = $order->id; $values["orderpayment_amount"] = $order->orderpayment_amount; // Save the orderitems with status if (!$this->saveOrderTax($values)) { // Output error message and halt $error_msg[] = $this->getError(); } if ($billing_address) { //dump all billing fields as json as it may contain custom field values as well $uset_account_type = ''; if ($this->session->has('uaccount', 'k2store')) { $uset_account_type = $this->session->get('uaccount', 'billing', 'k2store'); } if ($uset_account_type == 'register') { $type = 'register'; } elseif ($uset_account_type == 'guest') { $type = 'guest'; } elseif ($uset_account_type == 'login') { $type = 'billing'; } else { $type = 'billing'; } $values['orderinfo']['all_billing'] = $db->escape($this->processCustomFields($type, $billing_address)); foreach ($billing_address as $key => $value) { $values['orderinfo']['billing_' . $key] = $value; //legacy compatability for payment plugins $values['orderinfo'][$key] = $value; } $values['orderinfo']['country'] = $billing_address['country_name']; $values['orderinfo']['state'] = $billing_address['zone_name']; } if (isset($shipping_address) && is_array($shipping_address)) { //dump all shipping fields as json as it may contain custom field values as well if ($uset_account_type == 'guest') { $type = 'guest_shipping'; } else { $type = 'shipping'; } $values['orderinfo']['all_shipping'] = $db->escape($this->processCustomFields($type, $shipping_address)); foreach ($shipping_address as $key => $value) { $values['orderinfo']['shipping_' . $key] = $value; } } //now dump all payment_values as well. Because we may have custom fields there to if ($this->session->has('payment_values', 'k2store')) { $pay_values = $this->session->get('payment_values', array(), 'k2store'); $values['orderinfo']['all_payment'] = $db->escape($this->processCustomFields('payment', $pay_values)); } $values['orderinfo']['user_email'] = $user_email; $values['orderinfo']['user_id'] = $user->id; $values['orderinfo']['order_id'] = $order->order_id; $values['orderinfo']['orderpayment_id'] = $order->id; try { $this->saveOrderInfo($values['orderinfo']); } catch (Exception $e) { $redirect = $redirect_url; echo $e->getMessage() . "\n"; } //save shipping info if (isset($order->shipping) && !$this->saveOrderShippings($shipping_values)) { // TODO What to do if saving order shippings fails? $error = true; } } else { // Output error message and halt JError::raiseNotice('K2STORE_ERROR_SAVING_ORDER', $order->getError()); $redirect = $redirect_url; } // IMPORTANT: Store the order_id in the user's session for the postPayment "View Invoice" link $app->setUserState('k2store.order_id', $order->order_id); $app->setUserState('k2store.orderpayment_id', $order->id); $app->setUserState('k2store.order_token', $order->token); // in the case of orders with a value of 0.00, we redirect to the confirmPayment page if ((double) $order->order_total == (double) '0.00') { $free_redirect = JRoute::_('index.php?option=com_k2store&view=checkout&task=confirmPayment'); $view->assign('free_redirect', $free_redirect); } $payment_plugin = $this->session->get('payment_method', '', 'k2store'); $values['payment_plugin'] = $payment_plugin; $results = $dispatcher->trigger("onK2StorePrePayment", array($payment_plugin, $values)); // Display whatever comes back from Payment Plugin for the onPrePayment $html = ""; for ($i = 0; $i < count($results); $i++) { $html .= $results[$i]; } //check if plugins set a redirect if ($this->session->has('plugin_redirect', 'k2store')) { $redirect = $this->session->get('plugin_redirect', '', 'k2store'); } $view->assign('plugin_html', $html); $summary = $this->getOrderSummary(); $view->assign('orderSummary', $summary); } // Set display $view->setLayout('checkout_confirm'); $view->set('_doTask', true); $view->assign('order', $order); $view->assign('redirect', $redirect); $view->setModel($model, true); ob_start(); $view->display(); $html = ob_get_contents(); ob_end_clean(); echo $html; $app->close(); }
function display($cachable = false, $urlparams = array()) { //initialist system objects $app = JFactory::getApplication(); $session = JFactory::getSession(); K2StoreUtilities::cleanCache(); $params = JComponentHelper::getParams('com_k2store'); $view = $this->getView('mycart', 'html'); $view->set('_view', 'mycart'); //get post vars $post = $app->input->getArray($_POST); $model = $this->getModel('Mycart'); $checkout_model = $this->getModel('checkout'); $store = K2StoreHelperCart::getStoreAddress(); if (K2StoreHelperCart::hasProducts()) { $items = $model->getDataNew(); } else { $items = array(); } //coupon $post_coupon = $app->input->getString('coupon', ''); //first time applying? then set coupon to session if (isset($post_coupon) && !empty($post_coupon)) { try { if ($this->validateCoupon()) { $session->set('coupon', $post_coupon, 'k2store'); $msg = JText::_('K2STORE_COUPON_APPLIED_SUCCESSFULLY'); } } catch (Exception $e) { $msg = $e->getMessage(); } $this->setRedirect(JRoute::_("index.php?option=com_k2store&view=mycart"), $msg); } if ($post_coupon) { $view->assign('coupon', $post_coupon); } elseif ($session->has('coupon', 'k2store')) { $view->assign('coupon', $session->get('coupon', '', 'k2store')); } else { $view->assign('coupon', ''); } //shipping tax calculator //get countries $db = JFactory::getDbo(); $db->setQuery($db->getQuery(true)->select('country_id, country_name')->from('#__k2store_countries')->where('state=1')); $countries = $db->loadObjectList(); $country_id = $app->input->getInt('country_id'); if (isset($country_id)) { $session->set('billing_country_id', $country_id, 'k2store'); $session->set('shipping_country_id', $country_id, 'k2store'); } elseif ($session->has('shipping_country_id', 'k2store')) { $country_id = $session->get('shipping_country_id', '', 'k2store'); } else { $country_id = $store->country_id; } $countryList = JHtml::_('select.genericlist', $countries, 'country_id', $attribs = null, $optKey = 'country_id', $optText = 'country_name', $country_id, $idtag = 'cart_country', $translate = false); $zone_id = $app->input->getInt('zone_id'); if (isset($zone_id)) { $session->set('billing_zone_id', $zone_id, 'k2store'); $session->set('shipping_zone_id', $zone_id, 'k2store'); } elseif ($session->has('shipping_zone_id', 'k2store')) { $zone_id = $session->get('shipping_zone_id', '', 'k2store'); } else { $zone_id = $store->zone_id; } $postcode = $app->input->getString('postcode'); if (isset($postcode)) { $session->set('shipping_postcode', $postcode, 'k2store'); } elseif ($session->has('shipping_postcode', 'k2store')) { $postcode = $session->get('shipping_postcode', '', 'k2store'); } else { $postcode = $store->store_zip; } $view->assign('countryList', $countryList); $view->assign('country_id', $country_id); $view->assign('zone_id', $zone_id); $view->assign('postcode', $postcode); //assign a single selected method if it had been selected if ($session->has('shipping_values', 'k2store')) { //get exisitng values $shipping_values = $session->get('shipping_values', array(), 'k2store'); $rates = $checkout_model->getShippingRates(); $session->set('shipping_methods', $rates, 'k2store'); if (count($rates) < 1) { $session->set('shipping_method', array(), 'k2store'); } $is_same = false; foreach ($rates as $rate) { if ($shipping_values['shipping_name'] == $rate['name']) { $shipping_values['shipping_price'] = isset($rate['price']) ? $rate['price'] : 0; $shipping_values['shipping_extra'] = isset($rate['extra']) ? $rate['extra'] : 0; $shipping_values['shipping_code'] = isset($rate['code']) ? $rate['code'] : ''; $shipping_values['shipping_name'] = isset($rate['name']) ? $rate['name'] : ''; $shipping_values['shipping_tax'] = isset($rate['tax']) ? $rate['tax'] : 0; $shipping_values['shipping_plugin'] = isset($rate['element']) ? $rate['element'] : ''; $session->set('shipping_method', $shipping_values['shipping_plugin'], 'k2store'); $session->set('shipping_values', $shipping_values, 'k2store'); $is_same = true; } } if ($is_same === false) { //sometimes the previously selected method may not apply. In those cases, we will have remove the selected shipping. $session->set('shipping_values', array(), 'k2store'); } $view->assign('shipping_values', $session->get('shipping_values', array(), 'k2store')); } else { $view->assign('shipping_values', array()); } //do we have shipping methods if ($session->has('shipping_methods', 'k2store')) { $view->assign('shipping_methods', $session->get('shipping_methods', array(), 'k2store')); } //assign a single selected method if it had been selected if ($session->has('shipping_method', 'k2store')) { $view->assign('shipping_method', $session->get('shipping_method', array(), 'k2store')); } else { $view->assign('shipping_method', array()); } $cartobject = $model->checkItems($items, $params->get('show_tax_total')); $totals = $model->getTotals(); JPluginHelper::importPlugin('k2store'); $results = $app->triggerEvent('onK2StoreAfterDisplayCart', array($items)); $view->assign('onK2StoreAfterDisplayCart', trim(implode("\n", $results))); $view->assign('cartobj', $cartobject); $view->assign('totals', $totals); $view->assign('model', $model); $view->assign('params', $params); if (isset($post['return'])) { $view->assign('return', $post['return']); } $view->set('_doTask', true); $view->set('hidemenu', true); $view->setModel($model, true); $view->setLayout('default'); $view->display(); }