/** * Set the active discount for the shopping cart * * @since 1.4.1 * @param string $code Discount code * @return string[] All currently active discounts */ function edd_set_cart_discount($code = '') { if (edd_multiple_discounts_allowed()) { // Get all active cart discounts $discounts = edd_get_cart_discounts(); } else { $discounts = false; // Only one discount allowed per purchase, so override any existing } if ($discounts) { $key = array_search($code, $discounts); if (false !== $key) { unset($discounts[$key]); // Can't set the same discount more than once } $discounts[] = $code; } else { $discounts = array(); $discounts[] = $code; } EDD()->session->set('cart_discounts', implode('|', $discounts)); return $discounts; }
/** * Purchase Form Validate Discounts * * @access private * @since 1.0.8.1 * @return string */ function edd_purchase_form_validate_discounts() { // Retrieve the discount stored in cookies $discounts = edd_get_cart_discounts(); $user = ''; if (isset($_POST['edd_user_login']) && !empty($_POST['edd_user_login'])) { $user = sanitize_text_field($_POST['edd_user_login']); } else { if (isset($_POST['edd_email']) && !empty($_POST['edd_email'])) { $user = sanitize_text_field($_POST['edd_email']); } else { if (is_user_logged_in()) { $user = wp_get_current_user()->user_email; } } } $error = false; // Check for valid discount(s) is present if (!empty($_POST['edd-discount']) && __('Enter discount', 'easy-digital-downloads') != $_POST['edd-discount']) { // Check for a posted discount $posted_discount = isset($_POST['edd-discount']) ? trim($_POST['edd-discount']) : false; // Add the posted discount to the discounts if ($posted_discount && (empty($discounts) || edd_multiple_discounts_allowed()) && edd_is_discount_valid($posted_discount, $user)) { edd_set_cart_discount($posted_discount); } } // If we have discounts, loop through them if (!empty($discounts)) { foreach ($discounts as $discount) { // Check if valid if (!edd_is_discount_valid($discount, $user)) { // Discount is not valid $error = true; } } } else { // No discounts return 'none'; } if ($error) { edd_set_error('invalid_discount', __('One or more of the discounts you entered is invalid', 'easy-digital-downloads')); } return implode(', ', $discounts); }