I will like to create a custom logical checkout page regarding the fields. I would like to hide fields if the name is 'step' and has a value of '1' or '2'.
That is my code.
function wpb_custom_billing_fields( $fields = array()) {
if($_POST['post_data']){
parse_str( $_POST['post_data'], $post_data );
$_SESSION['post_data'] = $post_data;
};
if( $_SESSION['post_data']["step"] == '1' ) {
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields["billing"]["billing_country"] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_phone'] );
unset( $fields['billing']['billing_email'] );
unset( $fields["billing"]["billing_country"] );
unset($fields['shipping_address_1']);
unset($fields['shipping_address_2']);
unset($fields['shipping_city']);
unset($fields['shipping_state']);
unset($fields['shipping_postcode']);
};
if( $_SESSION['post_data']["step"] == '2' ) {
unset( $fields['billing']['billing_company'] );
unset( $fields['billing']['billing_address_1'] );
unset( $fields['billing']['billing_address_2'] );
unset( $fields['billing']['billing_city'] );
unset( $fields['billing']['billing_postcode'] );
unset( $fields['billing']['billing_country'] );
unset( $fields['billing']['billing_state'] );
unset( $fields['billing']['billing_phone'] );
unset( $fields['billing']['billing_email'] );
unset( $fields['last_name'] );
}
return $fields;
}
add_filter('woocommerce_checkout_fields','wpb_custom_billing_fields');
function sv_unrequire_wc_phone_field( $fields ) {
if( $_SESSION['post_data']["step"] == '1' ) {
$fields['billing_phone']['required'] = false;
}
if( $_SESSION['post_data']["step"] == '2' ) {
$fields['billing_phone']['required'] = false;
}
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field' );
The first function works but second one doesn't work.
Any help is highly appreciated.
Step - 1
Step - 2