示例#1
0
 /**
  * @param $coupon string Coupon code to check.
  * @return bool Whether specified code is valid for current cart.
  */
 public static function is_valid_coupon($coupon)
 {
     $coupon = JS_Coupons::get_coupon($coupon);
     if (!$coupon) {
         jigoshop::add_error(__('Coupon does not exist or is no longer valid!', 'jigoshop'));
         return false;
     }
     $payment_method = !empty($_POST['payment_method']) ? $_POST['payment_method'] : '';
     $pay_methods = (array) $coupon['pay_methods'];
     /* Whether the order has a valid payment method which the coupon requires. */
     if (!empty($pay_methods) && !empty($payment_method) && !in_array($payment_method, $pay_methods)) {
         jigoshop::add_error(sprintf(__("The coupon '%s' is invalid with that payment method!", 'jigoshop'), $coupon['code']));
         return false;
     }
     /* Subtotal minimum or maximum. */
     if (!empty($coupon['order_total_min']) || !empty($coupon['order_total_max'])) {
         /* Can't use the jigoshop_cart::get_cart_subtotal() method as it's not ready at this point yet. */
         $subtotal = self::$cart_contents_total;
         $order_total_max = apply_filters('jigoshop_coupon_order_total_max', $coupon['order_total_max'], $coupon);
         if (!empty($coupon['order_total_max']) && $subtotal > $order_total_max) {
             jigoshop::add_error(sprintf(__('Your subtotal does not match the <strong>maximum</strong> order total requirements of %.2f for coupon "%s" and it has been removed.', 'jigoshop'), $order_total_max, $coupon['code']));
             return false;
         }
         $order_total_min = apply_filters('jigoshop_coupon_order_total_min', $coupon['order_total_min'], $coupon);
         if (!empty($coupon['order_total_min']) && $subtotal < $order_total_min) {
             jigoshop::add_error(sprintf(__('Your subtotal does not match the <strong>minimum</strong> order total requirements of %.2f for coupon "%s" and it has been removed.', 'jigoshop'), $order_total_min, $coupon['code']));
             return false;
         }
     }
     // Check if coupon products are in cart
     if (!jigoshop_cart::has_valid_products_for_coupon($coupon)) {
         jigoshop::add_error(__('No products in your cart match that coupon!', 'jigoshop'));
         return false;
     }
     return true;
 }