Woocommerce结帐中的电子邮件字段比较验证

时间:2018-11-18 13:38:00

标签: php wordpress validation woocommerce checkout

我有一个名为gift的自定义字段,现在我正在尝试针对billing_email字段进行验证。换句话说,如果客户填写礼物字段,并且该电子邮件与帐单电子邮件相同,则在单击“完成订单”按钮时应该显示错误。

它不起作用。有什么想法吗?这是代码。

// verify that billing email and gift email are not the same
function billing_email_and_gift_validation( $posted ) {
$checkout = WC()->checkout;
if ( strcmp( $posted['billing_email'] == $posted['gift'] )) {
wc_add_notice( __( 'To send as gift, you cannot send it to yourself. That\'s the point of a gift, is it not?', 'woocommerce' ), 'error' ); } }
add_action( 'woocommerce_after_checkout_validation', 'billing_email_and_gift_validation', 10, 2 );

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

我已经将此验证与您之前做出的问题代码中的礼物验证进行了合并,因此您将在实际代码中将其删除,因为此函数可以处理这两个问题:

add_action('woocommerce_after_checkout_validation', 'gift_custom_field_validation', 20, 2 );
function gift_custom_field_validation( $data, $errors ) {
    // Validating that "Gift" is not empty when selected
    if( isset($_POST['gift_msg']) && isset($_POST['gift']) && empty($_POST['gift']) )
        $errors->add( 'gift', __( "You've chosen to send this as a gift, but did not submit a recipient email address.", "woocommerce" ) );

    // Validating that "Gift" imputed email is different from billing email
    if ( isset($_POST['gift']) && $data['billing_email'] === $_POST['gift'] ) {
        $errors->add( 'gift', __( "To send as gift, you cannot send it to yourself. That's the point of a gift, isn't it?", "woocommerce" ) );
    }
}

代码进入您的活动子主题(活动主题)的function.php文件中。经过测试,可以正常工作。

相关问题