I want to show/hide custom fields on the checkout page based on Product IDs. Therefore, I am using the following code:
function display_custom_checkout_fields_based_on_product( $fields ) {
$product_ids_participating_location = array( 5503 );
$product_ids_toxicology = array( 5453, 5454, 5455 );
$product_ids_artificial_intelligence = array( 5453, 5454, 5455, 5496 );
$current_product_id = 0;
if ( WC()->cart && WC()->cart->get_cart() ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
if ( $product && is_a( $product, 'WC_Product' ) ) {
$current_product_id = $product->get_id();
break;
}
}
}
unset( $fields['billing_participating_location'] );
unset( $fields['billing_toxicology'] );
unset( $fields['billing_artificial_intelligence'] );
if ( in_array( $current_product_id, $product_ids_participating_location ) ) {
$fields['billing_participating_location'] = array(
'label' => 'Participating Location',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true,
);
}
if ( in_array( $current_product_id, $product_ids_toxicology ) ) {
$fields['billing_toxicology'] = array(
'label' => 'Toxicology',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true,
);
}
if ( in_array( $current_product_id, $product_ids_artificial_intelligence ) ) {
$fields['billing_artificial_intelligence'] = array(
'label' => 'Artificial Intelligence',
'required' => true,
'class' => array( 'form-row-wide' ),
'clear' => true,
);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'display_custom_checkout_fields_based_on_product' );
But it's not working as expected.
I want to show each field only for the related product IDs in the defined array.
Any help will be greatly appreciated.
There are some mistakes, unnecessary and missing things in your code:
Use the following revisited code instead:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.