Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danieliser/23de0f95d7a66157962c347d82827936 to your computer and use it in GitHub Desktop.
Save danieliser/23de0f95d7a66157962c347d82827936 to your computer and use it in GitHub Desktop.
Custom EDD Template Functions For Sale Pricing
function __get_wp_fusion_auto_applied_discount()
{
$user_tags = wpf_get_tags(false, true);
$args = [
"post_status" => "active",
"meta_query" => [
[
"key" => "wpf_settings",
"compare" => "EXISTS",
],
],
];
$discounts = edd_get_discounts($args);
if (empty($discounts)) {
return false;
}
foreach ($discounts as $discount) {
$settings = (array) edd_get_adjustment_meta(
$discount->id,
"wpf_settings",
true
);
if (empty($settings["auto_apply_tags"])) {
continue;
}
if (!empty(array_intersect($user_tags, $settings["auto_apply_tags"]))) {
return $discount;
}
}
return false;
}
function __get_current_discount($code = false)
{
if ($code) {
$discount = edd_get_discount_by_code($code);
if ($discount && is_a($discount, "EDD_Discount")) {
return $discount; // Return discount details if the code is valid
}
}
$wpf_discount = __get_wp_fusion_auto_applied_discount();
if ($wpf_discount) {
return $wpf_discount;
}
// Check for a discount code in the URL
$url_discount = !empty($_GET["discount"]) ? $_GET["discount"] : false;
if ($url_discount) {
$discount = edd_get_discount_by_code($url_discount);
if ($discount && is_a($discount, "EDD_Discount")) {
return $discount; // Return discount details if the code is valid
}
}
// Check for any discounts applied directly to the cart
$cart_discounts = edd_get_cart_discounts();
if (!empty($cart_discounts)) {
foreach ($cart_discounts as $discount_code) {
$discount_details = edd_get_discount_by_code($discount_code);
if ($discount_details && is_a($discount_details, "EDD_Discount")) {
return $discount_details; // Return the first valid EDD_Discount object found
}
}
}
// Return false if no discounts found
return false;
}
function HasDiscount($slug_or_id = null, $code = false)
{
// Retrieve the current discount
$discount = __get_current_discount($code);
return is_a($discount, "EDD_Discount");
}
function DiscountedPrice($slug_or_id = null, $code = false)
{
global $post;
if ( null === $slug_or_id ) {
$slug_or_id = isset( $post ) ? $post->ID : null;
}
// Retrieve the current discount
$discount = __get_current_discount($code);
// Determine if the input is a numeric ID or a slug and retrieve the download ID
$download = edd_get_download($slug_or_id);
if ( ! $download ) {
return '';
}
// Get the original price of the download
$download_price = edd_get_download_price($download->ID);
if (!$discount || !edd_validate_discount($discount->ID, $download->ID)) {
$discounted_price = $download_price; // No discount available
} else {
// Apply the discount based on its type
if ($discount->type == "percent") {
$discount_amount = ($download_price * $discount->amount) / 100;
$discounted_price = $download_price - $discount_amount;
} elseif ($discount->type == "flat") {
$discounted_price = max($download_price - $discount->amount, 0); // Ensure price does not go negative
}
}
// Format the final price using EDD's formatting functions
$formatted_price = edd_format_amount($discounted_price);
$formatted_price =
$discounted_price == floor($discounted_price)
? number_format($discounted_price, 0)
: number_format($discounted_price, 2);
return $formatted_price; // Return formatted discounted price
}
function DiscountValue($slug_or_id = null, $code = false)
{
// Determine if the input is a numeric ID or a slug and retrieve the download ID
$download = edd_get_download($slug_or_id);
if ( ! $download ) {
return '0';
}
// Get the original price of the download
$download_price = edd_get_download_price($download->ID);
// Retrieve the current discount
$discount = __get_current_discount($code);
if (!$discount || !edd_validate_discount($discount->ID, $download->ID)) {
return 0; // No discount available, return 0
}
// Calculate the discount amount based on its type
if ($discount->type == "percent") {
$discount_amount = ($download_price * $discount->amount) / 100;
} elseif ($discount->type == "flat") {
$discount_amount = min($discount->amount, $download_price); // Ensure discount does not exceed the price
} else {
$discount_amount = 0; // If discount type is unknown, return 0
}
return number_format($discount_amount, 2); // Return the discount amount formatted to two decimal places
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment