Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/c38901e780aa390d11688c3e69b9bd6f to your computer and use it in GitHub Desktop.
Save dwanjuki/c38901e780aa390d11688c3e69b9bd6f to your computer and use it in GitHub Desktop.
Show the discount code field on checkout for specified levels, hide on others
<?php
/**
* Show the discount code field on checkout for specific levels only
*
* 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_pmpro_show_hide_discount_code() {
global $pmpro_show_discount_code;
if ( ! $pmpro_show_discount_code || ! function_exists( 'pmpro_getLevelAtCheckout' ) || ! function_exists( 'pmpro_is_checkout' ) ) {
return;
}
if ( pmpro_is_checkout() ) {
$level = pmpro_getLevelAtCheckout();
if ( ! empty( $level ) ) {
$level_id = $level->id;
if ( ! in_array( $level_id , array( 2, 3 ) ) ) { // show discount code field for level 2 and 3 only
$pmpro_show_discount_code = false;
}
}
}
}
add_action( 'wp', 'my_pmpro_show_hide_discount_code' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment