如何创建复选框以在WooCommerce结账中显示/隐藏字段

时间:2021-08-24 作者:Răuțu Denis

我想在我想询问人们是否有增值税编号的所有字段之前放置一个复选框,当他们选中该复选框时,我想显示增值税编号字段,当未选中时,我想隐藏该字段。我在互联网上找到了一个代码,但不起作用。请看一下我的照片,以便理解。提前谢谢,我会非常感激的!

enter image description here

1 个回复
最合适的回答,由SO网友:Tanmay Patel 整理而成

输入此代码functions.php 文件

add_filter( \'woocommerce_checkout_fields\' , \'rws_display_checkbox_and_new_checkout_field\' ); 
function rws_display_checkbox_and_new_checkout_field( $fields ) {
$fields[\'billing\'][\'have_vat_number\'] = array(
    \'type\'      => \'checkbox\',
    \'label\'     => __(\'Are you have Vat Number?\', \'woocommerce\'),
    \'class\'     => array(\'form-row-wide\'),
    \'clear\'     => true
);   
$fields[\'billing\'][\'vat_number\'] = array(
    \'label\'     => __(\'Vat Number\', \'woocommerce\'),
    \'placeholder\'   => _x(\'Vat Number\', \'placeholder\', \'woocommerce\'),
    \'class\'     => array(\'form-row-wide\'),
    \'clear\'     => true
);
return $fields;
}
add_action( \'woocommerce_after_checkout_form\', \'rws_conditionally_hide_show_new_field\', 9999 );
function rws_conditionally_hide_show_new_field() {
  wc_enqueue_js( "
      jQuery(\'input#have_vat_number\').change(function(){
         if (! this.checked) {
            jQuery(\'#vat_number_field\').fadeOut();
            jQuery(\'#vat_number_field input\').val(\'\');         
         } else {
            jQuery(\'#vat_number_field\').fadeIn();
         } 
      }).change();
  ");   
}
function rws_move_checkout_email_field( $address_fields ) {
    $address_fields[\'have_vat_number\'][\'priority\'] = 1;
    $address_fields[\'vat_number\'][\'priority\'] = 21;
    return $address_fields;
}
add_filter( \'woocommerce_billing_fields\', \'rws_move_checkout_email_field\', 10 , 1 );

相关推荐

WordPress Shorcode显示层次分类[子项,父项]

在wordpress中显示分类法的父级时,我有一个小问题。(CPT=property and tax = property_city)我做了一些研究,想知道该怎么办。但我不太明白如何显示它们。我给你举个例子,你会很快明白的。what I have (not actual shortcode or any code! Just dynamic tag term showing taxonomy without parent)what I want to do理想的做法是在短代码中显示它们,所以我会在需要的地