Customize the checkout layout with code

These snippets change how CheckoutWC’s checkout is laid out. Many layout changes no longer need code, so each recipe says when a setting does the same job.

Where to add these snippets

  • PHP snippets go under CheckoutWC » Advanced » Scripts, in the PHP Snippets box of the Global section. Leave out the opening <?php tag. You can also use a child theme’s functions.php or a code snippets plugin. See Advanced settings. The PHP Snippets box runs on storefront requests but not in the WordPress admin, so a snippet that must also run there goes in a code snippets plugin or functions.php.
  • CSS goes in Custom CSS under CheckoutWC » Appearance » Design, in the CSS section.
  • Test on staging first. These snippets were checked against CheckoutWC 11.5.0. They only affect CheckoutWC’s checkout, not WooCommerce’s own checkout or the Checkout block.

CheckoutWC’s checkout has three steps by default, each a panel with its own ID: Information (cfw-customer-info), Shipping (cfw-shipping-method) and Payment (cfw-payment-method). Enable Order Review Step adds Review (cfw-order-review). Enable One Page Checkout shows every step on one page, with no breadcrumb or step buttons. Some recipes behave differently in those modes, and say so.

Steps

Add a custom step

The cfw_get_checkout_tabs filter adds a step. Each step has a label, a priority (Information is 20, Shipping 30, Payment 40) and a function that prints its content. This example adds an “Age Verification” step between Information and Shipping, and points the Information step’s buttons to it:

add_filter( 'cfw_get_checkout_tabs', function ( $tabs ) {
    $tabs['age_verification'] = array(
        'label'            => 'Age Verification',
        'classes'          => array(),
        'priority'         => 25,
        'enabled'          => true,
        'display_callback' => 'my_cfw_age_verification_step',
    );
    return $tabs;
} );

function my_cfw_age_verification_step() {
    // Add your fields here with woocommerce_form_field().
    // One Page Checkout has no step buttons, so stop here.
    if ( has_action( 'cfw_checkout_payment_method_tab', 'cfw_payment_tab_nav_one_page_checkout' ) ) {
        return;
    }
    ?>
    <div class="cfw-bottom-controls">
        <div class="previous-button"><?php cfw_return_to_customer_information_link(); ?></div>
        <?php cfw_continue_to_shipping_button(); ?>
        <?php cfw_continue_to_payment_button(); ?>
    </div>
    <?php
}

// Send the Information step's Continue buttons to the new step.
add_filter( 'cfw_continue_to_shipping_button', function ( $html ) {
    return 'cfw-customer-info' === cfw_get_current_tab() ? str_replace( 'data-tab="#cfw-shipping-method"', 'data-tab="#age_verification"', $html ) : $html;
} );
add_filter( 'cfw_continue_to_payment_button', function ( $html ) {
    return 'cfw-customer-info' === cfw_get_current_tab() ? str_replace( 'data-tab="#cfw-payment-method"', 'data-tab="#age_verification"', $html ) : $html;
} );

Then add this CSS, so the new step shows the right Continue button when the shipping step is hidden:

#age_verification .cfw-continue-to-payment-btn { display: none !important; }
.cfw-hide-shipping #age_verification .cfw-continue-to-payment-btn { display: block !important; }

The step appears in the breadcrumb automatically. Required fields you add with woocommerce_form_field() are checked before the customer can continue. The Shipping step’s Return to information link still goes back to Information rather than to your step.

If you only need a few extra fields, custom checkout fields in a Checkout Editor slot don’t need code.

Hide the shipping step

Use the setting: turn on Disable Shipping Step under CheckoutWC » Pages, on the Checkout tab, or clear Shipping Methods Step in the Checkout Editor’s Steps section. It hides the step, its breadcrumb item and the shipping line in the review. Customers get your default shipping method, so only use it if you have one shipping option for every order.

The code equivalent is add_filter( 'cfw_show_shipping_tab', '__return_false', 99 );. The priority of 99 keeps local pickup’s pickup step from turning it back on.

CheckoutWC shows a Cart item at the start of the breadcrumb and a Return to cart link on the Information step. Both are removed automatically when the side cart is on, and the Glass template has no Cart breadcrumb item. Disable Cart Step under CheckoutWC » Pages also removes the breadcrumb item, but it sends customers straight to the checkout after adding to cart, and can’t be used with the side cart.

To remove both without changing anything else:

add_filter( 'cfw_show_return_to_cart_link', '__return_false' );
add_filter( 'cfw_breadcrumbs', function ( $breadcrumbs ) {
    unset( $breadcrumbs['cart'] );
    return $breadcrumbs;
} );

Notices and content

Add a notice to the checkout

Without code, add a + New HTML block to a slot in the Checkout Editor’s Slots section. For example, Before Express Checkout puts it at the top of the Information step. HTML blocks run shortcodes but strip scripts. See Checkout Editor.

To show a notice on every step, above the breadcrumb, use code:

add_action( 'cfw_checkout_main_container_start', function () {
    echo '<div class="woocommerce-info">Orders placed after 2pm ship the next working day.</div>';
}, 15 );

Other places for a notice:

Where Hook
Information step, above the email field cfw_before_customer_info_account_details
Shipping step, above the heading cfw_before_shipping_method_heading
Payment step, above the payment methods cfw_checkout_payment_method_tab with priority 9

The hook cfw_before_customer_info_tab_login, used in older versions of this recipe, no longer exists. In the Futurist template, the breadcrumb is also printed at the top of the page, so check the notice’s position there.

Add attributes to the checkout form

The cfw_form_attributes filter changes the attributes of CheckoutWC’s <form> tag. It’s called for the checkout form (checkout) and for the order pay page’s forms (order_review and login_form):

add_filter( 'cfw_form_attributes', function ( $attributes, $id ) {
    if ( 'checkout' !== $id ) {
        return $attributes;
    }
    $attributes['data-my-attribute'] = 'my-value';
    $attributes['class'][]           = 'my-extra-class';
    return $attributes;
}, 10, 2 );

class is an array; other attributes are strings.

Header and buttons

The logo or site name at the top of the checkout, thank you and order pay pages links to your home page. To change it:

add_filter( 'cfw_header_home_url', function () {
    return home_url( '/shop/' );
}, 20 );

If you use Polylang, the priority of 20 keeps your link from being replaced by its language home page. There’s no setting for this.

Move the Complete Order button into the cart summary

The simplest option is a setting: Enable Order Review Step under CheckoutWC » Pages adds a final Review step with the totals and the Complete Order button together. On phones, Enable Mobile Totals shows the totals right above the button.

If you want the button under the cart summary on large screens instead, this works with the default multi-step checkout. It doesn’t work with One Page Checkout or the Order Review Step.

add_action( 'cfw_after_cart_summary', function () {
    if ( cfw_is_checkout() ) {
        cfw_place_order();
    }
} );
#cfw-cart-summary-content .place-order { display: none; }
@media (min-width: 992px) {
    .context-checkout .cfw-bottom-controls .place-order { display: none; }
    .cfw-payment-method-active #cfw-cart-summary-content .place-order { display: block; }
}

The page then has two copies of the button. When the customer changes payment method, CheckoutWC updates the label of the first copy only, so a gateway that changes the button text, such as PayPal, won’t change the summary copy.

Shipping totals

Show the chosen shipping method in the totals

This adds the chosen method’s name to the Shipping line in the cart summary totals, for example “Shipping (Flat rate)”:

add_filter( 'cfw_cart_totals_shipping_label', function ( $label ) {
    $labels = cfw_get_chosen_shipping_method_labels();
    return empty( $labels ) ? $label : $label . ' (' . esc_html( implode( ', ', $labels ) ) . ')';
} );

It applies to the checkout summary and to the side cart’s totals when Show Shipping and Totals is on.

Replace the shipping total on the Information step

Before the customer enters an address, the Shipping line in the totals has no price yet. To show your own text there on the Information step, in the sidebar and the mobile summary:

main:not(.cfw-one-page-checkout) .cfw-customer-info-active .cfw-totals-list .shipping td { font-size: 0; }
main:not(.cfw-one-page-checkout) .cfw-customer-info-active .cfw-totals-list .shipping td::after { content: "Calculated at next step"; font-size: 1rem; }

With One Page Checkout there are no separate steps, so this CSS doesn’t apply. To hide the Shipping line entirely, use the cfw_show_shipping_total filter.

Keep learning