Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tobeyadr/de1c9a9ee94109f482577e606659a0e8 to your computer and use it in GitHub Desktop.
Save tobeyadr/de1c9a9ee94109f482577e606659a0e8 to your computer and use it in GitHub Desktop.
Set price_id to download default if license is expired for over 3 months
<?php
add_filter( 'edd_add_to_cart_item', 'update_price_id_during_renewal_if_license_expired_past_3_months', 10, 1 );
/**
* Update the price_id to the download's default if the license is expired by 3 months
*
* @param $item
*
* @return array the item
*/
function update_price_id_during_renewal_if_license_expired_past_3_months( $item ) {
$download_id = $item['id'];
$options = $item['options'];
// Product being added to the cart is not a renewal
if ( ! isset( $options['is_renewal'] ) || ! isset( $options['license_key'] ) || ! isset( $options['license_id'] ) ) {
return $item;
}
$license = edd_software_licensing()->get_license( $options['license_id'] );
// License is not expired or there is no expiration date
if ( ! $license || ! $license->is_expired() || ! $license->expiration ) {
return $item;
}
try {
$expiration_date = new DateTime('now', wp_timezone() );
$expiration_date->setTimestamp( $license->expiration );
$expiration_date->modify( '+3 months' );
} catch ( Exception $exception ) {
return $item;
}
// current date is withing 3 months of expiration date
if ( $expiration_date->getTimestamp() > time() ) {
return $item;
}
// Set the price id to the default variable price
$options['price_id'] = edd_get_default_variable_price( $download_id );
$item['options'] = $options;
return $item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment