/** * Returns the maximum possible discount available given the total amount of points the customer has * * @since 1.0 */ public static function get_discount_for_redeeming_points($applying = false) { global $woocommerce; // get the value of the user's point balance $available_user_discount = WC_Points_Rewards_Manager::get_users_points_value(get_current_user_id()); // no discount if ($available_user_discount <= 0) { return 0; } if ($applying && 'yes' === get_option('wc_points_rewards_partial_redemption_enabled') && !empty($woocommerce->session->wc_points_rewards_discount_amount)) { $requested_user_discount = WC_Points_Rewards_Manager::calculate_points_value($woocommerce->session->wc_points_rewards_discount_amount); if ($requested_user_discount > 0 && $requested_user_discount < $available_user_discount) { $available_user_discount = $requested_user_discount; } } $discount_applied = 0; if (!did_action('woocommerce_before_calculate_totals')) { $woocommerce->cart->calculate_totals(); } // calculate the discount to be applied by iterating through each item in the cart and calculating the individual // maximum discount available foreach ($woocommerce->cart->get_cart() as $item_key => $item) { $discount = 0; $max_discount = WC_Points_Rewards_Product::get_maximum_points_discount_for_product($item['data']); if (is_numeric($max_discount)) { // adjust the max discount by the quantity being ordered $max_discount *= $item['quantity']; // if the discount available is greater than the max discount, apply the max discount $discount = $available_user_discount <= $max_discount ? $available_user_discount : $max_discount; // Max should be product price. As this will be applied before tax, it will respect other coupons. } else { // Use the line price - this is the max we can apply here if (method_exists($item['data'], 'get_price_including_tax')) { $max_discount = $item['data']->get_price_including_tax($item['quantity']); } else { $max_discount = $item['data']->get_price() * $item['quantity']; } // if the discount available is greater than the max discount, apply the max discount $discount = $available_user_discount <= $max_discount ? $available_user_discount : $max_discount; } // add the discount to the amount to be applied $discount_applied += $discount; // reduce the remaining discount available to be applied $available_user_discount -= $discount; } // if the available discount is greater than the order total, make the discount equal to the order total less any other discounts if (get_option('woocommerce_prices_include_tax') == 'no') { $discount_applied = max(0, min($discount_applied, $woocommerce->cart->subtotal_ex_tax - $woocommerce->cart->discount_total)); } else { $discount_applied = max(0, min($discount_applied, $woocommerce->cart->subtotal - $woocommerce->cart->discount_total)); } // limit the discount available by the global maximum discount if set $max_discount = get_option('wc_points_rewards_cart_max_discount'); if ($max_discount && $max_discount < $discount_applied) { $discount_applied = $max_discount; } return $discount_applied; }