Typek – Blog

How to Remove the Shipping Address in WooCommerce Using PHP Snippet

WooCommerce is a popular e-commerce plugin for WordPress that allows you to set up an online store within your website. By default, WooCommerce requires customers to provide a shipping address during checkout. However, there might be situations where you’d like to remove the shipping address from the checkout process, such as when you’re selling digital products or offering local pickup.

In this tutorial, we’ll show you how to remove the shipping address from WooCommerce using a PHP snippet. We’ll cover the following topics:

Understanding WooCommerce Hooks

WooCommerce is built on top of WordPress and relies on hooks to allow developers to extend its functionality. Hooks are essentially events that occur during the execution of the WooCommerce code. When a hook is triggered, it allows developers to attach custom functions (referred to as callbacks) to these hooks, which can then modify or extend the default behavior of WooCommerce.

There are two types of hooks in WooCommerce:

  • Actions: These hooks allow you to add, modify, or remove functionality at specific points in the code execution.
  • Filters: These hooks allow you to modify data that is being passed through the code, such as modifying text or adding custom data to an array.

To remove the shipping address from WooCommerce, we’ll be using a filter hook called woocommerce_checkout_fields. This hook allows us to modify the fields displayed on the checkout page, including the shipping address fields.

Adding PHP Snippet to WordPress

There are several ways to add a PHP snippet to your WordPress website, such as:

  • Adding the code directly to your theme’s functions.php file
  • Creating a custom plugin to house your PHP snippets
  • Using a plugin like Code Snippets to manage your PHP snippets

For this tutorial, we’ll be adding our PHP snippet directly to the functions.php file of our theme. However, it’s essential to note that any changes made to the functions.php file will be lost when you update your theme. To prevent this, you can either create a child theme or use one of the alternative methods mentioned above.

Code Examples

Now that we understand hooks and how to add a PHP snippet to our website let’s dive into the code. Below is a PHP snippet that removes the shipping address fields from the WooCommerce checkout page:

<?php
// Remove shipping address fields from WooCommerce checkout
function typek_remove_shipping_address_fields( $fields ) {
    unset( $fields['shipping']['shipping_first_name'] );
    unset( $fields['shipping']['shipping_last_name'] );
    unset( $fields['shipping']['shipping_company'] );
    unset( $fields['shipping']['shipping_country'] );
    unset( $fields['shipping']['shipping_address_1'] );
    unset( $fields['shipping']['shipping_address_2'] );
    unset( $fields['shipping']['shipping_city'] );
    unset( $fields['shipping']['shipping_state'] );
    unset( $fields['shipping']['shipping_postcode'] );

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'typek_remove_shipping_address_fields' );
?>

To add this snippet to your website, simply copy and paste the above code into your theme’s functions.php file, or use one of the alternative methods mentioned earlier. Once you’ve added the snippet, your checkout page will no longer display the shipping address fields.

Conditional Removal of Shipping Address Fields

In some cases, you might want to conditionally remove the shipping address fields based on the products in the customer’s cart. For example, if you sell both physical and digital products, you might want to remove the shipping address fields only when the customer’s cart contains digital products.

Here’s a modified version of our PHP snippet that removes the shipping address fields only if the cart contains digital products:

<?php
// Remove shipping address fields from WooCommerce checkout if the cart contains digital products
function typek_remove_shipping_address_fields_conditional( $fields ) {
    $cart_contains_digital_products = false;

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['data']->is_virtual() ) {
            $cart_contains_digital_products = true;
            break;
        }
    }

    if ( $cart_contains_digital_products ) {
        unset( $fields['shipping']['shipping_first_name'] );
        unset( $fields['shipping']['shipping_last_name'] );
        unset( $fields['shipping']['shipping_company'] );
        unset( $fields['shipping']['shipping_country'] );
        unset( $fields['shipping']['shipping_address_1'] );
        unset( $fields['shipping']['shipping_address_2'] );
        unset( $fields['shipping']['shipping_city'] );
        unset( $fields['shipping']['shipping_state'] );
        unset( $fields['shipping']['shipping_postcode'] );
    }

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'typek_remove_shipping_address_fields_conditional' );
?>

Just like before, copy and paste the above code into your theme’s functions.php file or use one of the alternative methods mentioned earlier. With this snippet in place, the shipping address fields will only be removed from the checkout page if the customer’s cart contains digital products.

Alternative Plugins

If you’re not comfortable working with PHP or would prefer a more user-friendly solution, several plugins can help you remove the shipping address from WooCommerce. Some popular options include:

These plugins typically offer additional customization options for the checkout page, such as adding, modifying, or removing fields. Keep in mind that using a plugin adds extra overhead to your website, so it’s essential to weigh the benefits against any potential performance impact.

Conclusion

In this tutorial, we covered how to remove the shipping address from WooCommerce using a PHP snippet. We also discussed WooCommerce hooks, adding PHP snippets to your WordPress website, and provided code examples for removing the shipping address fields both unconditionally and conditionally based on the customer’s cart contents.

By following the steps outlined in this tutorial, you should now be able to remove the shipping address fields from your WooCommerce checkout page successfully. This can help streamline the checkout process for customers purchasing digital products or opting for local pickup, potentially leading to increased conversions and a better overall shopping experience.

More traffic??

Are you looking for effective solutions to increase your Google search visibility?

Contact us and discover the full potential of online sales.

What our clients are say?

For me, they are a happiness factory. They managed my store’s website in such a way that users became happy, after which it turned out that the search engine was also happy. When the search engine became happy and started bringing me more happy customers, my business became equally happy 🙂

Matt

Great company, I recommend it to everyone. Services of the highest standard. Very good customer approach. I am very satisfied with their work.

Lukasz Szajny

Complete professionalism and an incredible commitment to their work. It’s worth emphasizing their hassle-free communication and full availability. I definitely recommend them.

Alexander