/** * Get the data being exported * * @access public * @since 1.3 * @return array $data Data for Export */ public function get_data() { $args = array('status' => $this->status, 'number' => -1); $data = array(); $affiliates = affiliate_wp()->affiliates->get_affiliates($args); if ($affiliates) { foreach ($affiliates as $affiliate) { $data[] = array('affiliate_id' => $affiliate->affiliate_id, 'email' => affwp_get_affiliate_email($affiliate->affiliate_id), 'payment_email' => affwp_get_affiliate_payment_email($affiliate->affiliate_id), 'username' => affwp_get_affiliate_login($affiliate->affiliate_id), 'rate' => affwp_get_affiliate_rate($affiliate->affiliate_id), 'rate_type' => affwp_get_affiliate_rate_type($affiliate->affiliate_id), 'earnings' => $affiliate->earnings, 'referrals' => $affiliate->referrals, 'visits' => $affiliate->visits, 'status' => $affiliate->status, 'date_registered' => $affiliate->date_registered); } } $data = apply_filters('affwp_export_get_data', $data); $data = apply_filters('affwp_export_get_data_' . $this->export_type, $data); return $data; }
/** * Retrieves the referral rate for an affiliate * * @since 1.0 * @param int $affiliate_id The ID of the affiliate we are getting a rate for * @param bool $formatted Whether to return a formatted rate with %/currency * @param string $product_rate A custom product rate that overrides site/affiliate settings * @return string */ function affwp_get_affiliate_rate($affiliate_id = 0, $formatted = false, $product_rate = '') { // Global referral rate setting, fallback to 20 $default_rate = affiliate_wp()->settings->get('referral_rate', 20); $default_rate = affwp_abs_number_round($default_rate); // Get product-specific referral rate, fallback to global rate $product_rate = affwp_abs_number_round($product_rate); $product_rate = null !== $product_rate ? $product_rate : $default_rate; // Get affiliate-specific referral rate $affiliate_rate = affiliate_wp()->affiliates->get_column('rate', $affiliate_id); // Get rate in order of priority: Affiliate -> Product -> Global $rate = affwp_abs_number_round($affiliate_rate); $rate = null !== $rate ? $rate : $product_rate; // Get the referral rate type $type = affwp_get_affiliate_rate_type($affiliate_id); // Format percentage rates $rate = 'percentage' === $type ? $rate / 100 : $rate; /** * Filter the affiliate rate * * @param string $rate * @param int $affiliate_id * @param string $type */ $rate = (string) apply_filters('affwp_get_affiliate_rate', $rate, $affiliate_id, $type); // Return rate now if formatting is not required if (!$formatted) { return $rate; } // Format the rate based on the type switch ($type) { case 'percentage': $rate = affwp_abs_number_round($rate * 100) . '%'; break; case 'flat': $rate = affwp_currency_filter($rate); break; } return $rate; }
/** * Retrieves the referral rate for an affiliate * * @since 1.0 * @param $affiliate_id int The ID of the affiliate we are getting a rate for * @param $formatted bool Whether to return a formatted rate with %/currency * @param $custom_rate string A custom rate that overrides site/affiliate settings * @return float */ function affwp_get_affiliate_rate($affiliate_id = 0, $formatted = false, $custom_rate = '') { // default rate $rate = affiliate_wp()->settings->get('referral_rate', 20); $affiliate_rate = affiliate_wp()->affiliates->get_column('rate', $affiliate_id); if (!empty($custom_rate)) { $rate = $custom_rate; } elseif (!empty($affiliate_rate)) { $rate = $affiliate_rate; } $type = affwp_get_affiliate_rate_type($affiliate_id); if ('percentage' == $type) { // Sanitize the rate and ensure it's in the proper format if ($rate > 1) { $rate = $rate / 100; } } $rate = apply_filters('affwp_get_affiliate_rate', $rate, $affiliate_id, $type); // If rate should be formatted, format it based on the type if ($formatted) { switch ($type) { case 'percentage': $rate = $rate * 100 . '%'; break; case 'flat': $rate = affwp_currency_filter($rate); break; default: break; } } return $rate; }
function affwp_calc_referral_amount($amount = '', $affiliate_id = 0, $reference = 0, $rate = '', $product_id = 0) { // If the affiliate has a custom rate set, use it. If no rate is specified, use the fallback $rate = affwp_get_affiliate_rate($affiliate_id, false, $rate); if ('percentage' == affwp_get_affiliate_rate_type($affiliate_id)) { $referral_amount = round($amount * $rate, 2); } else { $referral_amount = $rate; } if ($referral_amount < 0) { $referral_amount = 0; } return apply_filters('affwp_calc_referral_amount', $referral_amount, $affiliate_id, $amount, $reference, $product_id); }
function test_get_affiliate_rate_type() { $this->assertEquals('percentage', affwp_get_affiliate_rate_type($this->_affiliate_id)); }
/** * 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); }
/** * Retrieves the rate and type for a specific product * * @access public * @since 1.2 * @return array */ public function calculate_referral_amount($base_amount = '', $reference = '', $product_id = 0) { $rate = ''; $this->affiliate_id = $this->get_affiliate_id(); if (!empty($product_id)) { $rate = $this->get_product_rate($product_id, $args = array('reference' => $reference)); $type = affwp_get_affiliate_rate_type($this->affiliate_id); if ('percentage' == $type) { // Sanitize the rate and ensure it's in the proper format if ($rate > 1) { $rate = $rate / 100; } } } $amount = affwp_calc_referral_amount($base_amount, $this->affiliate_id, $reference, $rate, $product_id); return $amount; }