Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwanjuki/26c48b0fa79c2caa19912e4287af42f6 to your computer and use it in GitHub Desktop.

Select an option

Save dwanjuki/26c48b0fa79c2caa19912e4287af42f6 to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe will take preference to discount codes. If a discount code is applied
* and is free, the variable price field will be removed.
*
* 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/
*/
function my_pmprovp_pmpro_checkout_level( $level ) {
// Get variable pricing info.
remove_filter( 'pmpro_checkout_level', 'pmprovp_pmpro_checkout_level' );
if( !empty( $_REQUEST['discount_code'] ) ){
$discount_code = new PMPro_Discount_Code( $_REQUEST['discount_code'] );
if( !empty( $discount_code ) ){
if( isset( $discount_code->levels[$level->id] ) ){
if(
$discount_code->levels[$level->id]['initial_payment'] == 0 ||
$discount_code->levels[$level->id]['billing_amount'] == 0
){
remove_action( 'pmpro_checkout_after_level_cost', 'pmprovp_pmpro_checkout_after_level_cost' );
}
}
}
return $level;
}
if ( isset( $_REQUEST['price'] ) ) {
$price = preg_replace( '[^0-9\.\,]', '', $_REQUEST['price'] );
}
$vpfields = pmprovp_get_settings( $level->id );
// Make sure level has variable pricing.
if ( empty( $vpfields ) || empty( $vpfields['variable_pricing'] ) ) {
return $level;
}
if ( isset( $price ) ) {
$level->initial_payment = $price;
if ( $level->billing_amount > 0 ) {
$level->billing_amount = $price;
}
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmprovp_pmpro_checkout_level' );
/**
* Handle things when the Apply Discount button is clicked on
*/
function my_pmprovp_pmpro_after_discount_code_applied( $discount_code, $discount_code_id, $level_id ){
if( !empty( $discount_code ) ){
$discount_code_obj = new PMPro_Discount_Code( $discount_code );
if( !empty( $discount_code_obj ) ){
if( isset( $discount_code_obj->levels[$level_id] ) ){
if(
$discount_code_obj->levels[$level_id]['initial_payment'] == 0 ||
$discount_code_obj->levels[$level_id]['billing_amount'] == 0
){
echo "jQuery('#pmprovp_fields').remove();";
}
}
}
}
}
add_action( 'pmpro_applydiscountcode_return_js', 'my_pmprovp_pmpro_after_discount_code_applied', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment