我正在关注下面的链接以自定义结帐字段。
Show or hide checkout fields based on shipping method in Woocommerce 3
但是,我遇到了有关错误消息和新标题字段的问题。
尽管添加了条件检查字段的错误消息代码,但仍显示了一些重复的错误消息。我不知道它们来自哪里。
添加了两个自定义标题。但是,它不能被JS隐藏。我不确定问题出在哪里。
有人可以给我一些建议吗?我不确定在这里问是否可以。如果有必要提出一个新问题,请告诉我。
//Checkout page
# 1. Customize checkout fields
# 1-1. Billing information
add_filter( 'woocommerce_billing_fields', 'customizing_billing_fields' );
function customizing_billing_fields($fields){
# 1-1-1. remove fields
unset($fields['billing_last_name']);
unset($fields['billing_company']);
unset($fields['billing_address_1']);
unset($fields['billing_address_2']);
unset($fields['billing_city']);
unset($fields['billing_postcode']);
unset($fields['billing_country']);
unset($fields['billing_state']);
# 1-1-2. customizefields
$fields['billing_first_name'] = array(
'label'=>"姓名",
'class' => array('form-row-wide'),
'required'=>true
);
return $fields;
}
# 1-2. shipping fields
add_filter( 'woocommerce_shipping_fields', 'customizing_shipping_fields' );
function customizing_shipping_fields($fields){
# 1-2-1. remove fields
unset($fields['shipping_first_name']);
unset($fields['shipping_last_name']);
unset($fields['shipping_company']);
unset($fields['shipping_address_2']);
unset($fields['shipping_country']);
# 1-2-2. add heading
add_filter( 'woocommerce_form_field_heading','woocommerce_form_field_heading', 10, 4 );
function woocommerce_form_field_heading($field, $key, $args, $value) {
$output = '<h3 class="form-row form-row-wide">'.__( $args['label'], 'woocommerce' ).'</h3>';
echo $output;
}
# 1-2-3. customize fields for shipping method "to home"
$fields['shipping_heading_tohome'] = array(
'id'=>'shipping_heading_tohome',
'label'=>"To home information",
'priority'=>1,
'class' => array('form-row-wide'),
'type' => 'heading',
'required'=>false
);
$fields['shipping_postcode'] = array(
'label'=>"Postcode",
'priority'=>5,
'class' => array('form-row-wide'),
'required'=>true,
'clear' => true,
'options'=>array(),
'label_class' => array(),
);
$fields['shipping_state'] = array(
'label'=>"Sate",
'priority'=>10,
'class' => array('form-row-wide'),
'required'=>true,
'clear' => true,
'options'=>array(),
'label_class' => array(),
);
$fields['shipping_city'] = array(
'label'=>"City",
'priority'=>20,
'class' => array('form-row-wide'),
'required'=>true,
'clear' => true,
'options'=>array(),
'label_class' => array(),
);
$fields['shipping_address_1'] = array(
'label'=>"Street",
'priority'=>30,
'class' => array('form-row-wide'),
'required'=>true,
'clear' => true,
'options'=>array(),
'label_class' => array(),
);
# 1-2-4. Customize fields for shipping method "to store"
$fields['shipping_heading_tostore'] = array(
'id'=>'shipping_heading_tostore',
'label'=>"To store information",
'priority'=>40,
'class' => array('form-row-wide'),
'type' => 'heading',
'required'=>false
);
$fields['shipping_store'] = array(
'label'=>"Store",
'priority'=>50,
'class' => array('form-row-wide'),
'type' => 'select',
'required'=> true,
'options'=> array(
'1' => 'A',
'2' => 'B',
'3' => 'C'
)
);
$fields['shipping_store_name'] = array(
'label'=>"Store name",
'priority'=>60,
'class' => array('form-row-wide'),
'type' => 'text',
'required'=> true,
);
$fields['shipping_store_number'] = array(
'label'=>"Store number",
'priority'=>70,
'class' => array('form-row-wide'),
'type' => 'text',
'required'=> true,
);
return $fields;
}
2.Show or hide fields based on shipping method
add_action( 'wp_footer', 'custom_checkout_field_script' );
function custom_checkout_field_script() {
$to_home = 'flat_rate:5'; 'flat_rate:8'; 'flat_rate:12'; 'flat_rate:13'; 'flat_rate:14'; 'flat_rate:15'; 'flat_rate:19'; 'flat_rate:20'; 'flat_rate:22';
$to_store = 'flat_rate:6'; 'flat_rate:11'; 'flat_rate:16'; 'flat_rate:17'; 'flat_rate:18';
$required_text = esc_attr__( 'required', 'woocommerce' );
$required_html = '<abbr class="required" title="' . $required_text . '">*</abbr>';
?>
<script>
jQuery(function($){
var ism = 'input[name^="shipping_method"]', ismc = ism+':checked',
csa = 'input#ship-to-different-address-checkbox',
rq = '-required', vr = 'validate'+rq, w = 'woocommerce', wv = w+'-validated',
iv = '-invalid', fi = '-field', wir = w+iv+' '+w+iv+rq+fi,
s = '#shipping_', f = '_field', z ='#shipping-zipcode',
a1 = 'heading_tohome', a2 = 'postcode', a3 = 'state', a4 = 'city', a5 = 'address_1',
a6 = 'heading_tostore', a7 = 'store', a8 = 'store_name', a9 = 'store_number',
s1 = s+a1+f, s2 = s+a2+f, s3 = s+a3+f, s4 = s+a4+f, s5 = s+a5+f,
s6 = s+a6+f, s7 = s+a7+f, s8 = s+a8+f, s9 = s+a9+f,
to_home = '<?php echo $to_home; ?>', to_store = '<?php echo $to_store; ?>';
// Utility function to shows or hide checkout fields
function showHide( action='show', selector='' ){
if( action == 'show' )
$(selector).show(function(){
$(this).addClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() == undefined )
$(selector+' label').append('<?php echo $required_html; ?>');
});
else
$(selector).hide(function(){
$(this).removeClass(vr);
$(this).removeClass(wv);
$(this).removeClass(wir);
if( $(selector+' > label > abbr').html() != undefined )
$(selector+' label > .required').remove();
});
}
// Initializing at start after checkout init (Based on the chosen shipping method)
setTimeout(function(){
if( $(ismc).val() == to_home ) // Chosen "to_home" (hiding "to_store")
{
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('show',s5);
showHide('show',z);
showHide('hide',s6 );
showHide('hide',s7 );
showHide('hide',s8 );
showHide('hide',s9 );
}
else if( $(ismc).val() == to_store ) // Choosen "to_store" (Hidding "to_home")
{
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('show',s6 );
showHide('show',s7 );
showHide('show',s8 );
showHide('show',s9 );
}
else
{
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('hide',s6);
showHide('hide',s7);
showHide('hide',s8);
showHide('hide',s9);
}
}, 100);
// When shipping method is changed (Live event)
$( 'form.checkout' ).on( 'change', ism, function() {
if( $(ismc).val() == to_home )
{
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('show',s5);
showHide('show',z);
showHide('hide',s6);
showHide('hide',s7);
showHide('hide',s8);
showHide('hide',s9);
if( $(csa).prop('checked') ) {
showHide('show',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('show',s5);
showHide('show',z);
showHide('hide',s6);
showHide('hide',s7);
showHide('hide',s8);
showHide('hide',s9);
}
}
else if( $(ismc).val() == to_store )
{
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('show',s6);
showHide('show',s7);
showHide('show',s8);
showHide('show',s9);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('show',s6);
showHide('show',s7);
showHide('show',s8);
showHide('show',s9);
}
}
else
{
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('hide',s6);
showHide('hide',s7);
showHide('hide',s8);
showHide('hide',s9);
if( $(csa).prop('checked') ) {
showHide('hide',s1);
showHide('hide',s2);
showHide('hide',s3);
showHide('hide',s4);
showHide('hide',s5);
showHide('hide',z);
showHide('hide',s6);
showHide('hide',s7);
showHide('hide',s8);
showHide('hide',s9);
}
}
});
});
</script>
<?php
}
// Checkout conditional fields validation
add_action('woocommerce_checkout_process', 'select_checkout_field_process');
function select_checkout_field_process() {
// HERE your shipping methods rate IDs
$to_home = 'flat_rate:5'; 'flat_rate:8'; 'flat_rate:12'; 'flat_rate:13'; 'flat_rate:14'; 'flat_rate:15'; 'flat_rate:19'; 'flat_rate:20'; 'flat_rate:22';
$to_store = 'flat_rate:6'; 'flat_rate:11'; 'flat_rate:16'; 'flat_rate:17'; 'flat_rate:18';
$chosen_shipping_method = WC()->session->get( 'chosen_shipping_methods' )[0];
$shipping_tohome = '<strong> ' . __('To home information', 'woocommerce') . ' ';
$postcode = __('postcode', 'woocommerce');
$state = __('State', 'woocommerce');
$city = __('city', 'woocommerce');
$address_1= __('street','woocommerce');
$shipping_tostore ='<strong>' . __('to store information', 'woocommerce') . ' ';
$store = __('store', 'woocommerce');
$store_name = __('store name', 'woocommerce');
$store_number = __('store number', 'woocommerce');
$end_text = '</strong> '. __('is required field', 'woocommerce');
if( $chosen_shipping_method == $to_home ) {
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_postcode']) )
wc_add_notice( $shipping_tohome . $postcode . $end_text, 'error' );
if( empty($_POST['shipping_state']) )
wc_add_notice( $shipping_tohome . $state . $end_text, 'error' );
if( empty($_POST['shipping_city']) )
wc_add_notice( $shipping_tohome . $city . $end_text, 'error' );
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping_tohome . $address_1 . $end_text, 'error' );
}
}
elseif( $chosen_shipping_method == $to_store ) {
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_store']) )
wc_add_notice( $shipping_tostore . $store . $end_text, 'error' );
if( empty($_POST['shipping_store_name']) )
wc_add_notice( $shipping_tostore . $store_name . $end_text, 'error' );
if( empty($_POST['shipping_store_number']) )
wc_add_notice( $shipping_tostore . $store_number . $end_text, 'error' );
}
}
else {
if( $_POST['ship_to_different_address'] ){
if( empty($_POST['shipping_postcode']) )
wc_add_notice( $shipping_tohome . $postcode . $end_text, 'error' );
if( empty($_POST['shipping_state']) )
wc_add_notice( $shipping_tohome . $state . $end_text, 'error' );
if( empty($_POST['shipping_city']) )
wc_add_notice( $shipping_tohome . $city . $end_text, 'error' );
if( empty($_POST['shipping_address_1']) )
wc_add_notice( $shipping_tohome . $address_1 . $end_text, 'error' );
if( empty($_POST['shipping_store']) )
wc_add_notice( $shipping_tostore . $store . $end_text, 'error' );
if( empty($_POST['shipping_store_name']) )
wc_add_notice( $shipping_tostore . $store_name . $end_text, 'error' );
if( empty($_POST['shipping_store_number']) )
wc_add_notice( $shipping_tostore . $store_number . $end_text, 'error' );
}
}
}