/** * Applies a coupon code * * @param string $coupon_code The code to apply * @return bool True if the coupon is applied, false if it does not exist or cannot be applied */ public static function add_discount($coupon_code) { if (!self::is_valid_coupon($coupon_code)) { return false; } // Check for other individual_use coupons before adding this coupon. foreach (self::get_coupons() as $code) { $current = JS_Coupons::get_coupon($code); if ($current['individual_use']) { fflcommerce::add_error(__("There is already an 'individual use' coupon on the Cart. No other coupons can be added until it is removed.", 'fflcommerce')); return false; } } $coupon = JS_Coupons::get_coupon($coupon_code); // Remove other coupons if this one is individual_use. if ($coupon['individual_use']) { if (!empty(self::$applied_coupons)) { fflcommerce::add_error(__("This is an 'individual use' coupon. All other discount coupons have been removed.", 'fflcommerce')); self::$applied_coupons = array(); } } // check if coupon is already applied and only add a new coupon if (!self::has_discount($coupon_code) && !empty($_POST['coupon_code'])) { self::$applied_coupons[] = $coupon_code; } fflcommerce_session::instance()->coupons = self::$applied_coupons; fflcommerce::add_message(__('Discount coupon applied successfully.', 'fflcommerce')); return true; }