如果产品有特定类别,我有这些 WooCommerce 字段会添加到结账页面,但现在我设置了两个“required = true”,但它不起作用?
这就是我制作字段的方式:
<?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;
}
?>
您引用了不同的函数名称。
该函数名为
add_youtube_short_checkout_fields
,但钩子引用 add_video_short_checkout_fields
。
<?php
add_action( 'woocommerce_before_order_notes', 'add_youtube_short_checkout_fields', 1000, 1 );
function add_youtube_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;
}
?>
你的代码有错误,而且你的问题不是很清楚。对于自定义结帐必填字段,您需要添加验证过程。
尝试以下重新访问的代码:
add_action( 'woocommerce_before_order_notes', 'add_video_short_checkout_fields', 20, 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_name = $cart_item['data']->get_name();
// Check that 'my_custom_field_1' is set as custom cart item data
if( isset($cart_item['my_custom_field_1']) ) {
continue; // Jump to next cart item
}
// Restrict to 'video-short' product category cart items
if ( has_term( 'video-short', 'product_cat', $cart_item['product_id'] ) ) {
$current_date = date('d-m-Y', strtotime('+72 hours'));
$hours_to_add = 24; // what is that for?
// Add a form field for the current product
echo '<div class="video-short-fields group_extra_info">
<h3>' . esc_html( $product_name ) . '</h3>
<div class="video-short-fields form-row-group">';
woocommerce_form_field( 'custom_field_1_' . $cart_item_key, 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; // what is that for?
echo '</div></div>';
}
}
}
自定义结账必填字段的字段验证:
// Field validation for custom checkout required fields
add_filter( 'woocommerce_checkout_process', 'custom_required_checkout_fields_validation' );
function disable_coupon_field_for_specific_products() {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
if( isset($_POST['custom_field_1_'.$item_key]) && empty($_POST['custom_field_1_'.$item_key]) ) {
wc_add_notice( __('"Video Title Short" is a required field.', 'woocommerce' ), 'error' );
}
}
}
感谢@LoicTheAztec,我找到了解决方案。
我只需要更改函数名称。
// Field validation for custom checkout required fields
add_filter( 'woocommerce_checkout_process', 'custom_required_checkout_fields_validation' );
function custom_required_checkout_fields_validation() {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
if( isset($_POST['custom_field_1_'.$item_key]) && empty($_POST['custom_field_1_'.$item_key]) ) {
wc_add_notice( __('"Video Title Short" is a required field.', 'woocommerce' ), 'error' );
}
}
}