How to Simplify Checkout Free Items Storefront Theme

Storefront Simplify Checkout Free ItemsIn most WooCommerce store, customers purchase products and remit payment after proceeding to the checkout. Moreover, they will also select a shipping method or enter a coupon code to receive a discount.

However, if you are offering a free item in your store, you may not require all of the fields at the checkout. This means that you will not be collecting any shipping payments. As a result, you may not require the billing details, just the name, and probably an email.

Storefront Simplify Checkout Free Items

It is worth mentioning that WooCommerce will helpfully remove payment method selection for free checkouts. However, the billing fields are still present and required to place an order:billing details

If you sell free virtual products, such as free membership, it would be best to simplify the checkout process.

If you are having this problem, it is easy to check if the checkout has no cost with the WC()->cart->needs_payment() check. I will illustrate how to use this to take a few actions to simplify the free WooCommerce checkout if no payment is needed. We need to remove some billing fields conditionally.

Let me walk you through some steps you can take to simplify the free checkout and put it together into a usable snippet.

Steps to Simplify the Checkout for Free Items

  1. Remove Coupon Forms

You do not need a coupon form if you want to have a free checkout, as there is nothing to discount. I will use ! WC()->cart->needs_payment() as a check, which will assume that the checkout total is $0, so there are no shipping costs or order payment needed. To safely remove the coupon input, we need to use the following code:

// remove coupon forms if you don't want a coupon for a free cart

remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
  1. Disable Order Notes

You may want to gather order notes for a free order. However, the main thing we are trying to do is simplifying the checkout process for customers in your WooCommerce store. I will remove the entire “Additional Information” section of the checkout. This can be done using the  woocommerce_enable_order_notes_field filter, as we can set this to false, so there are no order notes.

// Remove the "Additional Info" order notes

add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
  1. Unset Unnecessary Fields

Since it is a free order, we do not need to charge a credit card. Most of the billing fields in the checkout are not needed. This means that I have to unset the buying field I do not need. You can find a list of the checkout fields in the WooCommerce documentation.

// Unset the fields we don't want in a free checkout

function unset_unwanted_checkout_fields( $fields ) {

// list of the billing field keys to remove

$billing_keys = array(

'billing_company',

'billing_phone',

'billing_address_1',

'billing_address_2',

'billing_city',

'billing_postcode',

'billing_country',

'billing_state',

);

// unset each of those unwanted fields

foreach( $billing_keys as $key ) {

unset( $fields['billing'][$key] );

}

return $fields;

}

add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );

However, you can choose with fields you want to remove from the checkout form.

  1. Adding the Code

Here are the steps for adding the code in the functions.php file of the Storefront theme:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to simplify the free item checkout.
  3. Add the following code to the functions.php
/**

 * Removes coupon form, order notes, and several billing fields if the checkout doesn't require payment.

 *

 * REQUIRES PHP 5.3+

 *

  */

function sv_free_checkout_fields() {

            // first, bail if WC isn't active since we're hooked into a general WP hook

            if ( ! function_exists( 'WC' ) ) {

                        return;

            }

            // bail if the cart needs payment, we don't want to do anything

            if ( WC()->cart && WC()->cart->needs_payment() ) {

                        return;

            }

            // now continue only if we're at checkout

            // is_checkout() was broken as of WC 3.2 in ajax context, double-check for is_ajax

            // I would check WOOCOMMERCE_CHECKOUT but testing shows it's not set reliably

            if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) {

                        // remove coupon forms since why would you want a coupon for a free cart??

                        remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );

                        // Remove the "Additional Info" order notes

                        add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

                        // Unset the fields we don't want in a free checkout

                        add_filter( 'woocommerce_checkout_fields', function( $fields ) {

                                    // add or remove billing fields you do not want

                                    // fields: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2

                                    $billing_keys = array(

                                                'billing_company',

                                                'billing_phone',

                                                'billing_address_1',

                                                'billing_address_2',

                                                'billing_city',

                                                'billing_postcode',

                                                'billing_country',

                                                'billing_state',

                                    );

                                    // unset each of those unwanted fields

                                    foreach( $billing_keys as $key ) {

                                                unset( $fields['billing'][ $key ] );

                                    }

                                    return $fields;

                        } );

            }

}

add_action( 'wp', 'sv_free_checkout_fields' );
  1. When you add the entire code, the WooCommerce checkout will be simplified:simplified checkout

Conclusion

In summary, I have walked you through the steps of simplifying the checkout process. I have explained the steps in detail so that you can understand the code. However, you need to note that this article features code changes or snippets you can make in your active themes function.php file. If you are unfamiliar with this task, please hire a WordPress developer.

Similar Articles