• Resolved Bassam Radi

    (@bassamradi)


    I am working on a custom template and I would like to have the state name to be shown as a full name. I saw this post https://www.ads-software.com/support/topic/how-to-split-billing-address-name/ and checked the documentation but nothing worked

    I did the below but still, nothing is showing except one code just showed me the state code not the full name. BTW, the country is Saudi Arabia.

    in child theme function
    …………………………………
    add_filter( ‘woocommerce_localisation_address_formats’, ‘change_localisation_usa_state_format’, 20, 2 );
    function change_localisation_usa_state_format( $address_formats ){
    $address_formats[‘SA’] = “{name}\n{company}\n{address_1}\n{address_2}\n{city}, {state} {postcode}\n{country}”;

    return $address_formats;
    }
    …………………………………

    in the custom template, I tried all the below
    …………………………………
    a. <td><?php $this->get_shipping_state(); ?></td> // shows nothing
    b. <td><?php $this->order->get_shipping_state(); ?></td> // shows nothing
    c. <td><?php $order->get_shipping_state(); ?></td> // shows nothing
    d. <td><?php echo $order->get_meta(‘_shipping_state’); ?></td> // shows nothing
    e. <td><?php echo $order->get_meta(‘shipping_state’); ?></td> // shows nothing
    f. <td><?php $this->custom_field(‘_shipping_state’); ?></td> // shows Code only
    ………………………………….

    and the order meta field is showing the correct label (IMG attached) https://ibb.co/3y8y5Nq

    I don’t know what I am doing wrong. Please advice.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    with the first snippet you shouldn’t make any changes in the custom template. It changes the full formatted address, so if you only get the separate data you won’t see the effect. Can you try with just the first snippet and the original Simple template?

    p.s. b & c are the correct methods to get the data (code as stored in the meta, not full name), but you need to put echo otherwise it gets the value without displaying it.

    Thread Starter Bassam Radi

    (@bassamradi)

    thanks for your replay

    Thread Starter Bassam Radi

    (@bassamradi)

    After a lot of research, I was able to do it in the customized template.

    in the custom template I added the code below after the line 2 in the template:

    <?php
            		$countries_obj = new WC_Countries();
                            $countries_array = $countries_obj->get_countries();
                            $country_states_array = $countries_obj->get_states();
                                    
                            // Get the country name:
                            $country_name = $countries_array['SA'];
                                    
                            // Get the state name:
                            $state_name = WC()->countries->states[$order->shipping_country][$order->shipping_state];
    
                            ?>

    in the shipping address area I added the code below

            	        <tr>
                                <td><?php // Display state names:
                                echo $state_name;
                                ?></td>
            	        </tr>

    It’s working fine now. I hope someone finds this useful.

    • This reply was modified 3 years, 6 months ago by Bassam Radi.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @bassamradi,

    This is a safer way to display your State name:

    $country = $order->get_shipping_country();
    $state = $order->get_shipping_state();
    $wc_state_name = WC()->countries->get_states( $country )[$state];
    $state_name = !empty($wc_state_name) ? $wc_state_name : $state;
    echo $state_name;

    Let me know if it works ??

    Thread Starter Bassam Radi

    (@bassamradi)

    @yordansoares it worked thank you. Whats wrong with my code?

    • This reply was modified 3 years, 6 months ago by Bassam Radi.
    • This reply was modified 3 years, 6 months ago by Bassam Radi.
    Plugin Contributor Yordan Soares

    (@yordansoares)

    The issue with your code is that if the customer types the state, instead of selecting it in the case of countries that preload the list of their states, the field will be empty. With the code I sent you, the state will always be printed, whether it’s selected from a list or typed by your customer ??

    By the way, if you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

    Thread Starter Bassam Radi

    (@bassamradi)

    I needed the country as well so I mixed it from my code and yours to echo $country_name

    `$country = $order->get_shipping_country();
    $countries_obj = new WC_Countries();
    $countries_array = $countries_obj->get_countries();
    $country_name = $countries_array[‘SA’];
    $state = $order->get_shipping_state();
    $wc_state_name = WC()->countries->get_states( $country )[$state];
    $state_name = !empty($wc_state_name) ? $wc_state_name : $state;`

    sure 5 star ??

    • This reply was modified 3 years, 6 months ago by Bassam Radi.
    • This reply was modified 3 years, 6 months ago by Bassam Radi.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Having the state as a full name’ is closed to new replies.