/** * @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) { fflcommerce::add_error(__('Coupon does not exist or is no longer valid!', 'fflcommerce')); 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)) { fflcommerce::add_error(sprintf(__("The coupon '%s' is invalid with that payment method!", 'fflcommerce'), $coupon['code'])); return false; } /* Subtotal minimum or maximum. */ if (!empty($coupon['order_total_min']) || !empty($coupon['order_total_max'])) { /* Can't use the fflcommerce_cart::get_cart_subtotal() method as it's not ready at this point yet. */ $subtotal = self::$cart_contents_total; $order_total_max = apply_filters('fflcommerce_coupon_order_total_max', $coupon['order_total_max'], $coupon); if (!empty($coupon['order_total_max']) && $subtotal > $order_total_max) { fflcommerce::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.', 'fflcommerce'), $order_total_max, $coupon['code'])); return false; } $order_total_min = apply_filters('fflcommerce_coupon_order_total_min', $coupon['order_total_min'], $coupon); if (!empty($coupon['order_total_min']) && $subtotal < $order_total_min) { fflcommerce::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.', 'fflcommerce'), $order_total_min, $coupon['code'])); return false; } } // Check if coupon products are in cart if (!fflcommerce_cart::has_valid_products_for_coupon($coupon)) { fflcommerce::add_error(__('No products in your cart match that coupon!', 'fflcommerce')); return false; } return true; }