• Resolved aliaksu

    (@aliaksu)


    I customized Woocomerce’s default address fields. I selected the Address1 and City fields.

    Fields save data seamlessly. When the first page is loaded in the My Account>Address>Address edit, these fields appear empty. Same thing at checkout. Normally, when these fields are input, if the user has registered data, these fields are filled. But I tried many things in the select, but it didn’t work.

    //  Checkout field update güncelle
    function custom_override_checkout_fields_plugin( $fields ) {
        global $tr_cities;
    
        $fields['shipping']['shipping_state']['type'] = 'select';
        $fields['shipping']['shipping_state']['options'] = array_merge( array( '' => 'Bir se?enek belirleyin...' ), $tr_cities );
        $fields['shipping']['shipping_state']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'state_select');
    
        $fields['shipping']['shipping_city']['type'] = 'select';
        $fields['shipping']['shipping_city']['options'] = array('' => '?nce il se?in...');
        $fields['shipping']['shipping_city']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'city_select');
    
        $fields['shipping']['shipping_address_1']['type'] = 'select';
        $fields['shipping']['shipping_address_1']['options'] = array('' => '?nce il?e se?in...');
        $fields['shipping']['shipping_address_1']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'address_select');
    
        return $fields;
    }
    add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields_plugin' );
    
    // My Account field update
    function custom_override_my_account_fields_plugin( $address ) {
        global $tr_cities;
    
        $address['state']['type'] = 'select';
        $address['state']['options'] = array_merge( array( '' => 'Bir se?enek belirleyin...' ), $tr_cities );
        $address['state']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'state_select');
    
        $address['city']['type'] = 'select';
        $address['city']['options'] = array('' => '?nce il se?in...');
        $address['city']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'city_select');
    
        $address['address_1']['type'] = 'select';
        $address['address_1']['options'] = array('' => '?nce il?e se?in...');
        $address['address_1']['class'] = array('form-row-wide', 'address-field', 'update_totals_on_change', 'validate-required', 'address_select');
    
        return $address;
    }
    add_filter( 'woocommerce_default_address_fields', 'custom_override_my_account_fields_plugin' );
    
    //load city
    function load_districts() {
        check_ajax_referer('checkout_nonce', 'nonce');
    
        global $tr_districts;
    
        $city_code = sanitize_text_field($_POST['city_code']);
        if (isset($tr_districts[$city_code])) {
            wp_send_json_success($tr_districts[$city_code]);
        } else {
            wp_send_json_error('Ge?ersiz il kodu');
        }
    }
    add_action('wp_ajax_load_districts', 'load_districts');
    add_action('wp_ajax_nopriv_load_districts', 'load_districts');
    
    //js code
    jQuery(document).ready(function($) {
        function loadDistricts(cityCode) {
            if (!cityCode) return;
    
            $.ajax({
                type: 'POST',
                url: checkout_params.ajax_url,
                data: {
                    action: 'load_districts',
                    city_code: cityCode,
                    nonce: checkout_params.nonce
                },
                success: function(response) {
                    if (response.success) {
                        var options = '<option value="">Bir il?e se?in...</option>';
                        $.each(response.data, function(index, district) {
                            options += '<option value="' + district + '">' + district + '</option>';
                        });
                        $('#shipping_city').html(options).trigger('change');
                    } else {
                        alert(response.data);
                    }
                }
            });
        }
    
    

    When we use the address 1 and city fields as selections, the registered data will be selected when the pages are first loaded.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Rajesh K. (woo-hc)

    (@rajeshml)

    Hello @aliaksu,

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself.

    For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter aliaksu

    (@aliaksu)

    ok,thanks.You can delete this post.

    Hi @aliaksu,

    ok,thanks.You can delete this post.

    I’m afraid we’re unable to delete posts made in this forum. All I can do from my end is to mark this thread as solved, as they’re used by the community for reference should anyone else face a similar issue as yours.

    I hope this clarifies any confusion.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WooCommerce default address fields customization’ is closed to new replies.