gpt4 book ai didi

php - WooCommerce 结帐自定义字段条件验证

转载 作者:行者123 更新时间:2023-12-04 03:35:45 27 4
gpt4 key购买 nike

我想将我的自定义字段保存到新用户元数据中。我的自定义字段 = T.C.金利克没有

代码行开头的函数用于将字段添加到成员(member)部分。

我主要想做的是当涉及到注册用户的结帐页面时,自动拉取我在顶部控制的自定义区域

/** TC Kimlik No Ekleme **/

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
$fields['billing']['shipping_tc'] = array(
'label' => __('TC Kimlik No', 'woocommerce'),
'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);

return $fields;
}
/** TC Doğrula **/
function isTcKimlik($tc){
if(strlen($tc) < 11){ return false; }
if($tc[0] == '0'){ return false; }
$plus = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
$minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
$mod = $minus % 10;
if($mod != $tc[9]){ return false; }
$all = '';
for($i = 0 ; $i < 10 ; $i++){ $all += $tc[$i]; }
if($all % 10 != $tc[10]){ return false; }

return true;
}
/** TC Kimlik Noyu Doğrula **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
$tcno = $_POST['shipping_tc'];
if(!isTcKimlik($tcno))
wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
}
/** Admin Sipariş Detayında Fatura Bilgilerinde TC No'yu Görebilmesi İçin**/

add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('TC Kimlik No').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_tc', true ) . '</p>';
}
add_filter( 'woocommerce_checkout_fields', 'misha_email_first' );

function misha_email_first( $checkout_fields ) {
$checkout_fields['billing']['shipping_tc']['priority'] = 20;
return $checkout_fields;
}





// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
$checkout = WC()->checkout;
foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
if($key!='billing_email')
woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
$address = $_POST;
foreach ($address as $key => $field){
// Only billing fields values
if( strpos( $key, 'billing_' ) !== false ){
// Condition to add firstname and last name to user meta table
if($key == 'billing_first_name' || $key == 'billing_last_name'){
$new_key = str_replace( 'billing_', '', $key );
update_user_meta( $user_id, $new_key, $_POST[$key] );
}
update_user_meta( $user_id, $key, $_POST[$key] );
}
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
foreach ($_POST as $key => $field) :
// Validation: Required fields
if( strpos( $key, 'billing_' ) !== false ){
if($key == 'billing_country' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Lütfen ülke seçimi yapınız.', 'woocommerce' ));
}
if($key == 'billing_first_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Adınızı giriniz.', 'woocommerce' ) );
}
if($key == 'billing_last_name' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Soyadınızı giriniz.', 'woocommerce' ) );
}
if($key == 'billing_address_1' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Adresinizi giriniz.', 'woocommerce' ) );
}
if($key == 'billing_city' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Lütfen şehir seçimi yapınız.', 'woocommerce' ) );
}
if($key == 'billing_state' && empty($field) ){
if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
$validation_errors->add( $key.'_error', __( 'Mahalle girişi yapınız.', 'woocommerce' ) );
}
if($key == 'billing_postcode' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Posta kodu girişi yapınız.', 'woocommerce' ) );
}
/*
if($key == 'billing_email' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
}
*/
if($key == 'billing_phone' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'Lütfen telefon numaranızı giriniz.', 'woocommerce' ) );
}
if($key == 'shipping_tc' && empty($field) ){
$validation_errors->add( $key.'_error', __( 'TC.', 'woocommerce' ) );
}


}
endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
$fields['billing_phone']['required'] = true;
$fields['billing_city']['required'] = true;
$fields['billing_country']['required'] = true;
$fields['billing_address_1']['required'] = true;
return $fields;
}

最佳答案

根据您的其他问题回答 Save WooCommerce checkout custom field as user meta data ,即解决将字段数据保存为用户元数据和订单元数据。

现在要进行字段验证,您将使用:

function is_billing_identifier_valid( $tc ){
if (strlen($tc) < 11 || $tc[0] == '0')
return false;

$plus = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
$minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
$all = '';

if ( $minus % 10 != $tc[9])
return false;

for ($i = 0 ; $i < 10 ; $i++)
$all += $tc[$i];

return $all % 10 != $tc[10] ? false : true;
}

add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation() {
if( isset($_POST['billing_identifier']) && ! is_billing_identifier_valid( esc_attr($_POST['billing_identifier']) ) ) {
wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
}
}

代码进入事件子主题(或事件主题)的 functions.php 文件。它应该有效。

关于php - WooCommerce 结帐自定义字段条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66968303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com