Example #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;
 }
Example #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;
 }
Example #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;
     }
 }