Esempio n. 1
0
 function validateCoupon($values, $options = array())
 {
     $coupon_code = $values['coupon_code'];
     $user_id = $values['user_id'];
     // check if coupon code is valid
     Citruscart::load('CitruscartHelperCoupon', 'helpers.coupon');
     $helper_coupon = new CitruscartHelperCoupon();
     $coupon = $helper_coupon->isValid($coupon_code, 'code', $user_id);
     if (!$coupon) {
         $this->setError($helper_coupon->getError());
         return $this->check();
     }
     if (!empty($values['coupons']) && in_array($coupon->coupon_id, $values['coupons'])) {
         $this->setError(JText::_('COM_CITRUSCART_THIS_COUPON_ALREADY_ADDED_TO_THE_ORDER'));
         return $this->check();
     }
     // Check per product coupon code
     if (!empty($values['cartitems']) && $coupon->coupon_type == '1') {
         $ids = array();
         foreach ($values['cartitems'] as $item) {
             if (is_int($item)) {
                 $ids[] = $item;
             } elseif (is_object($item)) {
                 $ids[] = $item->product_id;
             }
         }
         if (!empty($ids)) {
             if (!($check = $helper_coupon->checkByProductIds($coupon->coupon_id, $ids))) {
                 $this->setError(JText::_('COM_CITRUSCART_THIS_COUPON_NOT_RELATED_TO_PRODUCT_IN_YOUR_CART'));
                 return $this->check();
             }
         }
     }
     return $coupon;
 }
Esempio n. 2
0
 /**
  * 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 = $this->user->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;
 }
Esempio n. 3
0
 /**
  * Calculates the discounted value of a per_product coupon
  *
  * @param   $product_id     the product id
  * @param   $value          the original value on which calculate the discount
  * @param   $type           could be price, tax, shipping
  */
 function calculatePerProductCouponValue($product_id, $value, $type = 'price')
 {
     $total = 0.0;
     switch ($type) {
         case 'shipping':
             $coupons = !empty($this->_coupons['product_shipping']) ? $this->_coupons['product_shipping'] : array();
             break;
         default:
         case 'price':
             $coupons = !empty($this->_coupons['product_price']) ? $this->_coupons['product_price'] : array();
             break;
     }
     Citruscart::load('CitruscartHelperCoupon', 'helpers.coupon');
     // If there are per_product coupons that apply to this product
     // adjust the orderitem_final_price here.
     // remember that the orderitem_final_price already == product_price * orderitem_quantity
     if ($coupons) {
         foreach ($coupons as $coupon) {
             $is_for_this_product = CitruscartHelperCoupon::checkByProductIds($coupon->coupon_id, array($product_id));
             if ($is_for_this_product) {
                 switch ($coupon->coupon_value_type) {
                     case "1":
                         // percentage
                         $amount = $coupon->coupon_value / 100 * $value;
                         break;
                     case "0":
                         // flat-rate
                         $amount = $coupon->coupon_value;
                         break;
                 }
                 // update the total amount of the discount
                 $total += $amount;
                 // save the ordercoupons object
                 $oc = JTable::getInstance('OrderCoupons', 'CitruscartTable');
                 $oc->coupon_id = $coupon->coupon_id;
                 $oc->ordercoupon_name = $coupon->coupon_name;
                 $oc->ordercoupon_code = $coupon->coupon_code;
                 $oc->ordercoupon_value = $coupon->coupon_value;
                 $oc->ordercoupon_value_type = $coupon->coupon_value_type;
                 $oc->ordercoupon_amount = $amount;
                 $this->_ordercoupons[] = $oc;
             }
         }
     }
     // Allow this to be modified via plugins
     JFactory::getApplication()->triggerEvent("onCalculatePerProductCouponValue", array($this));
     if ($total > $value) {
         return 0;
     } else {
         return $value - $total;
     }
 }
