mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/78707475/add-a-returns-and-refunds-policy-checkbox-with-a-link-below-woocommerce-terms-an
25 lines
1.4 KiB
Text
25 lines
1.4 KiB
Text
// Add a custom checkbox to checkout
|
|
add_action( 'woocommerce_review_order_before_submit', 'add_custom_checkout_privacy_policy', 9 );
|
|
function add_custom_checkout_privacy_policy() {
|
|
// Here define the privacy policy link page URL
|
|
$page_link = site_url('/privacy-policy/');
|
|
|
|
$privacy_text = sprintf( __("I have read and agree to the website %s", 'woocommerce'),
|
|
'<a href="'. $page_link .'">'. __("returns and refunds policy", 'woocommerce') . '</a>');
|
|
|
|
printf('<p class="form-row privacy validate-required privacy_policy_field">
|
|
<label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
|
|
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="privacy_policy" id="privacy_policy" %s />
|
|
<span class="woocommerce-terms-and-conditions-checkbox-text">%s</span>
|
|
<abbr class="required" title="%s">*</abbr>
|
|
</label></p>', checked( isset($_POST['privacy_policy']), true, false ),
|
|
$privacy_text, esc_attr__( 'required', 'woocommerce' ) );
|
|
}
|
|
|
|
// Checkbox validation as it's a required field
|
|
add_action( 'woocommerce_checkout_process', 'validate_custom_checkout_privacy_policy' );
|
|
function validate_custom_checkout_privacy_policy() {
|
|
if ( ! isset($_POST['privacy_policy']) ) {
|
|
wc_add_notice('Please read and accept the returns and refunds policy to proceed with your order.', 'error');
|
|
}
|
|
}
|