Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Last active November 28, 2024 16:01
Show Gist options
  • Save ipokkel/047dd85711def88df572f3cbea422050 to your computer and use it in GitHub Desktop.
Save ipokkel/047dd85711def88df572f3cbea422050 to your computer and use it in GitHub Desktop.
Allow users to make the billing fields optional for the PMPro checkout page when they check a checkbox.
<?php
/**
* Make billing fields optional at checkout
*
* 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/
*/
// Add a checkbox to make billing fields optional
function add_optional_billing_checkbox() {
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
$level = pmpro_getLevelAtCheckout();
if ( empty( $level ) || ! empty( $level->initial_payment ) || ! empty( $level->billing_amount ) ) {
?>
<div class="pmpro_checkout-field pmpro_checkout-field-optional-billing">
<label>
<input type="checkbox" id="optional_billing" name="optional_billing" value="1">
<?php _e( 'Make billing fields optional', 'paid-memberships-pro' ); ?>
</label>
</div>
<?php
}
}
add_action( 'pmpro_checkout_boxes', 'add_optional_billing_checkbox' );
// Make billing fields optional when checkbox is checked
function make_billing_fields_optional() {
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
if ( ! empty( $_REQUEST['optional_billing'] ) ) {
add_filter( 'pmpro_required_billing_fields', '__return_empty_array' );
}
}
add_action( 'pmpro_checkout_preheader', 'make_billing_fields_optional' );
// Add JavaScript to handle the checkbox behavior
function optional_billing_js() {
if ( ! defined( 'PMPRO_VERSION' ) ) {
return;
}
$level = pmpro_getLevelAtCheckout();
if ( empty( $level ) || ! empty( $level->initial_payment ) || ! empty( $level->billing_amount ) ) {
?>
<script>
jQuery(document).ready(function() {
// Move the optional billing checkbox to the billing section
var optionalBillingCheckbox = jQuery('.pmpro_checkout-field-optional-billing');
var billingFieldsDiv = jQuery('.pmpro_form_fields.pmpro_cols-2').first();
if (optionalBillingCheckbox.length && billingFieldsDiv.length) {
optionalBillingCheckbox.insertBefore(billingFieldsDiv);
}
// Handle checkbox change event
jQuery('#optional_billing').change(function() {
var billingFields = jQuery('#pmpro_billing_address_fields');
if (jQuery(this).is(':checked')) {
// Hide required indicators and make fields optional
billingFields.find('.pmpro_asterisk').hide();
billingFields.find('input, select').prop('required', false);
} else {
// Show required indicators and make fields required
billingFields.find('.pmpro_asterisk').show();
billingFields.find('input, select').prop('required', true);
}
});
});
</script>
<?php
}
}
add_action( 'wp_footer', 'optional_billing_js' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment