Skip to content

Instantly share code, notes, and snippets.

@MaximilianoRicoTabo
Last active February 19, 2024 13:59
Show Gist options
  • Save MaximilianoRicoTabo/42d27ef56e343710bb62249fddf7dfea to your computer and use it in GitHub Desktop.
Save MaximilianoRicoTabo/42d27ef56e343710bb62249fddf7dfea to your computer and use it in GitHub Desktop.
Apply a discount code to child level at the moment of checkout
<?php
/**
* Apply a discount code to child level at the moment of checkout
*
* link: TBD
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
// Remove the default pmprogroupacct_pmpro_checkout_level function
function remove_pmprogroupacct_pmpro_checkout_parent() {
remove_action( 'pmpro_checkout_level', 'pmprogroupacct_pmpro_checkout_level_parent' );
}
add_filter('init', 'remove_pmprogroupacct_pmpro_checkout_parent');
add_action( 'pmpro_checkout_level', 'custom_pmprogroupacct_pmpro_checkout_parent', 9, 1 );
/**
* Custom function to add a discount to the checkout page in group accounts
*
* @param object $level The level object being checked out for.
* @return object $level The level object being checked out for.
*/
function custom_pmprogroupacct_pmpro_checkout_parent( $level ) {
//Modify line below as needed
$discount_code_to_match = "4D71A54CD2";
//Get the discount code from the request
//If v2.12 use line below
$discount_code = isset( $_REQUEST['discount_code'] ) ? sanitize_text_field( $_REQUEST['discount_code'] ) : '';
//If v3 use line below instead
//$discount_code = isset( $_REQUEST['pmpro_discount_code'] ) ? sanitize_text_field( $_REQUEST['pmpro_discount_code'] ) : '';
//bail if discount code is not the one we are looking for
if ($discount_code_to_match == $discount_code) {
// Remove the default pmprogroupacct_pmpro_checkout_boxes_parent function
remove_action('pmpro_checkout_boxes', 'pmprogroupacct_pmpro_checkout_boxes_parent');
add_action( 'pmpro_checkout_boxes', 'custom_pmprogroupacct_pmpro_checkout_boxes_parent' );
//Modify this cost as needed
$each_seat_cost = 6;
}
//Check if group accounts add on is active otherwise bail
if ( ! function_exists( 'pmprogroupacct_get_settings_for_level' ) ) {
return $level;
}
// Get the group settings for this level.
$settings = null;
if ( ! empty( $level->id ) ) {
$settings = pmprogroupacct_get_settings_for_level( $level->id );
}
// If there are no settings, then this is not a group parent level. Bail.
if ( empty( $settings ) ) {
return $level;
}
// Get the number of seats being purchased.
$seats = isset( $_REQUEST['pmprogroupacct_seats'] ) ? intval( $_REQUEST['pmprogroupacct_seats'] ) : 0;
// If the number of seats is not an integer, bail.
if ( empty( $seats ) || ! is_numeric( $seats ) ) {
return $level;
}
// If the pricing model is not set to fixed, bail.
if( 'none' === $settings['pricing_model'] ) {
return $level;
}
$seat_cost = 0;
switch ( $settings['pricing_model'] ) {
case 'none':
break;
case 'fixed':
$seat_cost = $seats * (float)$settings['pricing_model_settings'];
break;
}
if ( isset($each_seat_cost) ) {
$seat_cost = $seats * $each_seat_cost;
}
// If the price per seat is not a number or negative, bail.
if ( ! is_numeric( $seat_cost ) || $seat_cost < 0 ) {
return $level;
}
switch ( $settings['price_application'] ) {
case 'both':
$level->initial_payment += $seat_cost;
$level->billing_amount += $seat_cost;
// If the level is not already recurring, default to 1 per Month.
if ( empty ( $level->cycle_number ) ) {
$level->cycle_number = 1;
}
if ( empty( $level->cycle_period ) ) {
$level->cycle_period = 'Month';
}
break;
case 'initial':
$level->initial_payment += $seat_cost;
break;
case 'recurring':
$level->billing_amount += $seat_cost;
// If the level is not already recurring, default to 1 per Month.
if ( empty ( $level->cycle_number ) ) {
$level->cycle_number = 1;
}
if ( empty( $level->cycle_period ) ) {
$level->cycle_period = 'Month';
}
break;
}
return $level;
}
/**
* Custom function to add a discount to the checkout page in group accounts
*
*/
function custom_pmprogroupacct_pmpro_checkout_boxes_parent() {
// Get the level being checked out for.
$level = pmpro_getLevelAtCheckout();
//Modify line below as needed
$discount_percentage_per_seat = 0.8;
// Get the group settings for this level.
$settings = null;
if ( ! empty( $level->id ) ) {
$settings = pmprogroupacct_get_settings_for_level( $level->id );
}
// If there are no settings, then this is not a group parent level. Bail.
if ( empty( $settings ) ) {
return;
}
// Build the checkout box.
// We can check if there are a variable amount of seats by checking if min_seats is equal to max_seats.
// The seats option should be a number input defaulting to the minimum seats.
?>
<div class="pmpro_checkout">
<hr />
<h2>
<?php esc_html_e( 'Group Account Information', 'pmpro-group-accounts' ); ?>
</h2>
<div class="pmpro_checkout-fields">
<?php
// Show seats.
if ( $settings['min_seats'] === $settings['max_seats'] ) {
?>
<input type="hidden" name="pmprogroupacct_seats" value="<?php echo esc_attr( $settings['min_seats'] ); ?>" />
<p class="pmpro_checkout-field pmpro_checkout-field-seats">
<?php
$seat_count = (int)$settings['min_seats'];
switch ( $settings['pricing_model'] ) {
case 'none':
/* translators: %d: Number of seats */
printf(
esc_html__(
_n(
'This purchase includes %s additional seat.',
'This purchase includes %s additional seats.',
$seat_count,
'pmpro-group-accounts'
)
),
esc_html( number_format_i18n( $seat_count ) )
);
break;
case 'fixed':
/* translators: %d: Number of seats */
printf(
esc_html__(
_n(
'You are purchasing %s additional seat.',
'You are purchasing %s additional seats.',
$seat_count,
'pmpro-group-accounts'
)
),
esc_html( number_format_i18n( $seat_count ) )
);
break;
case 'variable':
/* translators: %d: Number of seats */
printf(
esc_html__(
_n(
'You are purchasing between %s and %s seats.',
'You are purchasing between %s and %s seats.',
$seat_count,
'pmpro-group-accounts'
)
),
esc_html( number_format_i18n( $seat_count ) ),
esc_html( number_format_i18n( $seat_count ) )
);
break;
}
?>
</p>
<?php
} else {
?>
<div class="pmpro_checkout-field pmpro_checkout-field-seats">
<label for="pmprogroupacct_seats"><?php esc_html_e( 'Number of Seats', 'pmpro-group-accounts' ); ?></label>
<input id="pmprogroupacct_seats" name="pmprogroupacct_seats" type="number" min="<?php echo esc_attr( $settings['min_seats'] ); ?>" max="<?php echo esc_attr( $settings['max_seats'] ); ?>" value="<?php echo esc_attr( $settings['min_seats'] ); ?>" />
<p class="description"><?php printf( esc_html__( 'Choose the number of seats to purchase. You can purchase between %s and %s seats.', 'pmpro-group-accounts' ), esc_html( number_format_i18n( ( (int)$settings['min_seats'] ) ) ), esc_html( number_format_i18n( (int)$settings['max_seats'] ) ) ); ?></p>
</div> <!-- end .pmpro_checkout-field-seats -->
<?php
}
// Show pricing.
switch ( $settings['pricing_model'] ) {
case 'none':
break;
case 'fixed':
?>
<p class="pmpro_checkout-field pmpro_checkout-field-pricing">
<?php
switch ( $settings['price_application'] ) {
case 'both':
printf( esc_html__( 'The price per seat is %s', 'pmpro-group-accounts' ), esc_html( pmpro_formatPrice( $settings['pricing_model_settings'] * $discount_percentage_per_seat ) ) );
break;
case 'initial':
printf( esc_html__( 'You will be charged an additional %s per seat for with initial payment only.', 'pmpro-group-accounts' ), esc_html( pmpro_formatPrice( $settings['pricing_model_settings'] * $discount_percentage_per_seat ) ) );
break;
case 'recurring':
printf( esc_html__( 'You will be charged an additional %s per seat with each recurring payment.', 'pmpro-group-accounts' ), esc_html( pmpro_formatPrice( $settings['pricing_model_settings'] * $discount_percentage_per_seat ) ) );
break;
}
?>
</p>
<?php
break;
}
// Show child levels.
?>
<p class="pmpro_checkout-field pmpro_checkout-field-child-levels">
<?php
$all_levels = pmpro_getAllLevels( true, true );
$child_level_names = array();
foreach ( $all_levels as $child_level ) {
if ( in_array( $child_level->id, $settings['child_level_ids'] ) ) {
$child_level_names[] = $child_level->name;
}
}
echo esc_html( sprintf( _n( 'Group members will be able to claim the %s membership level.', 'Group members will be able to claim the following membership levels: %s', count( $child_level_names ) ,'pmpro-group-accounts' ), implode( ', ', $child_level_names ) ) );
?>
</div>
</div>
<?php
}
//custom function to add a discount to the checkout page in group accounts
function custom_pmpro_applydiscountcode_return_js($discount_code, $discount_code_id, $level_ids, $code_levels) {
//check if the discount code is the one we are looking for
$discount_code_to_match = "4D71A54CD2";
if ($discount_code_to_match == $discount_code) {
$level = pmpro_getLevel( $level_ids );
//bail if the group account add on is not active
if( ! function_exists( 'pmprogroupacct_get_settings_for_level' ) ) {
return;
}
//get the group settings for this level
$settings = pmprogroupacct_get_settings_for_level( $level->id );
//Define here the seat cost.
$seat_cost = 6;
$toReplace = "<p class=\"pmpro_checkout-field pmpro_checkout-field-pricing\">";
switch ( $settings['price_application'] ) {
case 'both':
$toReplace .= "The price per seat is " . pmpro_formatPrice( $seat_cost ) . "</p>";
break;
case 'initial':
$toReplace .= "You will be charged an additional " . pmpro_formatPrice( $seat_cost ) . " per seat for with initial payment only.";
break;
case 'recurring':
$toReplace .= "You will be charged an additional " . pmpro_formatPrice( $seat_cost ) . " per seat with each recurring payment.</p>";
break;
}
?>
jQuery(".pmpro_checkout-field.pmpro_checkout-field-pricing").html('<?php echo $toReplace; ?>');
<?php
}
}
add_action( 'pmpro_applydiscountcode_return_js', 'custom_pmpro_applydiscountcode_return_js', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment