/** * Records referrals for the affiliate if a discount code belonging to the affiliate is used * * @access public * @since 1.1 */ public function track_discount_referral($payment_id = 0) { $user_info = edd_get_payment_meta_user_info($payment_id); if (isset($user_info['discount']) && $user_info['discount'] != 'none') { if (affiliate_wp()->settings->get('edd_disable_on_renewals')) { $was_renewal = get_post_meta($payment_id, '_edd_sl_is_renewal', true); if ($was_renewal) { return; } } $discounts = array_map('trim', explode(',', $user_info['discount'])); if (empty($discounts)) { return; } foreach ($discounts as $code) { $discount_id = edd_get_discount_id_by_code($code); $affiliate_id = get_post_meta($discount_id, 'affwp_discount_affiliate', true); if (!$affiliate_id) { continue; } $this->affiliate_id = $affiliate_id; if (!affiliate_wp()->tracking->is_valid_affiliate($this->affiliate_id)) { continue; } $existing = affiliate_wp()->referrals->get_by('reference', $payment_id, $this->context); // calculate the referral total $referral_total = $this->get_referral_total($payment_id, $this->affiliate_id); // referral already exists, update it if (!empty($existing->referral_id)) { // If a referral was already recorded, overwrite it with the linked discount affiliate affiliate_wp()->referrals->update($existing->referral_id, array('affiliate_id' => $this->affiliate_id, 'status' => 'unpaid', 'amount' => $referral_total), '', 'referral'); } else { // new referral if (0 == $referral_total && affiliate_wp()->settings->get('ignore_zero_referrals')) { return false; // Ignore a zero amount referral } $desc = $this->get_referral_description($payment_id); if (empty($desc)) { return false; } $referral_id = affiliate_wp()->referrals->add(array('amount' => $referral_total, 'reference' => $payment_id, 'description' => $desc, 'affiliate_id' => $this->affiliate_id, 'context' => $this->context, 'products' => $this->get_products($payment_id))); } } } }
/** * Retrieves the final price of a downloadable product after purchase * this price includes any necessary discounts that were applied * * @since 1.0 * @param int $download_id ID of the download * @param array $user_purchase_info - an array of all information for the payment * @param string $amount_override a custom amount that over rides the 'edd_price' meta, used for variable prices * @return string - the price of the download */ function edd_get_download_final_price($download_id = 0, $user_purchase_info, $amount_override = null) { if (is_null($amount_override)) { $original_price = get_post_meta($download_id, 'edd_price', true); } else { $original_price = $amount_override; } if (isset($user_purchase_info['discount']) && $user_purchase_info['discount'] != 'none') { // if the discount was a %, we modify the amount. Flat rate discounts are ignored if (edd_get_discount_type(edd_get_discount_id_by_code($user_purchase_info['discount'])) != 'flat') { $price = edd_get_discounted_amount($user_purchase_info['discount'], $original_price); } else { $price = $original_price; } } else { $price = $original_price; } return apply_filters('edd_final_price', $price, $download_id, $user_purchase_info); }
/** * Returns the discounts that are global to the cart * * @return array A list of discounts that are global to the cart. */ function get_global_discounts() { $discounts = edd_get_cart_discounts(); if (empty($discounts)) { $discounts = array(); } $discounts = array_map(function ($discount_code) { $discount_id = edd_get_discount_id_by_code($discount_code); return array('code' => $discount_code, 'id' => $discount_id, 'name' => get_post_meta($discount_id, '_edd_discount_name', true)); }, $discounts); return array_filter($discounts, function ($discount) { return !edd_is_discount_not_global($discount['id']); }); }
/** * Show the fully formatted cart discount * * @since 1.4.1 * @param bool $formatted * @param bool $echo Echo? * @return string $amount Fully formatted cart discount */ function edd_display_cart_discount($formatted = false, $echo = false) { $discounts = edd_get_cart_discounts(); if (empty($discounts)) { return false; } $discount_id = edd_get_discount_id_by_code($discounts[0]); $amount = edd_format_discount_rate(edd_get_discount_type($discount_id), edd_get_discount_amount($discount_id)); if ($echo) { echo $amount; } return $amount; }
/** * Increase Discount Usage * * Increases the use count of a discount code. * * @access public * @since 1.0 * @param $code string - the discount code to be incremented * @return int - the new use count */ function edd_increase_discount_usage($code) { $discount_id = edd_get_discount_id_by_code($code); $discounts = edd_get_discounts(); $uses = isset($discounts[$discount_id]['uses']) ? $discounts[$discount_id]['uses'] : false; if ($uses) { $uses++; } else { $uses = 1; } $discounts[$discount_id]['uses'] = $uses; return update_option('edd_discounts', $discounts); }
/** * Discount remove icon */ function pp_edd_get_cart_discounts_html($html, $discounts, $rate, $remove_url) { if (!$discounts) { $discounts = edd_get_cart_discounts(); } if (!$discounts) { return; } $html = ''; foreach ($discounts as $discount) { $discount_id = edd_get_discount_id_by_code($discount); $rate = edd_format_discount_rate(edd_get_discount_type($discount_id), edd_get_discount_amount($discount_id)); $remove_url = add_query_arg(array('edd_action' => 'remove_cart_discount', 'discount_id' => $discount_id, 'discount_code' => $discount), edd_get_checkout_uri()); $svg = '<svg width="16" height="16" viewBox="0 0 16 16"><use xlink:href="' . get_stylesheet_directory_uri() . "/images/svg-defs.svg#icon-remove" . '"></use></svg>'; $html .= "<span class=\"edd_discount\">\n"; $html .= "<span class=\"edd_discount_rate\">{$discount} – {$rate}</span>\n"; $html .= "<a href=\"{$remove_url}\" data-code=\"{$discount}\" class=\"edd_discount_remove\">{$svg}</a>\n"; $html .= "</span>\n"; } return $html; }
/** * Increase Discount Usage * * Increases the use count of a discount code. * * @access public * @since 1.0 * @param $code string - the discount code to be incremented * @return int - the new use count */ function edd_increase_discount_usage($code) { $discount_id = edd_get_discount_id_by_code($code); $uses = edd_get_discount_uses($discount_id); if ($uses) { $uses++; } else { $uses = 1; } update_post_meta($discount_id, '_edd_discount_uses', $uses); return $uses; }