Last active
May 23, 2024 10:09
-
-
Save ipokkel/f9118ced4f18a85cd727b716e2da9280 to your computer and use it in GitHub Desktop.
Add a discount code field to a PMPro Shortcode Signup form.
This file contains hidden or 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 creates a discount code field for a specific level | |
* when using the Signup Shortcode for membership registration. | |
* | |
* This can be helpful when a level requires a discount code for registration, or | |
* if the Signup Shortcode level is a paid level and you have a discount code that | |
* makes this level free. | |
* | |
* This recipe assumes the following: | |
* | |
* 1. The level for the Signup Shortcode is level 2. You should change this to your | |
* level's ID in the 'levels' option for the field, e.g. 'levels' => array( 3 ), | |
* 2. The Signup Shortcode is configured to show custom fields. | |
* e.g. [pmpro_signup level="2" custom_fields="true"] | |
* | |
* 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_discount_code_for_signup_shortcode() { | |
// Don't break if PMPro is out of date or not loaded. | |
if ( ! function_exists( 'pmpro_add_user_field' ) || pmpro_is_checkout() || ! function_exists( 'pmprosus_signup_shortcode' ) ) { | |
return; | |
} | |
// define the fields | |
$fields = array(); | |
$fields[] = new PMPro_Field( | |
'discount_code', // input field name, used as meta key | |
'text', // field type | |
array( | |
'id' => 'signup-discount-code', | |
'label' => 'Discount Code', // display custom label, if not used field name will be used | |
'html_attributes' => array( 'required' => 'required' ), | |
'levels' => array( 2 ), // levels to display field for | |
'class' => 'discount_code', // custom class for input field | |
'memberslistcsv' => true, // include when using export members to csv | |
'addmember' => true, // include when using add member from admin | |
'required' => true, // make field required | |
) | |
); | |
foreach ( $fields as $field ) { | |
pmpro_add_user_field( | |
'signup-shortcode', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
unset( $field ); | |
// that's it. see the PMPro Register Helper readme for more information and examples. | |
} | |
add_action( 'init', 'my_pmpro_discount_code_for_signup_shortcode' ); | |
// Remove signup-shortcode location when we're on the checkout page. | |
function remove_signup_shortcode_discount_code_field_where( $where, $field ) { | |
if ( 'signup-shortcode' === $where && 'discount_code' === $field->name && pmpro_is_checkout() ) { | |
return false; | |
} | |
return $where; | |
} | |
add_filter( 'pmpro_add_user_field_where', 'remove_signup_shortcode_discount_code_field_where', 10, 2 ); | |
function my_pmpro_extend_is_checkout( $is_checkout ) { | |
global $pmpro_pages; | |
if ( $is_checkout ) { | |
return $is_checkout; | |
} | |
if ( ! $is_checkout && ! empty( $pmpro_pages['checkout'] ) ) { | |
// Get the current page slug | |
$checkout_page_slug = get_post_field( 'post_name', $pmpro_pages['checkout'] ); | |
// If the URL contains the checkout page slug, set $is_checkout to true | |
if ( ! empty( $checkout_page_slug ) && strpos( $_SERVER['REQUEST_URI'], $checkout_page_slug ) !== false ) { | |
$is_checkout = true; | |
} | |
} | |
return $is_checkout; | |
} | |
add_filter( 'pmpro_is_checkout', 'my_pmpro_extend_is_checkout' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment