Conditionally redirect to specific page using checkbox value

I added a custom checkbox in WooCommerce checkout page to redirect visitors to a specific page if the checkbox was checked.

Checkbox in checkout page:

woocommerce_form_field( 'checkbox_name', array(
    'type'          => 'checkbox',
    'class'         => array('checkbox_class form-row-wide'),
    'label'         => __('Yes'),
    ), $checkout->get_value( 'checkbox_name'));

Redirection Hook:

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' ); 
function wc_custom_redirect_after_purchase() {
   global $wp;
   if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
     if ( isset( $_POST['checkbox_name'] ) && $_POST['checkbox_name'] == 1 ) 
        wp_redirect( 'https://website-domain.com/page1' );
     else 
        wp_redirect( 'https://website-domain.com/page2' );
   }
}

Read more here: Conditionally redirect to specific page using checkbox value

Leave a Reply

Your email address will not be published. Required fields are marked *