久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Woocommerce 結帳頁面中的運輸承運人自定義字段驗

Shipping carrier custom fields validation in Woocommerce checkout page(Woocommerce 結帳頁面中的運輸承運人自定義字段驗證)
本文介紹了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 = '真';}別的 {$cheq = '假';}woocommerce_form_field( 'fieldtester1' , array('類型' =>'文本','類' =>數組('wccs-field-class wccs-form-row-wide blahblah1'),'標簽' =>'輸入您的運營商名稱','必需' =>$支票,'占位符' =>'運營商名稱',), $checkout->get_value('fieldtester1'));woocommerce_form_field( 'fieldtester2' , array('類型' =>'文本','類' =>數組('wccs-field-class wccs-form-row-wide blahblah2'),'標簽' =>'輸入您的運營商帳戶#','必需' =>$支票,'占位符' =>'承運人號碼',), $checkout->get_value('fieldtester2'));

但這里的問題是,即使 required 屬性設置為 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 ) {如果(空($_POST['fieldtester1'])){wc_add_notice( ("請不要忘記輸入您的承運人詳細信息."), "錯誤");}}}

所以,這就是我要找的:

  1. 誰能幫我強制驗證我的兩個輸入字段添加了嗎?
  2. 驗證后,我還想在新訂單電子郵件中添加這兩個字段,并在 Wordpress 儀表板中添加訂單詳細信息.

如果您想知道為什么我不首先使用 woocommerce 結帳字段掛鉤來添加輸入字段...我沒有將字段添加到結帳表單中,因此掛鉤沒有任何幫助.這些字段添加在運輸方式部分.另一個原因是,我想在每次用戶切換運輸方式時更新 required 屬性.使用 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 functions.php file of your active child theme (or theme). Tested and works.

Not displayed:

Displayed when "Local Pickup" is selected:

這篇關于Woocommerce 結帳頁面中的運輸承運人自定義字段驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: av夜夜操| 国产小视频在线 | 综合精品| 欧美xxxx黑人又粗又长 | 视频在线一区 | 成人在线观看免费 | 无人区国产成人久久三区 | 最新中文字幕在线 | www.久久精品视频 | 国产视频欧美 | 国产免费人成xvideos视频 | 中文字幕在线观看一区二区 | 国产精品一区二区在线播放 | 亚洲一区国产精品 | 四季久久免费一区二区三区四区 | 人操人免费视频 | 人人玩人人添人人澡欧美 | 男人天堂色 | 欧美一区二区三区 | 国产精品欧美一区二区三区 | 国产成人精品久久二区二区91 | 青青草原综合久久大伊人精品 | 免费在线观看成年人视频 | 成人超碰在线 | 国产视频在线观看一区二区三区 | 丝袜天堂 | 男人天堂免费在线 | 欧美午夜一区二区三区免费大片 | 午夜看看 | 婷婷久久久久 | 久久99精品久久久久久国产越南 | 激情五月综合网 | 天天操网 | 一区二区三区在线观看视频 | 欧美一区二区综合 | 一级片av| 手机av免费在线 | 国产综合久久久久久鬼色 | 青青草视频免费观看 | 免费能直接在线观看黄的视频 | 亚洲成人在线视频播放 |