예제 #1
0
파일: carts.php 프로젝트: annggeel/tienda
 /**
  * Validate Coupon Code
  *
  * @return unknown_type
  */
 function validateCouponCode()
 {
     JLoader::import('com_tienda.library.json', JPATH_ADMINISTRATOR . '/components');
     $elements = json_decode(preg_replace('/[\\n\\r]+/', '\\n', JRequest::getVar('elements', '', 'post', 'string')));
     // convert elements to array that can be binded
     Tienda::load('TiendaHelperBase', 'helpers._base');
     $helper = TiendaHelperBase::getInstance();
     $values = $helper->elementsToArray($elements);
     $coupon_code = JRequest::getVar('coupon_code', '');
     $response = array();
     $response['msg'] = '';
     $response['error'] = '';
     // check if coupon code is valid
     $user_id = JFactory::getUser()->id;
     Tienda::load('TiendaHelperCoupon', 'helpers.coupon');
     $helper_coupon = new TiendaHelperCoupon();
     $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_TIENDA_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_TIENDA_CANNOT_ADD_THIS_COUPON_TO_ORDER'));
         echo json_encode($response);
         return;
     }
     // Check per product coupon code
     $ids = array();
     $items = TiendaHelperCarts::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_TIENDA_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;
 }
예제 #2
0
파일: orders.php 프로젝트: annggeel/tienda
 /**
  * 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;
     }
     Tienda::load('TiendaHelperCoupon', '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 = TiendaHelperCoupon::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', 'TiendaTable');
                 $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
     $dispatcher = JDispatcher::getInstance();
     $dispatcher->trigger("onCalculatePerProductCouponValue", array($this));
     if ($total > $value) {
         return 0;
     } else {
         return $value - $total;
     }
 }
예제 #3
0
 function validateCoupon($values, $options = array())
 {
     $coupon_code = $values['coupon_code'];
     $user_id = $values['user_id'];
     // check if coupon code is valid
     Tienda::load('TiendaHelperCoupon', 'helpers.coupon');
     $helper_coupon = new TiendaHelperCoupon();
     $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_TIENDA_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_TIENDA_THIS_COUPON_NOT_RELATED_TO_PRODUCT_IN_YOUR_CART'));
                 return $this->check();
             }
         }
     }
     return $coupon;
 }