So I have these WooCommerce fields that get added to the checkout page if a product has a specific category, but now I have set the two ‘required = true’, but it doesn’t work?
This is how I made the fields:
<?php
add_action( 'woocommerce_before_order_notes', 'add_video_short_checkout_fields', 1000, 1 );
function add_video_short_checkout_fields( $checkout ) {
date_default_timezone_set('Europe/Amsterdam');
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( has_term( 'video-short', 'product_cat', $product->get_id() ) ) {
$product_title = $product->get_name();
$product_id = $product->get_id();
$unique_id = $cart_item_key;
$current_date = date('d-m-Y', strtotime('+72 hours'));
$hours_to_add = 24;
// Add custom fields for the product
echo '<div class="video-short-fields group_extra_info">';
echo '<h3>' . esc_html( $product_title ) . '</h3>';
echo '<div class="video-short-fields form-row-group">';
woocommerce_form_field( 'custom_field_1_' . $unique_id, array(
'type' => 'text',
'class' => array( 'video_veld customfield_start form-row-wide' ),
'label' => __( 'Video Title Short', 'woocommerce' ),
'placeholder' => __( 'Your title', 'woocommerce' ),
'required' => true,
), $cart_item['my_custom_field_1'] );
$hours_to_add += 24;
}
?>
2
Answers
You’re referencing different functions names.
The function is named
add_youtube_short_checkout_fields
, but the hook referencesadd_video_short_checkout_fields
.There are mistakes in your code, and your question is not very clear. For custom checkout required fields, you need to add a validation process.
Try the following revisited code:
The field validation for custom checkout required fields: