/** * Decreases an affiliate's total paid earnings by the specified amount * * @since 1.0 * @return float|bool */ function affwp_decrease_affiliate_earnings($affiliate_id = 0, $amount = '') { if (empty($affiliate_id)) { return false; } if (empty($amount) || floatval($amount) <= 0) { return false; } $earnings = affwp_get_affiliate_earnings($affiliate_id); $earnings -= $amount; $earnings = round($earnings, affwp_get_decimal_count()); if ($earnings < 0) { $earnings = 0; } if (affiliate_wp()->affiliates->update($affiliate_id, array('earnings' => $earnings), '', 'affiliate')) { $alltime = get_option('affwp_alltime_earnings'); $alltime -= $amount; if ($alltime < 0) { $alltime = 0; } update_option('affwp_alltime_earnings', $alltime); return $earnings; } else { return false; } }
/** * Returns a nicely formatted amount. * * @since 1.0 * * @param string $amount Price amount to format * @param string $decimals Whether or not to use decimals. Useful when set to false for non-currency numbers. * * @return string $amount Newly formatted amount or Price Not Available */ function affwp_format_amount($amount, $decimals = true) { global $affwp_options; $thousands_sep = affiliate_wp()->settings->get('thousands_separator', ','); $decimal_sep = affiliate_wp()->settings->get('decimal_separator', '.'); // Format the amount if ($decimal_sep == ',' && false !== ($found = strpos($amount, $decimal_sep))) { $whole = substr($amount, 0, $sep_found); $part = substr($amount, $sep_found + 1, strlen($amount) - 1); $amount = $whole . '.' . $part; } // Strip , from the amount (if set as the thousands separator) if ($thousands_sep == ',' && false !== ($found = strpos($amount, $thousands_sep))) { $amount = str_replace(',', '', $amount); } if (empty($amount)) { $amount = 0; } if ($decimals) { $decimals = apply_filters('affwp_format_amount_decimals', affwp_get_decimal_count(), $amount); } else { $decimals = 0; } $formatted = number_format($amount, $decimals, $decimal_sep, $thousands_sep); return apply_filters('affwp_format_amount', $formatted, $amount, $decimals, $decimal_sep, $thousands_sep); }
/** * Calculate the referral amount * * @param string $amount * @param int $affiliate_id * @param int $reference * @param string $rate * @param int $product_id * @return float */ function affwp_calc_referral_amount($amount = '', $affiliate_id = 0, $reference = 0, $rate = '', $product_id = 0) { $rate = affwp_get_affiliate_rate($affiliate_id, false, $rate, $reference); $type = affwp_get_affiliate_rate_type($affiliate_id); $decimals = affwp_get_decimal_count(); $referral_amount = 'percentage' === $type ? round($amount * $rate, $decimals) : $rate; if ($referral_amount < 0) { $referral_amount = 0; } return (string) apply_filters('affwp_calc_referral_amount', $referral_amount, $affiliate_id, $amount, $reference, $product_id); }