【发布时间】:2018-08-07 07:39:06
【问题描述】:
我有一个复选框,如果选中它会打开一个下拉字段(要求)。如果您尝试发送 Woocommerce 订单,它会有条件地检查该字段是否有内容,如果没有则返回错误。
当需求字段确实有信息输入时,这一切都可以接受,它仍然将其视为没有内容并返回错误。
由于这是表单应该工作的基本方式,我不明白为什么我会得到这样的结果。代码如下:
/**
* Add a message field to the WC checkout
*/
add_action( 'woocommerce_before_checkout_form', 'custom_checkout_field' );
function custom_checkout_field( $checkout ) {
echo '<div id="message"><h3>' . __( '<i class="fas fa-envelope"></i> Message' ) . '</h3><p style="margin: 0 0 8px;">Would you like to leave a message?</p>';
woocommerce_form_field( 'checkbox', array(
'type' => 'checkbox',
'class' => array( 'msg-checkbox' ),
'label' => __( 'Yes' ),
), $checkout->get_value( 'checkbox' ) );
woocommerce_form_field( 'requirements', array(
'type' => 'text',
'class' => array('msg'),
'label' => __('Please input your message.'),
'placeholder' => __(''),
), $checkout->get_value( 'requirements' ));
echo '</div>';
}
/**
* Process checkout with additional custom field
*/
add_action('woocommerce_checkout_process', 'custom_checkout_field_process');
function custom_checkout_field_process() {
// Check if set, if not add an error.
if(isset($_POST['checkbox']) && ( empty($_POST['requirements'])));
wc_add_notice( __( 'Please let us know what you would like to do' ), 'error' );
}
我希望这是有道理的。基本上它可以工作到一定程度,但是输入字段的验证在它有内容时不起作用。
谢谢。
【问题讨论】:
-
谢谢。你是对的。不清楚。
标签: php jquery wordpress woocommerce checkout