Woocommerce结帐页面中的运输承运商自定义字段验证 [英] Shipping carrier custom fields validation in Woocommerce checkout page

查看:136
本文介绍了Woocommerce结帐页面中的运输承运商自定义字段验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发货方法部分添加了两个自定义输入字段,方法是更改​​模板 /genesis-sample/woocommerce/checkout/review-order.php
我还设法有条件地要求它们。仅当选中特定单选按钮时,输入字段才会出现并成为必需。我使用jQuery代码使字段显示和消失。所有这一切都很好。代码在这里:

  if(isset($ _ POST ['shipping_method_0_legacy_local_pickup'])&& $ _POST ['shipping_method_0_legacy_local_pickup '] == 1){
$ cheq ='true';
}
else {
$ cheq ='false';
}
woocommerce_form_field('fieldtester1',数组(
'类型'=>'text',
'class'=>数组('wccs-field-class wccs -form-row-wide blahblah1'),
'label'=>'输入您的运营商名称',
'必需'=> $ cheq,
'占位符'=> '运营商名称',
),$ checkout-> get_value('fieldtester1'));
woocommerce_form_field('fieldtester2',数组(
'类型'=>'text',
'class'=>数组('wccs-field-class wccs-form-row-广泛blahblah2'),
'标签'=>'输入您的运营商帐号#',
'必需'=> $ cheq,
'占位符'=>'运营商号码' ,
),$ checkout-> get_value('fieldtester2'));

但这就是问题,即使需要 对于这两个字段,>属性设置为 true ,如果下订单按钮。理想情况下,订单不应该通过,应该生成错误消息。我已经在 functions.php 中添加了以下代码来强制验证输入字段,但它没有做任何事情。代码在这里:

  add_action('woocommerce_checkout_process','carrier_checkout_process'); 
函数carrier_checkout_process(){
if(isset($ _ POST ['shipping_method_0_legacy_local_pickup'])&& $ _POST ['shipping_method_0_legacy_local_pickup'] == 1){
if(empty($) _POST ['fieldtester1'])){
wc_add_notice((请不要忘记输入您的运输公司详细信息。),错误);
}
}
}

所以,这就是我的意思寻找:


  1. 任何人都可以帮我强制验证两个输入字段I
    已添加?

  2. 验证后,我还想在新订单电子邮件中添加这两个字段,并在Wordpress仪表板中订购详细信息。

如果您想知道我为什么不使用woocommerce checkout字段挂钩来添加输入字段......我我没有将字段添加到结帐表单,因此挂钩没有任何帮助。这些字段将添加到送货方法部分。另一个原因是,每次用户切换发货方法时,我都想更新必需的属性。使用Jquery更改 required 属性无论出于何种原因都无法正常工作,请相信我,我试了两天。无论如何,那部分已经在运作。唯一的问题是让字段进行验证并将其添加到订单电子邮件和订单详细信息中。我到处都看了看,我得到的最近的帮助就是这篇文章



选择本地代答时显示:




I have added two custom input fields in the shipping method section by changing the template /genesis-sample/woocommerce/checkout/review-order.php I have also managed to get them conditionally required. Only when the specific radio button is checked, the input fields appear and become required. I am using jQuery code to make the fields appear and disappear. All of this is working fine. The code is here:

if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
    $cheq = 'true';
}
else {
    $cheq = 'false';
}
woocommerce_form_field( 'fieldtester1' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah1'), 
    'label'         => 'Enter Your Carrier Name',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Name',
    ), $checkout->get_value( 'fieldtester1' )); 
woocommerce_form_field( 'fieldtester2' , array(
    'type'          => 'text',
    'class'         => array('wccs-field-class wccs-form-row-wide blahblah2'), 
    'label'         => 'Enter Your Carrier Account #',
    'required'  => $cheq,
    'placeholder'       => 'Carrier Number',
    ), $checkout->get_value( 'fieldtester2' )); 

But here's the problem, even with the required attribute set as true for the two fields, the validation doesn't happen if the field is empty when the Place Order button is pressed. Ideally, the order should not go through and an error message should be generated. I have already added following code in functions.php to force validation of the input field but it doesn't do a thing. The code is here:

add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if ( isset($_POST['shipping_method_0_legacy_local_pickup']) && $_POST['shipping_method_0_legacy_local_pickup'] == 1 ) {
        if( empty( $_POST['fieldtester1'] ) ) {
            wc_add_notice( ( "Please don't forget to enter your shipping carrier details." ), "error" );
        }
    }
}

So, here's what I am looking for:

  1. can anyone help me in forcing the validation for two input fields I have added?
  2. After the validation, I would also like to add these two fields in the new order email, and order details in the Wordpress dashboard.

If you are wondering why I am not using a woocommerce checkout field hook to add the input fields in the first place...I am not adding the fields to the checkout form so the hooks aren't of any help. The fields are added in the shipping method section. Another reason was, I wanted to update the required attribute every time a user would switch the shipping method. Using Jquery to change the required attribute wasn't working for whatever reason, believe me I tried for two days. Anyways, that part is already working. The only issues are getting the fields to validate and add them to the order emails and order details. I have looked around everywhere and the closest help I got was this post where LoicTheAztec gave a detailed solution

解决方案

This can be done without jQuery using a special hook that will display your two "Carrier" custom fields below legacy_local_pickup shipping method when it's selected. If customer change to a different shipping method, those fields will be removed.

So you should need to remove your customized template and all related code before.

Now the validation works perfectly and when "Carrier" custom fields are filled, they are saved in order meta data.

// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
    if( ! is_checkout()) return; // Only on checkout page

    $customer_carrier_method = 'legacy_local_pickup';

    if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"

    $chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];

    // If the chosen shipping method is 'legacy_local_pickup' we display
    if($chosen_method_id == $customer_carrier_method ):

    echo '<div class="custom-carrier">';

    woocommerce_form_field( 'carrier_name' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-name'),
        'label'         => 'Carrier Information:',
        'required'      => true,
        'placeholder'   => 'Carrier Name',
    ), WC()->checkout->get_value( 'carrier_name' ));

    woocommerce_form_field( 'carrier_number' , array(
        'type'          => 'text',
        'class'         => array('form-row-wide carrier-number'),
        'required'      => true,
        'placeholder'   => 'Carrier Number',
    ), WC()->checkout->get_value( 'carrier_number' ));

    echo '</div>';
    endif;
}

// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
    if( isset( $_POST['carrier_name'] ) && empty( $_POST['carrier_name'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier name." ), "error" );

    if( isset( $_POST['carrier_number'] ) && empty( $_POST['carrier_number'] ) )
        wc_add_notice( ( "Please don't forget to enter the shipping carrier account number." ), "error" );
}

// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
    if( isset( $_POST['carrier_name'] ))
        update_post_meta( $order_id, '_carrier_name', sanitize_text_field( $_POST['carrier_name'] ) );

    if( isset( $_POST['carrier_number'] ))
        update_post_meta( $order_id, '_carrier_number', sanitize_text_field( $_POST['carrier_number'] ) );
}

This code goes on function.php file of your active child theme (or theme). Tested and works.

Not displayed:

Displayed when "Local Pickup" is selected:

这篇关于Woocommerce结帐页面中的运输承运商自定义字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