Last active
          June 6, 2017 21:25 
        
      - 
      
- 
        Save ScottDeLuzio/677488447e21a80489a10027ca10eb50 to your computer and use it in GitHub Desktop. 
    Add multiple volume discounts to EDD
  
        
  
    
      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 | |
| /* | |
| * Our discounts are going to be applied to two products. In this case product IDs 54 & 55. Add/modify the $product_ids array as applicable. | |
| * Both products will get the same tiered discounts but we don't want someone to buy 10 of product 54 and 1 of product 55 and get the discount for product 55. | |
| * So, we need to create separate discounts only if the criteria is met for each product. | |
| * The tiered discount will look like this: | |
| * 10-24 of any single product = 25% discount | |
| * 25-99 of any single product = 33% discount | |
| * 100-249 of any single product = 40% discount | |
| * 250-499 of any single product = 45% discount | |
| * 500+ of any single product = 50% discount | |
| */ | |
| function sd_add_discount() { | |
| $product_ids = array( 54, 55 ); // Downloads to apply discount to. | |
| $cart_items = edd_get_cart_contents(); // Get current cart's contents | |
| $discount_ranges = array( | |
| 10 => 24, | |
| 25 => 99, | |
| 100 => 249, | |
| 250 => 499, | |
| 500 => 9999999999 //If anyone buys 10 billion of these I'd shit myself. Really I just need an absurdely large number to check the min/max on the hign end. | |
| ); | |
| if ( !empty( $cart_items ) ){ // Make sure the cart isn't empty | |
| foreach( $cart_items as $item ){ // Loop through each item in the cart | |
| if ( in_array( $item[ 'id' ], $product_ids ) ){ // If the item's ID is one of the downloads we want to apply a discount to, continue. | |
| $range_cycle = 0; // Each time we cycle through the foreach loop we'll increase this count. This lets us apply the correct discount based on the discount ranges set earlier. We also need this to reset the $range_cycle for each product. | |
| $regular_price = edd_get_cart_item_price( $item[ 'id' ] ); | |
| foreach ( $discount_ranges as $min => $max) { | |
| if ( ( $min <= $item[ 'quantity' ] ) && ( $item[ 'quantity' ] <= $max ) ){ | |
| switch ( $range_cycle ) { | |
| case 0: | |
| $discount_pct = 0.25; | |
| $discount_amount[ $item[ 'id' ] ] = $item[ 'quantity' ] * $regular_price * $discount_pct; | |
| break; | |
| case 1: | |
| $discount_pct = 0.33; | |
| $discount_amount[ $item[ 'id' ] ] = $item[ 'quantity' ] * $regular_price * $discount_pct; | |
| break; | |
| case 2: | |
| $discount_pct = 0.40; | |
| $discount_amount[ $item[ 'id' ] ] = $item[ 'quantity' ] * $regular_price * $discount_pct; | |
| break; | |
| case 3: | |
| $discount_pct = 0.45; | |
| $discount_amount[ $item[ 'id' ] ] = $item[ 'quantity' ] * $regular_price * $discount_pct; | |
| break; | |
| case 4: | |
| $discount_pct = 0.50; | |
| $discount_amount[ $item[ 'id' ] ] = $item[ 'quantity' ] * $regular_price * $discount_pct; | |
| break; | |
| default: | |
| $discount_amount[ $item[ 'id' ] ] = 0.00; | |
| break; | |
| } | |
| } | |
| $range_cycle++; | |
| } //foreach $discount_ranges | |
| } //if in_array | |
| if ( is_array( $discount_amount ) ){ | |
| foreach( $discount_amount as $id => $value ){ | |
| if ( $value != 0 ){ | |
| $amount = -1* $value; | |
| $label = get_the_title( $id ) . ' Discount'; | |
| $id = 'volume_discount_' . $id; | |
| EDD()->fees->add_fee( $amount, $label, $id ); | |
| } | |
| } | |
| } | |
| }//foreach $cart_items | |
| }//if !empty $cart_items | |
| } | |
| add_action( 'init', 'sd_add_discount' ); | |
| /* | |
| * Need to be able to remove a discount if the download is removed from the cart. | |
| */ | |
| function sd_remove_discount(){ | |
| $product_ids = array( 54, 55 ); | |
| $cart_items = edd_get_cart_contents(); | |
| var_dump($cart_items); | |
| if ( !empty( $cart_items ) ){ // Make sure the cart isn't empty | |
| $download_ids = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : false; | |
| if ( $download_ids ){ | |
| foreach ( $product_ids as $product ){ | |
| if ( !in_array( $product, $download_ids ) ){ | |
| EDD()->fees->remove_fee( 'volume_discount_' . $product ); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| add_action( 'init', 'sd_remove_discount' ); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
This very well may not be the best way to accomplish this, but it is functional. The need was to have two separate volume based discounts for two different products applied to the customer's cart. The discount also needed to be removed if the product was removed from the cart.
Any feedback is welcome.