Exemplo 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;
 }
Exemplo 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;
 }
Exemplo n.º 3
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;
 }