Created
November 13, 2020 04:16
-
-
Save DeveloperWil/e41ae77d8d400d1bad817895cd0e4b11 to your computer and use it in GitHub Desktop.
WooCommerce: Add Privacy Policy checkbox to checkout form
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
/** | |
* Add a privacy policy checkbox on the checkout form | |
* | |
* @author Wil Brown zeropointdevelopment.com | |
*/ | |
function zpd_add_checkout_privacy_policy() { | |
woocommerce_form_field( 'privacy_policy', array( | |
'type' => 'checkbox', | |
'class' => array('form-row privacy'), | |
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'), | |
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'), | |
'required' => true, | |
'label' => 'I have read and agree with the <a href="' . get_privacy_policy_url() . '">Privacy Policy</a>', | |
)); | |
} | |
add_action( 'woocommerce_review_order_before_submit', 'zpd_add_checkout_privacy_policy', 9 ); | |
// Show notice if customer doesn't check the box (required) | |
/** | |
* Show notice if customer doesn't check the new privacy policy checkbox | |
* | |
* @author Wil Brown zeropointdevelopment.com | |
*/ | |
function zpd_privacy_not_agreed() { | |
if ( ! (int) isset( $_POST['privacy_policy'] ) ) { | |
wc_add_notice( __( 'Please read and accept the <strong>privacy policy</strong> to proceed with your order.' ), 'error' ); | |
} | |
} | |
add_action( 'woocommerce_checkout_process', 'zpd_privacy_not_agreed' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment