Forked from JarrydLong/mypmpro-hide-discount-field-not-available.php
Last active
February 4, 2025 06:19
-
-
Save ipokkel/06d190b6a68343a3b253afd24a0500dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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