Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active February 4, 2025 06:25
Show Gist options
  • Save JarrydLong/0f952dae2c804daaf6e3a837095db5b0 to your computer and use it in GitHub Desktop.
Save JarrydLong/0f952dae2c804daaf6e3a837095db5b0 to your computer and use it in GitHub Desktop.
<?php
/**
* This recipe will hide the discount code field on the checkout page if no discount codes
* have been created for that specific level.
*
* 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 mypmpro_show_discount_code_field_if_available() {
global $wpdb, $pmpro_show_discount_code;
if ( ! function_exists( 'pmpro_getLevelAtCheckout' ) || ! function_exists( 'pmpro_is_checkout' ) ) {
return;
}
if ( pmpro_is_checkout() ) {
$level = pmpro_getLevelAtCheckout();
if ( ! empty( $level ) ) {
$level_id = $level->id;
$sql = $wpdb->prepare(
"SELECT c.id
FROM {$wpdb->pmpro_discount_codes} c
LEFT JOIN {$wpdb->pmpro_discount_codes_levels} cl
ON cl.level_id = c.id
WHERE cl.level_id = %d",
$level_id
);
$results = $wpdb->get_results( $sql );
if ( ! $results ) {
$pmpro_show_discount_code = false;
}
}
}
}
add_action( 'wp', 'mypmpro_show_discount_code_field_if_available' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment