/** * Validates the credit amount and applies it to the order * return unknown_type */ function validateApplyCredit() { $input = JFactory::getApplication()->input; JLoader::import('com_citruscart.library.json', JPATH_ADMINISTRATOR . '/components'); $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $input->getString('elements'))); // convert elements to array that can be binded Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance(); $values = $helper->elementsToArray($elements); $user_id = $this->user->id; $apply_credit_amount = (double) $input->get('apply_credit_amount', ''); $response = array(); $response['msg'] = ''; $response['error'] = ''; // is the credit amount valid (i.e. greater than 0, if ($apply_credit_amount < (double) '0.00') { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_PLEASE_SPECIFY_VALID_CREDIT_AMOUNT')); echo json_encode($response); return; } // less than/== their available amount & order amount? $userinfo = JTable::getInstance('UserInfo', 'CitruscartTable'); $userinfo->load(array('user_id' => $user_id)); $userinfo->credits_total = (double) $userinfo->credits_total; if ($apply_credit_amount > $userinfo->credits_total) { $apply_credit_amount = $userinfo->credits_total; } // get the order object so we can populate it $order = $this->_order; // a TableOrders object (see constructor) // bind what you can from the post $order->bind($values); // unset the order credit because it may have been set by the bind $order->order_credit = '0'; // set the currency Citruscart::load('CitruscartHelperCurrency', 'helpers.currency'); $order->currency_id = CitruscartHelperCurrency::getCurrentCurrency(); // USD is default if no currency selected // set the shipping method $order->shipping = new JObject(); $order->shipping->shipping_price = $values['shipping_price']; $order->shipping->shipping_extra = $values['shipping_extra']; $order->shipping->shipping_name = $values['shipping_name']; $order->shipping->shipping_tax = $values['shipping_tax']; // set the addresses $this->setAddresses($values, false, true); // get the items and add them to the order Citruscart::load('CitruscartHelperCarts', 'helpers.carts'); $items = CitruscartHelperCarts::getProductsInfo(); foreach ($items as $item) { $order->addItem($item); } // get all coupons and add them to the order if (!empty($values['coupons'])) { foreach ($values['coupons'] as $coupon_id) { $coupon = JTable::getInstance('Coupons', 'CitruscartTable'); $coupon->load(array('coupon_id' => $coupon_id)); $order->addCoupon($coupon); } } $this->addAutomaticCoupons(); // get the order totals $order->calculateTotals(); if ($apply_credit_amount > $order->order_total) { $apply_credit_amount = $order->order_total; } // if valid, return the html for the credit $response['msg'] = "<input type='hidden' name='order_credit' value='{$apply_credit_amount}'>"; echo json_encode($response); return; }
/** * Validate Coupon Code * * @return unknown_type */ function validateCouponCode() { $input = JFactory::getApplication()->input; JLoader::import('com_citruscart.library.json', JPATH_ADMINISTRATOR . '/components'); $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $input->getString('elements'))); // convert elements to array that can be binded Citruscart::load('CitruscartHelperBase', 'helpers._base'); $helper = CitruscartHelperBase::getInstance(); $values = $helper->elementsToArray($elements); $coupon_code = $input->get('coupon_code', ''); $response = array(); $response['msg'] = ''; $response['error'] = ''; // check if coupon code is valid $user_id = JFactory::getUser()->id; Citruscart::load('CitruscartHelperCoupon', 'helpers.coupon'); $helper_coupon = new CitruscartHelperCoupon(); $coupon = $helper_coupon->isValid($coupon_code, 'code', $user_id); if (!$coupon) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage($helper_coupon->getError()); echo json_encode($response); return; } if (!empty($values['coupons']) && in_array($coupon->coupon_id, $values['coupons'])) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_THIS_COUPON_ALREADY_ADDED_TO_THE_ORDER')); echo json_encode($response); return; } // TODO Check that the user can add this coupon to the order $can_add = true; if (!$can_add) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_CANNOT_ADD_THIS_COUPON_TO_ORDER')); echo json_encode($response); return; } // Check per product coupon code $ids = array(); $items = CitruscartHelperCarts::getProductsInfo(); foreach ($items as $item) { $ids[] = $item->product_id; } if ($coupon->coupon_type == '1') { $check = $helper_coupon->checkByProductIds($coupon->coupon_id, $ids); if (!$check) { $response['error'] = '1'; $response['msg'] = $helper->generateMessage(JText::_('COM_CITRUSCART_THIS_COUPON_NOT_RELATED_TO_PRODUCT_IN_YOUR_CART')); echo json_encode($response); return; } } // if valid, return the html for the coupon $response['msg'] = " <input type='hidden' name='coupons[]' value='{$coupon->coupon_id}'>"; echo json_encode($response); return; }
public function prepare($values, $options = array(), &$order = null) { if (empty($order)) { $order = $this->getTable(); } $this->_order = $order; $this->_values = $values; $this->_options = $options; if (empty($options['skip_adjust_credits'])) { $order->_adjustCredits = true; // this is not a POS order, so adjust the user's credits (if any used) } $order->bind($values); $order->user_id = $values['user_id']; $order->ip_address = $values['ip_address']; //$_SERVER['REMOTE_ADDR']; // set the currency if (empty($values['currency_id'])) { Citruscart::load('CitruscartHelperCurrency', 'helpers.currency'); $order->currency_id = CitruscartHelperCurrency::getCurrentCurrency(); } // Store the text verion of the currency for order integrity Citruscart::load('CitruscartHelperOrder', 'helpers.order'); $order->order_currency = CitruscartHelperOrder::currencyToParameters($order->currency_id); $saveAddressesToDB = empty($options["save_addresses"]) ? false : true; $this->setAddresses($values, $saveAddressesToDB); // set the shipping method if (isset($values['shippingrequired']) || !empty($values['shipping_plugin'])) { $order->shipping = new JObject(); $order->shipping->shipping_price = $values['shipping_price']; $order->shipping->shipping_extra = $values['shipping_extra']; $order->shipping->shipping_name = $values['shipping_name']; $order->shipping->shipping_tax = $values['shipping_tax']; } if (empty($options['skip_add_items'])) { //get the items from the current user's cart and add them to the order Citruscart::load('CitruscartHelperCarts', 'helpers.carts'); $reviewitems = CitruscartHelperCarts::getProductsInfo(); foreach ($reviewitems as $reviewitem) { $order->addItem($reviewitem); } } if (empty($options['skip_add_coupons'])) { $this->addCoupons($values); } if (empty($options['skip_add_credit']) && !empty($values['order_credit'])) { $order->addCredit($values['order_credit']); } $order->order_state_id = empty($values['orderstate_id']) ? $this->initial_order_state : $values['orderstate_id']; $order->calculateTotals(); $order->getShippingTotal(); $order->getInvoiceNumber(); return $order; }
} require_once dirname(__FILE__) . '/helper.php'; // include lang files $lang = JFactory::getLanguage(); $lang->load('com_citruscart', JPATH_BASE); $lang->load('com_citruscart', JPATH_ADMINISTRATOR); $mainframe = JFactory::getApplication(); $document = JFactory::getDocument(); // params $display_null = $params->get('display_null', '1'); $null_text = $params->get('null_text', 'No Items in Your Cart'); $isAjax = $mainframe->getUserState('mod_usercart.isAjax'); $ajax = $isAjax == '1'; // Grab the cart Citruscart::load('CitruscartHelperCarts', 'helpers.carts'); $items = CitruscartHelperCarts::getProductsInfo(); $num = count($items); // Convert the cart to a "fake" order, to show totals and others things JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_citruscart/tables'); $orderTable = JTable::getInstance('Orders', 'CitruscartTable'); foreach ($items as $item) { $orderTable->addItem($item); } $items = $orderTable->getItems(); Citruscart::load('Citruscart', 'defines'); $config = Citruscart::getInstance(); $show_tax = $config->get('display_prices_with_tax'); if ($show_tax) { Citruscart::load('CitruscartHelperUser', 'helpers.user'); $geozones = CitruscartHelperUser::getGeoZones(JFactory::getUser()->id); if (empty($geozones)) {