Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 自定义签出字段:使用选择器隐藏/显示字段_Javascript_Php_Jquery_Wordpress_Woocommerce - Fatal编程技术网

Javascript 自定义签出字段:使用选择器隐藏/显示字段

Javascript 自定义签出字段:使用选择器隐藏/显示字段,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,根据这一工作答案: // Registering external jQuery/JS file function cfields_scripts() { // IMPORTANT NOTE: // For a child theme replace get_template_directory_uri() by get_stylesheet_directory_uri() // The external cfields.js file goes in a subfo

根据这一工作答案:

 // Registering external jQuery/JS file
function cfields_scripts() {

    // IMPORTANT NOTE:
    // For a child theme replace get_template_directory_uri() by get_stylesheet_directory_uri()
    // The external cfields.js file goes in a subfolder "js" of your active child theme or theme.
    wp_enqueue_script( 'checkout_script', get_template_directory_uri().'/js/cfields.js', array('jquery'), '1.0', true );

}
add_action( 'wp_enqueue_scripts', 'cfields_scripts' );


add_filter( 'woocommerce_checkout_fields', 'ba_custom_checkout_billing_fields' );
function ba_custom_checkout_billing_fields( $fields ) {

    // 1. Creating the additional custom billing fields

    // The "status" selector
    $fields['billing']['billing_status']['type'] = 'select';
    $fields['billing']['billing_status']['class'] = array('form-row-wide, status-select');
    $fields['billing']['billing_status']['required'] = true;
    $fields['billing']['billing_status']['label'] = __('Statut Juridic', 'theme_domain');
    $fields['billing']['billing_status']['placeholder'] = __('Alege statutul', 'theme_domain');
    $fields['billing']['billing_status']['options'] = array(
        '1' => __( 'Persoana Fizica', 'theme_domain' ),
        '2' => __( 'Persoana Juridica', 'theme_domain' )
    );

    // The "Nr. registrul comertului" text field (this field is common)
    $fields['billing']['billing_ser_id']['type'] = 'text';
    $fields['billing']['billing_ser_id']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_ser_id']['required'] = true; // <== HERE has to be "true" as it always be shown and need validation
    $fields['billing']['billing_ser_id']['label'] = __('Nr. Reg. Comert', 'theme_domain');
    $fields['billing']['billing_ser_id']['placeholder'] = __('Introdu numarul', 'theme_domain');

    // The "Banca" text field
    $fields['billing']['billing_bt_id']['type'] = 'text';
    $fields['billing']['billing_bt_id']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_bt_id']['required'] = false;
    $fields['billing']['billing_bt_id']['label'] = __('Banca', 'theme_domain');
    $fields['billing']['billing_bt_id']['placeholder'] = __('Adauga Banca', 'theme_domain');

    // The "IBAN" text field
    $fields['billing']['billing_ib_id']['type'] = 'text';
    $fields['billing']['billing_ib_id']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_ib_id']['required'] = false;
    $fields['billing']['billing_ib_id']['label'] = __('IBAN', 'theme_domain');
    $fields['billing']['billing_ib_id']['placeholder'] = __('Adauga IBAN-ul', 'theme_domain');

    // The "CIF" text field
    $fields['billing']['billing_cf_id']['type'] = 'text';
    $fields['billing']['billing_cf_id']['class'] = array('form-row-wide', 'status-group2');
    $fields['billing']['billing_cf_id']['required'] = false;
    $fields['billing']['billing_cf_id']['label'] = __('Cod Fiscal', 'theme_domain');
    $fields['billing']['billing_cf_id']['placeholder'] = __('Adauga CIF-ul', 'theme_domain');


    // 2. Ordering the billing fields

    $fields_order = array(
        'billing_first_name', 'billing_last_name', 'billing_email',
        'billing_phone',      'billing_address_1', 'billing_address_2',
        'billing_postcode',   'billing_city',      'billing_country',
        'billing_status',     'billing_company',   'billing_ser_id',
        'billing_bt_id',      'billing_ib_id',     'billing_cf_id'
    );

    foreach($fields_order as $field)
        $ordered_fields[$field] = $fields['billing'][$field];

    $fields['billing'] = $ordered_fields;


    // 4. Returning Checkout customized billing fields
    return $fields;

}


// Process the checkout (Checking if required fields are not empty)
add_action('woocommerce_checkout_process', 'ba_custom_checkout_field_process');
function ba_custom_checkout_field_process() {

    if ( ! $_POST['billing_ser_id'] )
        wc_add_notice( __( '<strong>Nr. Reg. Comert</strong> is a required field.', 'theme_domain' ), 'error' );

    if ( ! $_POST['billing_bt_id'] )
        wc_add_notice( __( '<strong>Banca</strong> is a required field.', 'theme_domain' ), 'error' );

    if ( ! $_POST['billing_ib_id'] )
        wc_add_notice( __( '<strong>IBAN</strong> is a required field.', 'theme_domain' ), 'error' );

    if ( ! $_POST['billing_cf_id'] )
        wc_add_notice( __( '<strong>Cod Fiscal</strong> is a required field.', 'theme_domain' ), 'error' );
}


// Adding/Updating meta data to the order with the custom-fields values
add_action( 'woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta' );
function ba_custom_checkout_field_update_order_meta( $order_id ) {

    $billing_company = $_POST['billing_company'];
    $billing_ser_id  = $_POST['billing_ser_id'];
    $billing_bt_id   = $_POST['billing_bt_id'];
    $billing_ib_id   = $_POST['billing_ib_id'];
    $billing_cf_id   = $_POST['billing_cf_id'];

    // For Individual resetting billing company to "" (no value) instead of 'no'
    if ( !empty($billing_company) && 'no' == $billing_company )
        update_post_meta( $order_id, '_billing_company', '' );

    if ( !empty($billing_ser_id) )
        update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id ) );

    // Adding/updating data only for companies
    if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
        update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id ) );

    // Adding/updating data only for companies
    if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
        update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id ) );

    // Adding/updating data only for companies
    if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
        update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id ) );
}
要在customer order视图、Thankyu页面、My account order视图和电子邮件通知上显示数据,请在function.php文件中添加以下2个代码段:

// Displaying data on order view in "customer details" zone
add_action('woocommerce_order_details_after_customer_details','ba_add_values_to_order_item_meta', 10, 1 );
function ba_add_values_to_order_item_meta( $order ) {

    $output = '';
    $billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
    $billing_bt_id  = get_post_meta( $order->id, '_billing_bt_id',  true );
    $billing_ib_id  = get_post_meta( $order->id, '_billing_ib_id',  true );
    $billing_cf_id  = get_post_meta( $order->id, '_billing_cf_id',  true );

    if ( !empty($billing_ser_id) )
        $output .= '
        <tr>
            <th>' . __( "Nr. Reg. Comert:", "woocommerce" ) . '</th>
            <td>' . $billing_ser_id . '</td>
        </tr>';

    if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
        $output .= '
        <tr>
            <th>' . __( "Banca:", "woocommerce" ) . '</th>
            <td>' . $billing_bt_id . '</td>
        </tr>';

    if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
        $output .= '
        <tr>
            <th>' . __( "IBAN:", "woocommerce" ) . '</th>
            <td>' . $billing_ib_id . '</td>
        </tr>';

    if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
        $output .= '
        <tr>
            <th>' . __( "Cod Fiscal:", "woocommerce" ) . '</th>
            <td>' . $billing_cf_id . '</td>
        </tr>';

    echo $output;
}


// Displaying data on email notifications
add_action('woocommerce_email_customer_details','ba_add_values_to_emails_notifications', 15, 4 );
function ba_add_values_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {

    $output = '<ul>';
    $billing_ser_id = get_post_meta( $order->id, '_billing_ser_id', true );
    $billing_bt_id  = get_post_meta( $order->id, '_billing_bt_id',  true );
    $billing_ib_id  = get_post_meta( $order->id, '_billing_ib_id',  true );
    $billing_cf_id  = get_post_meta( $order->id, '_billing_cf_id',  true );

    if ( !empty($billing_ser_id) )
        $output .= '<li><strong>' . __( "Nr. Reg. Comert:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ser_id . '</span></li>';

    if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
        $output .= '<li><strong>' . __( "Banca:", "woocommerce" ) . '</strong> <span class="text">' . $billing_bt_id . '</span></li>';

    if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
        $output .= '<li><strong>' . __( "IBAN:", "woocommerce" ) . '</strong> <span class="text">' . $billing_ib_id . '</span></li>';

    if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
        $output .= '<li><strong>' . __( "Cod Fiscal:", "woocommerce" ) . '</strong> <span class="text">' . $billing_cf_id . '</span></li>';
        $output .= '</ul>';

    echo $output;
}
Javascript cfields.js代码外部文件:

//这个名为cfields.js的文件位于活动子主题或主题的子文件夹js中 jQuerydocument.readyfunction${ //公共串行ID字段 if!$billing\u ser\u id\u field.hasClassvalidate-required{ $billing\u ser\u id\u field.addClassvalidate-required; } //如果不是Persoana Juridica,则在开始时要隐藏的4个字段 如果$billing_状态选项:selected.val==1{ $'billing\u company\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $‘账单公司’。valno; }; $'billing_bt_id_field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing_bt_id'.valno; }; $'billing_ib_id_field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $“账单id”。valno; }; $'billing\u cf\u id\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing\u cf\u id'.valno; }; } //使用选择器显示/隐藏和添加/删除类的操作 $billing\u status.changefunction{ //司法人员 如果$billing_状态选项:selected.val==2 { $“账单\公司\字段”。showfunction{ $this.addClassvalidate-required; $‘账单公司’.val; }; $'billing_bt_id_field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing_bt_id'.val; }; $'billing_ib_id_field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing_ib_id'.val; }; $'billing\u cf\u id\u field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing\u cf\u id'.val; }; } //佩索纳·菲齐卡酒店 如果$billing_状态选项:selected.val==1,则为else { $'billing\u company\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $‘账单公司’。valno; }; $'billing_bt_id_field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing_bt_id'.valno; }; $'billing_ib_id_field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $“账单id”。valno; }; $'billing\u cf\u id\u field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing\u cf\u id'.valno; }; } }; };
因此,我的问题是,如何从第二个选项Persoana Juridica中删除公共字段CNP?

您只需将挂接函数ba_custom_checkout_billing_字段中的该行值更改为false而不是true:

$fields['billing']['billing_ser_id']['required'] = false;
然后我们也稍微改变一下这个钩子函数:

// Adding/Updating meta data to the order with the custom-fields values
add_action( 'woocommerce_checkout_update_order_meta', 'ba_custom_checkout_field_update_order_meta' );
function ba_custom_checkout_field_update_order_meta( $order_id ) {

    $billing_company = $_POST['billing_company'];
    $billing_ser_id  = $_POST['billing_ser_id'];
    $billing_bt_id   = $_POST['billing_bt_id'];
    $billing_ib_id   = $_POST['billing_ib_id'];
    $billing_cf_id   = $_POST['billing_cf_id'];

    if ( !empty($billing_company) && 'no' == $billing_company )
        update_post_meta( $order_id, '_billing_company', '' );

    ## Here we add 'no' == $billing_ser_id to the condition
    if ( !empty($billing_ser_id) && 'no' == $billing_ser_id )
        update_post_meta( $order_id, '_billing_ser_id', sanitize_text_field( $billing_ser_id ) );

    if ( !empty($billing_bt_id) && 'no' != $billing_bt_id )
        update_post_meta( $order_id, '_billing_bt_id', sanitize_text_field( $billing_bt_id ) );

    if ( !empty($billing_ib_id) && 'no' != $billing_ib_id )
        update_post_meta( $order_id, '_billing_ib_id', sanitize_text_field( $billing_ib_id ) );

    if ( !empty($billing_cf_id) && 'no' != $billing_cf_id )
        update_post_meta( $order_id, '_billing_cf_id', sanitize_text_field( $billing_cf_id ) );
}
您必须将外部JS文件替换为此更新文件:

jQuerydocument.readyfunction${ //如果不是Persoana Juridica,则在开始时要隐藏的4个字段 如果$billing_状态选项:selected.val==1{ $'billing\u company\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $‘账单公司’。valno; }; $'billing_bt_id_field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing_bt_id'.valno; }; $'billing_ib_id_field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $“账单id”。valno; }; $'billing\u cf\u id\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing\u cf\u id'.valno; }; } //使用选择器显示/隐藏和添加/删除类的操作 $billing\u status.changefunction{ //体育 司法机构 如果$billing_状态选项:selected.val==2 { $“账单\公司\字段”。showfunction{ $this.addClassvalidate-required; $‘账单公司’.val; }; $'billing_bt_id_field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing_bt_id'.val; }; $'billing_ib_id_field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing_ib_id'.val; }; $'billing\u cf\u id\u field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing\u cf\u id'.val; }; //隐藏序列ID字段 $'billing\u ser\u id\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $“账单服务id”。valno; }; } //佩索纳·菲齐卡酒店 如果$billing_状态选项:selected.val==1,则为else { $'billing\u company\u field'.hidefunction{ $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $‘账单公司’。valno; }; $'billing_bt_id_field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing_bt_id'.valno; }; $'billing_ib_id_field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $“账单id”。valno; }; $'billing\u cf\u id\u field'.hidefunction{ $this.childrenabbr.required.remove; $this.removeClassvalidate-required; $this.removeClasswoocommerce-validated; $'billing\u cf\u id'.valno; }; //显示串行ID字段 $'billing\u ser\u id\u field'.showfunction{ $this.children'label'.追加'*'; $this.addClassvalidate-required; $'billing_seru_id'.val; }; } }; }; 这样,序列ID自定义字段仅在选择“Persoana Fizica”时出现,在这种情况下,该字段仅为必填字段


最后一个更改未经测试,但这些都是需要进行的更改。

现在我还需要隐藏Persoana Juridica的个人名称