Created
August 30, 2016 12:13
-
-
Save ashleyfae/ff650536ee3884400f2ea55d1282da84 to your computer and use it in GitHub Desktop.
Automatically applies an EDD discount code if certain cart conditions are met.
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 | |
/** | |
* Plugin Name: EDD - Discounts for Subscribers | |
* Plugin URI: https://www.nosegraze.com/apply-edd-discount-code-cart-contents | |
* Description: Automatically applies an EDD discount code if certain cart conditions are met. | |
* Version: 1.0 | |
* Author: Nose Graze | |
* Author URI: https://www.nosegraze.com | |
* License: GPL2 | |
* | |
* @package edd-auto-apply-discount-code | |
* @copyright Copyright (c) 2016, Nose Graze Ltd. | |
* @license GPL2+ | |
* | |
* INSTRUCTIONS: | |
* | |
* This plugin isn't meant to be installed as-is. You'll need to comb through the code and | |
* make changes for it to apply to your circumstances and requirements. | |
* | |
* First create a discount code in EDD with no restrictions. We'll be adding the restrictions | |
* manually in the code. | |
* | |
* Then edit the discount name in the code to reflect your own. | |
* | |
* Finally, you'll need to edit this logic to suit your needs. | |
*/ | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
/** | |
* Maybe Apply 20% Discount | |
* | |
* Automatically applied if UBB is in the cart and at least one theme or paid add-on. | |
* | |
* @since 1.0 | |
* @return void | |
*/ | |
function ng_edd_maybe_apply_discounts() { | |
$cart_items = edd_get_cart_contents(); | |
$discount_code = 'UBB_Bonus'; // Change this to your custom discount code. | |
$discount_applies = $has_ubb = $has_theme = $has_addons = false; | |
if ( is_array( $cart_items ) ) { | |
foreach ( $cart_items as $item ) { | |
$post = get_post( $item['id'] ); | |
$currentprice = get_post_meta( $post->ID, 'edd_price', true ); | |
// This is the Ultimate Book Blogger plugin. | |
if ( $item['id'] == 8779 ) { | |
$has_ubb = true; | |
} | |
// Product is a WordPress theme (in the "WordPress Themes" category). | |
if ( has_term( 'WordPress Themes', 'download_category', $post ) ) { | |
$has_theme = true; | |
} | |
// Product is a UBB Add-On with a price greater than zero. | |
if ( has_term( 'UBB Add-On', 'download_tag', $post ) && $currentprice > 0 ) { | |
$has_addons = true; | |
} | |
} | |
} | |
// Has UBB and a theme. | |
if ( $has_ubb && $has_theme ) { | |
$discount_applies = true; | |
} | |
// Has UBB and one or more add-ons. | |
if ( $has_ubb && $has_addons ) { | |
$discount_applies = true; | |
} | |
if ( $discount_applies ) { | |
edd_set_cart_discount( $discount_code ); | |
} else { | |
edd_unset_cart_discount( $discount_code ); | |
} | |
} | |
add_action( 'edd_cart_items_before', 'ng_edd_maybe_apply_discounts' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment