Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active January 29, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pebblo/c2dd51d7b5e117810d9b5b70b9366b87 to your computer and use it in GitHub Desktop.
Save Pebblo/c2dd51d7b5e117810d9b5b70b9366b87 to your computer and use it in GitHub Desktop.
EE4 Promotions simple Gift Certificate system. This snippet reduces the value set on a promotion code if is it as 'dollar discount' by the total amount of the transaction it is applied to, once the value reachs 0 it sets the Valid Until date of the promotion code to 'now' so it can no longer be used.
<?php // Please do not include the opening PHP tag if you already have one.
// This snippet reduces the value set on a promotion code if is it as 'dollar discount'
// by the total of the transaction it is applied to, once the value reachs 0 it sets the
// Valid Until date of the promotion code to 'now' so it can no longer be used.
add_filter('FHEE__EED_Promotions__add_promotion_line_item__bypass_increment_promotion_scope_uses', 'tw_ee_poc_promotions_gift_cert', 10, 4);
function tw_ee_poc_promotions_gift_cert( $bypass, $parent_line_item, $promotion, $promotion_line_item) {
// Pull the related EE_Price object.
$promotion_price = $promotion->price();
// Check we have a valid EE_PRice object and the promotion is NOT a percentage discount.
if( $promotion_price instanceof EE_Price && !$promotion->is_percent() ) {
// Pull the users checkout from SSN.
$checkout = EE_Registry::instance()->SSN->checkout();
// Check we have a valid EE_Checkout object.
if ( $checkout instanceof EE_Checkout ) {
// Pull the EE_Transaction from checkout.
$transaction = $checkout->transaction;
// Check we have a valid EE_Transaction object.
if( $transaction instanceof EE_Transaction ){
// Reduce the promotion value by the current transaction total.
$new_promo_price = $promotion_price->amount() - $transaction->total();
// If the sum of the above is negiatve, set the promotion value to 0.
$new_promo_price = max($new_promo_price, 0);
// Set the amount on the promotion price object.
$promotion_price->set_amount($new_promo_price);
// Save the changes to the DB.
$promotion_price->save();
// If the promotion value is now 0, set the value until date to now and save.
if( $new_promo_price === 0) {
$promotion->set_end(date('Y-m-d g:i a'));
$promotion->save();
}
}
}
}
// Setting $bypass to true will bypass the 'uses' value increment when a promotion code is used,
// leaving it false will not bypass the increment.
return $bypass;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment