Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Hide Country & State Field in the WooCommerce Shipping Calculator on the Cart Page?

In an essential feature of WooCommerce is the shipping calculator on the cart page. The shipping calculator allows customers to estimate shipping costs before proceeding to checkout. But in some cases, you might want to customize this functionality by hiding certain fields, such as the Country or State field to cater to specific business requirements.

In this post, we’ll guide you through the process of hiding the Country and State fields in the WooCommerce shipping calculator on the cart page.

Where to Add Custom Code in WooCommerce

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Hiding the Country & State Fields in the WooCommerce Cart Page

If your online store caters to customers from a specific country or region, it is advisable to hide the country and state fields in order to prevent them from entering incorrect information or selecting options that are not applicable to your shipping capabilities. To achieve this, you can use the below code snippet that will hide the Country & State fields in the WooCommerce cart page.

// 1 Disable Country
add_filter('woocommerce_shipping_calculator_enable_country','__return_false');

// 2 Disable State
add_filter( 'woocommerce_shipping_calculator_enable_state', '__return_false' );

add_filter( 'gettext', 'modify_woocommerce_text_strings', 20, 3 );

function modify_woocommerce_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Change address' :
            $translated_text = __( 'Calculate Shipping', 'woocommerce' );
            break;
    }
    return $translated_text;
}

Output

When you click on the “Calculate Shipping” button, it shows the City and Postcode. but it hides the Country and State fields on the WooCommerce cart page as shown below.

How to Hide Country & State Field in the WooCommerce Shipping Calculator on the Cart Page?

Code Explanation

Let’s break it down step by step-

1. Disabling Country and State Selection

  • The first two lines of code use WordPress filters i.e. woocommerce_shipping_calculator_enable_country and woocommerce_shipping_calculator_enable_state to disable the selection of both Country and State in the WooCommerce shipping calculator. The shipping calculator is a feature that allows customers to estimate shipping costs based on their location.
  • These filters prevent users from selecting a Country and a State when using the shipping calculator.

2. Modifying Text Strings:

  • The next part of the code is a function that modifies specific text strings displayed on the WooCommerce interface. It uses the gettext filter to replace certain text with custom text.
    • The add_filter function is used to hook the modify_woocommerce_text_strings function to the gettext filter. This function will be called whenever a text string is being translated using the gettext function.
    • The modify_woocommerce_text_strings function takes three parameters: the original translated text, the original text, and the text domain (which is ‘woocommerce’ in this case).
    • Inside the function, there’s a switch statement that checks if the original text is ‘Change address’. If it is, the function returns the translated version ‘Calculate Shipping’ instead.

Conclusion

You can also hide City and Postcode fields in the WooCommerce shipping calculator on the cart page.

Please share your feedback in the comments section regarding the usefulness of the code or if you have any further queries.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x