Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Last active June 19, 2019 21:32
Show Gist options
  • Save zackkatz/9f981f95ac47ba93a1cf8aa76b72dce6 to your computer and use it in GitHub Desktop.
Save zackkatz/9f981f95ac47ba93a1cf8aa76b72dce6 to your computer and use it in GitHub Desktop.
Easily run a sale in Easy Digital Downloads. This code applies a coupon globally.
<?php
// When not running a sale, just RETURN here. (but if you forget, no problem...)
# return;
// Pass the coupon code that you want applied as the global sale coupon
new GV_Theme_Sale( 'SOLSTICE2019' );
class GV_Theme_Sale {
/** @var EDD_Discount $discount */
var $discount;
/**
* GV_Theme_Sale constructor.
*/
public function __construct( $code = false ) {
if ( empty( $code ) || ! class_exists('EDD_Discount') ) {
return;
}
$this->discount = new EDD_Discount( $code, true, true );
if( ! $this->should_run() ) {
return;
}
add_action( 'init', array( $this, 'add_discount_to_cart' ) );
add_filter( 'edd_get_download_price', array( $this, 'modify_final_price') );
add_filter( 'edd_final_price', array( $this, 'modify_final_price' ) );
}
/**
* Should the rest of the class initialize?
*/
function should_run() {
// Don't mess with overrides
if ( ! empty( $_GET['discount'] ) ) {
return false;
}
if ( ! class_exists( 'EDD_Discount' ) ) {
return false;
}
if( ! $this->discount ) {
return false;
}
if( ! $this->discount instanceof EDD_Discount ) {
return false;
}
if( ! $this->discount->is_active( false, false ) ) {
return false;
}
if( $this->discount->is_expired( false ) ) {
return false;
}
if( ! $this->discount->is_started( false ) ) {
return false;
}
return true;
}
// Add the discount code to the cart.
function add_discount_to_cart() {
if ( $this->edd_cart_has_renewals() ) {
edd_remove_cart_discount( $this->discount->get_code() );
return;
}
EDD()->session->set( 'preset_discount', $this->discount->get_code() );
edd_set_cart_discount( $this->discount->get_code() );
}
/**
* Check if there's a renewing product in the cart
*
* @param array $cart_contents Optional: pass existing cart contents array
*
* @return bool True: cart contains a renewal; False: cart does not contain a renewal
*/
function edd_cart_has_renewals( $cart_contents = array() ) {
$cart_contents = empty( $cart_contents ) ? edd_get_cart_contents() : $cart_contents;
foreach ( $cart_contents as $cart_item ) {
if( ! empty( $cart_item['options']['is_renewal'] ) || ! empty( $cart_item['item_number']['options']['is_renewal'] ) ) {
return true;
}
}
return false;
}
/**
* Modify price shown in pricing page & extensions
*
* @param int $price
* @param int $download_id
* @param array $user_purchase_info
*
* @return float|int
*/
function modify_final_price( $price = 0, $download_id = 0, $user_purchase_info = array() ) {
// Is checkout page or is loading gateway via AJAX
if( edd_is_checkout() || ( isset( $_POST['action'] ) && 'edd_load_gateway' === $_POST['action'] ) ) {
return $price;
}
$percent = 100 - $this->discount->get_amount();
return (double) $price * $percent / 100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment