[英]Shipping carrier custom fields validation in Woocommerce checkout page
我通過更改模板/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' , 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' ));
但問題是,即使兩個字段的required
屬性設置為true
,如果按下Place Order
按鈕時字段為空,驗證也不會發生。 理想情況下,訂單不應通過,並應生成錯誤消息。 我已經在functions.php
添加了以下代碼來強制驗證輸入字段,但它沒有做任何事情。 代碼在這里:
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" );
}
}
}
所以,這就是我要找的:
如果您想知道為什么我不首先使用 woocommerce 結帳字段掛鈎來添加輸入字段……我沒有將字段添加到結帳表單中,因此掛鈎沒有任何幫助。 這些字段添加在運輸方式部分。 另一個原因是,我想在每次用戶切換運輸方式時更新required
屬性。 無論出於何種原因,使用 Jquery 更改required
屬性都不起作用,相信我,我嘗試了兩天。 無論如何,那部分已經在工作了。 唯一的問題是讓字段進行驗證並將它們添加到訂單電子郵件和訂單詳細信息中。 我四處看了看,我得到的最接近的幫助是這篇文章,其中 LoicTheAztec 給出了詳細的解決方案
這可以在沒有 jQuery 的情況下使用一個特殊的鈎子來完成,當它被選中時,它將在legacy_local_pickup
運輸方式下方顯示您的兩個“承運人”自定義字段。 如果客戶更改為不同的運輸方式,這些字段將被刪除。
因此,您應該需要先刪除您的自定義模板和所有相關代碼。
現在驗證工作完美,當“運營商”自定義字段被填充時,它們會按訂單元數據保存。
// 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'] ) );
}
此代碼位於活動子主題(或主題)的 functions.php 文件中。 測試和工作。
不顯示:
選擇“本地取件”時顯示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.