Created
September 1, 2022 13:43
-
-
Save webzunft/0d7134d203994c1e92f3798d5416a28e to your computer and use it in GitHub Desktop.
Shortcodes to deliver discount-based information, show prices, and actual discounts. Helpful for dynamic pricing pages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Shortcodes that help with displaying pricing information | |
*/ | |
/** | |
* Show the original price of a given download and price ID | |
* | |
* @param array $atts shortcode attributes. | |
* @param array $content shortcode attributes. | |
*/ | |
function advads_show_price( $atts, $content ) { | |
$atts = shortcode_atts( array( | |
'download_id' => 95170, // All Access | |
'price_id' => 1, | |
'include_decimals' => false, // don’t show decimals | |
'highlight_decimals' => false, // whether to show decimal numbers | |
'include_discounts' => false, // whether to show the discounted value | |
), $atts ); | |
// get the original price | |
$price = edd_get_price_option_amount( $atts['download_id'], $atts['price_id'] ); | |
// calculate discounts | |
if ( $atts['include_discounts'] !== false | |
&& edd_cart_has_discounts() && edd_is_discount_active( edd_get_discount_id_by_code( edd_get_cart_discounts()[0] ) ) ) { | |
$price = edd_get_discounted_amount( edd_get_cart_discounts()[0], $price ); | |
} | |
// format the price | |
$price = edd_format_amount( $price, $atts['include_decimals'] ); | |
// add special markup to decimals | |
if ( $atts['include_decimals'] && $atts['highlight_decimals'] ) { | |
$price_array = explode( '.', $price ); | |
$price_markup = sprintf( '%s<sup>.%s</sup>', $price_array[0], $price_array[1] ); | |
} else { | |
$price_markup = $price; | |
} | |
return $price_markup; | |
} | |
add_shortcode( 'advads_show_price', 'advads_show_price' ); | |
/** | |
* Shortcode that allows showing content only when a user has a discount in the cart | |
* | |
* @param array $atts shortcode attributes. | |
* @param string $content the content between two shortcodes. | |
*/ | |
function advads_has_active_discount( $atts, $content ) { | |
if ( edd_cart_has_discounts() && edd_is_discount_active( edd_get_discount_id_by_code( edd_get_cart_discounts()[0] ) ) ) { | |
return do_shortcode( $content ); | |
} | |
} | |
add_shortcode( 'advads_has_active_discount', 'advads_has_active_discount' ); | |
/** | |
* Shortcode that allows showing content only when a user has NO active discount in the cart | |
* | |
* @param array $atts shortcode attributes. | |
* @param string $content the content between two shortcodes. | |
*/ | |
function advads_no_active_discount( $atts, $content ) { | |
if ( ! edd_cart_has_discounts() || ! edd_is_discount_active( edd_get_discount_id_by_code( edd_get_cart_discounts()[0] ) ) ) { | |
return do_shortcode( $content ); | |
} | |
} | |
add_shortcode( 'advads_no_active_discount', 'advads_no_active_discount' ); | |
/** | |
* Show the currently applied discount | |
* if absolute, show the amount and a currency symbol | |
* if relatice, show the amount and a percentage symbol | |
* | |
* @param array $atts shortcode attributes. | |
* @return string discount amount. | |
*/ | |
function advads_show_discount( $atts ) { | |
if ( empty ( edd_get_cart_discounts()[0] ) ) { | |
return; | |
} | |
$discount = new EDD_Discount( edd_get_discount_id_by_code( edd_get_cart_discounts()[0] ) ); | |
// calculate discounts | |
if ( ! $discount->is_active() ) { | |
return; | |
} | |
$output = ''; | |
switch ( $discount->type ) { | |
case 'flat' : | |
$output = edd_currency_filter( edd_format_amount( $discount->amount ) ); | |
break; | |
case 'percent' : | |
$output = $discount->amount . ' %'; | |
break; | |
} | |
return $output; | |
} | |
add_shortcode( 'advads_show_discount', 'advads_show_discount' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment