Conditionally customizing WooCommerce checkout fields

后端 未结 3 2039
情书的邮戳
情书的邮戳 2021-02-11 09:44

I am trying to modify WooCommerce check out fields. There are two points I want to reach.

  1. Conditional fields

    I want to make conditional fields for differe

相关标签:
3条回答
  • 2021-02-11 09:57

    You've missed quote around "shipping_type" on this line :

    $("input[name='shipping_type'").on("change",function(){
    

    If this fix's not working : do you have error in the javascript console ?

    0 讨论(0)
  • 2021-02-11 10:09

    First you can merge some functions when the same hook is involved… Then for your jQuery script is better to set it like in the following code (there is also many little mistakes in your jQuery script):

    // Customizing default checkout shipping fields
    add_filter( 'woocommerce_shipping_fields', 'customizing_shipping_fields' );
    function customizing_shipping_fields($fields){
    
        # 1. Remove shipping fields
        unset($fields['shipping_country']);
        unset($fields['shipping_state']);
    
        # 2. Customize shipping fields
        $label_fields = array(
            'first_name' => __('取件者 *'),  'last_name'  =>   __('手機號碼 *'),
            'company'    => __('店名 *'),    'city'        => __('服務編號 *'),
            'address_1'  => __('收件地址 *'), 'address_2' => __('預計來訪時間 *'),
        );
        foreach( $label_fields as $key => $value ){
            $fields['shipping_'.$key]['label'] = $value;
            $fields['shipping_'.$key]['id'] = 'shipping_'.$key;
        }
    
        # 3. Customize shipping fields required
        $required_fields = array( 'first_name', 'last_name', 'company', 'city', 
            'address_1', 'address_2', 'postcode');
        foreach( $required_fields as $key => $value )
            $fields['shipping_'.$key]['required'] = false;
    
        return $fields;
    }
    
    // Add a Custom radio field for shipping options
    add_action( 'woocommerce_before_checkout_shipping_form', 'custom_shipping_radio_button', 10, 1 );
    function custom_shipping_radio_button( $checkout ) {
        # 1. CSS styling
        ?>
        <style>label.radio{ display:inline-block; margin-right:1em; }</style>
        <?php
    
        # 2. Add a custom radio field
        woocommerce_form_field( 'shipping_type', array(
            'type' => 'radio',
            'class' => array( 'form-row-wide' ),
            'label' => __('收件方式'),
            'options' => array(
                'shipping_1' => __('全家店到店'),
                'shipping_2' => __('指定地址'),
                'shipping_3' => __('自行取貨'),
            ),
        ), $checkout->get_value( 'shipping_type' ) );
    
        # 3. jQuery Script
        ?>
        <script type="text/javascript">
            jQuery(function($){
                $("input[name=shipping_type]").on("change",function(){
                    if($("#shipping_type_shipping_1").is(":checked")) {
                        $("#shipping_first_name,#shipping_last_name,#shipping_city,#shipping_company").fadeIn();
                    } else {
                        $("#shipping_first_name,#shipping_last_name,#shipping_city,#shipping_company").fadeOut();
                    }
                    if($("#shipping_type_shipping_2").is(":checked")) {
                        $("#shipping_postcode,#shipping_address_1").fadeIn();
                    } else {
                        $("#shipping_postcode,#shipping_address_1").fadeOut();
                    }
                    if($("#shipping_type_shipping_3").is(":checked")) {
                        $("#shipping_address_2_field_2").fadeIn();
                    } else {
                        $("#shipping_address_2_field_2").fadeOut();
                    }
                });
            });
        </script>
        <?php
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    Tested and works.


    Now if you want to hide fields with labels, your jQuery script will be:

        <script type="text/javascript">
            jQuery(function($){
                $("input[name=shipping_type]").on("change",function(){
                    if($("#shipping_type_shipping_1").is(":checked")) {
                        $("#shipping_first_name_field,#shipping_last_name_field,#shipping_city_field,#shipping_company_field").fadeIn();
                    } else {
                        $("#shipping_first_name_field,#shipping_last_name_field,#shipping_city_field,#shipping_company_field").fadeOut();
                    }
                    if($("#shipping_type_shipping_2").is(":checked")) {
                        $("#shipping_postcode_field,#shipping_address_1_field").fadeIn();
                    } else {
                        $("#shipping_postcode_field,#shipping_address_1_field").fadeOut();
                    }
                    if($("#shipping_type_shipping_3").is(":checked")) {
                        $("#shipping_address_2_field_2").fadeIn();
                    } else {
                        $("#shipping_address_2_field_2").fadeOut();
                    }
                });
            });
        </script>
    

    Tested and works too…

    But you should need to set the fields to be not required as you will face some problems when submitting data… This will oblige you to make some other changes in order to get something functional. But this will be a new question

    0 讨论(0)
  • 2021-02-11 10:12

    @LoicTheAztec Thanks you for your reply, you gave me a great help. As you mentioned, required fields are the problem. As a result, I tried to use the code as below to solve the problem. Although it solved the problem about required fields, it cause the labels of the fields cannot be shown.

    add_filter( 'woocommerce_shipping_fields', 'customizing_shipping_fields_required' );
    function customizing_shipping_fields_required($fields) {
        $fields['shipping_first_name'] = array(
        'required'=>false
        );
        $fields['shipping_last_name'] = array(
        'required'=>false
        );
        $fields['shipping_company'] = array(
        'required'=>false
        );
        $fields['shipping_city'] = array(
        'required'=>false
        );
        $fields['shipping_address_1'] = array(
        'required'=>false
        );
        $fields['shipping_address_2'] = array(
        'required'=>false
        );
        $fields['shipping_postcode'] = array(
        'required'=>false
        );
        return $fields;
    }
    
    0 讨论(0)
提交回复
热议问题