Skip to content

Instantly share code, notes, and snippets.

@michael-cannon
Last active August 29, 2015 14:07
Show Gist options
  • Save michael-cannon/b8b5f1e3925fd918f534 to your computer and use it in GitHub Desktop.
Save michael-cannon/b8b5f1e3925fd918f534 to your computer and use it in GitHub Desktop.
Modify the item being added to the cart during EDD Software License renewal. Why? A year ago, I offered a single, cheap unlimited license, but that's not a sustainable business model. Overtime, I raised the price and built up to 4 licensing levels. This script looks too see if a license renewal is happening and correct the pricing from unlimited…
<?php
add_filter( 'edd_add_to_cart_item', 'aihrus_add_to_cart_item' );
function aihrus_add_to_cart_item( $item ) {
if ( empty( $_GET['edd_license_key'] ) ) {
return $item;
} else {
$license = ! empty( $_GET['edd_license_key'] ) ? sanitize_text_field( $_GET['edd_license_key'] ) : false;
if ( ! $license ) {
return $item;
}
}
$license_id = edd_software_licensing()->get_license_by_key( $license );
if ( empty( $license_id ) ) {
return $item;
}
$download_id = ! empty( $_GET['download_id'] ) ? absint( $_GET['download_id'] ) : false;
if ( empty( $download_id ) ) {
return $item;
}
$payment_id = get_post_meta( $license_id, '_edd_sl_payment_id', true );
$cart_details = edd_get_payment_meta_cart_details( $payment_id, true );
if ( empty( $cart_details ) ) {
return $item;
}
// current single site pricing by product
$base_prices = array(
14714 => 29.99,
17383 => 39.99,
19963 => 14.99,
);
$base_price = $base_prices[ $download_id ];
// current single site position in options, 0 is first so 3 is fourth
$price_ids = array(
14714 => 3,
17383 => 3,
19963 => 3,
);
$price_id = $price_ids[ $download_id ];
// for some reason or another, despite changing the item added to the cart before license discounting, the discount is wrong. The factor number is inline with my pricing strategy of doubling the price per licensing level. In the end, it works.
$renewal_discount = 20;
$discount_factor = 8;
$changed_price_id = false;
foreach ( $cart_details as $key => $detail ) {
if ( empty( $detail['id'] ) || $download_id != $detail['id'] ) {
continue;
}
if ( empty( $detail['price'] ) ) {
continue;
}
$price = $detail['price'];
$compare = bccomp( $base_price, $price, 2 );
if ( 1 == $compare ) {
$item['options']['price_id'] = $price_id;
$changed_price_id = true;
} elseif ( 0 == $compare ) {
$item['options']['price_id'] = $price_id;
$changed_price_id = true;
}
}
if ( $changed_price_id ) {
global $edd_options;
// on WP Engine gotta love their superpowered caching…
$edd_options['edd_sl_renewal_discount'] = $renewal_discount/$discount_factor;
}
return $item;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment