-
-
Save w3guy/4a20f5bea9e798f6a8a0f8fcfa4d82a1 to your computer and use it in GitHub Desktop.
Easily run a sale in Easy Digital Downloads. This code applies a coupon globally.
This file contains hidden or 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 | |
| // 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 ( gvtheme_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() ); | |
| } | |
| /** | |
| * 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() ) { | |
| if( gvtheme_edd_is_checkout() ) { | |
| 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