Skip to content

Instantly share code, notes, and snippets.

@w3guy
Forked from zackkatz/edd-run-a-sale.php
Created June 19, 2019 21:32
Show Gist options
  • Save w3guy/4a20f5bea9e798f6a8a0f8fcfa4d82a1 to your computer and use it in GitHub Desktop.
Save w3guy/4a20f5bea9e798f6a8a0f8fcfa4d82a1 to your computer and use it in GitHub Desktop.

Revisions

  1. @zackkatz zackkatz revised this gist Jun 18, 2019. 1 changed file with 21 additions and 1 deletion.
    22 changes: 21 additions & 1 deletion edd-run-a-sale.php
    Original file line number Diff line number Diff line change
    @@ -73,14 +73,34 @@ function should_run() {

    function add_discount_to_cart() {

    if ( gvtheme_edd_cart_has_renewals() ) {
    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
  2. @zackkatz zackkatz revised this gist Jun 18, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion edd-run-a-sale.php
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,8 @@ function add_discount_to_cart() {
    */
    function modify_final_price( $price = 0, $download_id = 0, $user_purchase_info = array() ) {

    if( gvtheme_edd_is_checkout() ) {
    // Is checkout page or is loading gateway via AJAX
    if( edd_is_checkout() || ( isset( $_POST['action'] ) && 'edd_load_gateway' === $_POST['action'] ) ) {
    return $price;
    }

  3. @zackkatz zackkatz created this gist Jun 18, 2019.
    104 changes: 104 additions & 0 deletions edd-run-a-sale.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,104 @@
    <?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;
    }
    }