Esempio n. 4
0
 /**
  * Prepares data for and returns the html of the order summary layout.
  * This assumes that $this->_order has already had its properties set
  *
  * @return unknown_type
  */
 function getOrderSummary()
 {
     // get the order object
     $order = $this->_order;
     // a TableOrders object (see constructor)
     Citruscart::load('CitruscartHelperCoupon', 'helpers.coupon');
     // Coupons
     $coupons_id = array();
     $coupons = $order->getCoupons();
     foreach ($coupons as $cg) {
         foreach ($cg as $c) {
             if ($c->coupon_type == '1') {
                 $coupons_id = array_merge($coupons_id, CitruscartHelperCoupon::getCouponProductIds($c->coupon_id));
             }
         }
     }
     $model = $this->getModel('carts');
     $view = $this->getView('checkout', 'html');
     $view->set('_controller', 'checkout');
     $view->set('_view', 'checkout');
     $view->set('_doTask', true);
     $view->set('hidemenu', true);
     $view->setModel($model, true);
     $view->assign('state', $model->getState());
     $view->assign('coupons', $coupons_id);
     $config = Citruscart::getInstance();
     $show_tax = $config->get('display_prices_with_tax');
     $view->assign('show_tax', $show_tax);
     $view->assign('using_default_geozone', false);
     $view->assign('order', $order);
     $orderitems = $order->getItems();
     Citruscart::load("CitruscartHelperBase", 'helpers._base');
     $order_helper = CitruscartHelperBase::getInstance('Order');
     $orderitems = $order->getItems();
     Citruscart::load('CitruscartHelperTax', 'helpers.tax');
     if ($show_tax) {
         $taxes = CitruscartHelperTax::calculateTax($orderitems, 1, $order->getBillingAddress(), $order->getShippingAddress());
     }
     $tax_sum = 0;
     foreach ($orderitems as $item) {
         $item->price = $item->orderitem_price + floatval($item->orderitem_attributes_price);
         if ($show_tax) {
             $item->price = $item->orderitem_price + floatval($item->orderitem_attributes_price) + $taxes->product_taxes[$item->product_id]->amount;
             $item->orderitem_final_price = $item->price * $item->orderitem_quantity;
             $tax_sum += $taxes->product_taxes[$item->product_id]->amount * $item->orderitem_quantity;
         }
     }
     $order->order_subtotal += $tax_sum;
     if (empty($order->user_id)) {
         //$order->order_total += $tax_sum;
         $order->order_tax += $tax_sum;
     }
     $view->assign('orderitems', $orderitems);
     // Checking whether shipping is required
     $showShipping = false;
     $cartsModel = $this->getModel('carts');
     if ($isShippingEnabled = $cartsModel->getShippingIsEnabled()) {
         $showShipping = true;
         $view->assign('shipping_total', $order->getShippingTotal());
     }
     $view->assign('showShipping', $showShipping);
     //START onDisplayOrderItem: trigger plugins for extra orderitem information
     if (!empty($orderitems)) {
         $onDisplayOrderItem = $order_helper->onDisplayOrderItems($orderitems);
         $view->assign('onDisplayOrderItem', $onDisplayOrderItem);
     }
     //END onDisplayOrderItem
     $view->setLayout('cart');
     ob_start();
     $view->display();
     $html = ob_get_contents();
     ob_end_clean();
     return $html;
 }
Esempio n. 5
0
 /**
  * Validate Coupon Code
  *
  * @return unknown_type
  */
 function validateCouponCode()
 {
     $app = JFactory::getApplication();
     JLoader::import('com_citruscart.library.json', JPATH_ADMINISTRATOR . '/components');
     $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', $app->input->post->get('elements', '')));
     // convert elements to array that can be binded
     Citruscart::load('CitruscartHelperBase', 'helpers._base');
     $helper = CitruscartHelperBase::getInstance();
     $values = $helper->elementsToArray($elements);
     $values['sameasbilling'] = isset($values['_checked']['sameasbilling']) && !empty($values['_checked']['sameasbilling']);
     $coupon_code = $app->input->getString('coupon_code', '');
     //$coupon_code = JRequest::getVar('coupon_code', '');
     $response = array();
     $response['msg'] = '';
     $response['error'] = '';
     // check if coupon code is valid
     $session = JFactory::getSession();
     $user_id = $session->get('user_id', '', 'citruscart_pos');
     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_COUPON_NOTICE'));
         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_COUPON_NOTICE'));
         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;
 }