Home » WooCommerce: Retain Field Values @ Checkout Reload

WooCommerce: Retain Field Values @ Checkout Reload

by Tutor Aspire

Go to WooCommerce Checkout as a logged out user. Fill out billing name, surname, address, email and phone. Change your mind and go back to Cart. Return to Checkout. Billing name, surname, email and phone (basically everything but the address) are gone!

But hey, this annoying WooCommerce flaw is about to go away with a few lines of code. Either reload the Checkout page or go back to it later – your info will still be there (as long as you haven’t cleared your cache/cookies and the WooCommerce session hasn’t expired of course).

Enhoy!

A pretty annoying thing on the WooCommerce Checkout page is when you reload the page and you end up losing your Billing / Shipping data for no apparent reason. This snippet will now save that data, and keep it for you at the next reload!

PHP Snippet: Keep Billing, Shipping and Custom Field Data @ WooCommerce Checkout Reload

/**
 * @snippet       Retain Field Values | WooCommerce Checkout
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 6
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'woocommerce_checkout_update_order_review', 'tutoraspire_save_checkout_values', 9999 );

function tutoraspire_save_checkout_values( $posted_data ) {
 parse_str( $posted_data, $output );
    WC()->session->set( 'checkout_data', $output );
}

add_filter( 'woocommerce_checkout_get_value', 'tutoraspire_get_saved_checkout', 9999, 2 );

function tutoraspire_get_saved_checkout( $value, $index ) {
    $data = WC()->session->get( 'checkout_data' );
    if ( ! $data || empty( $data[$index] ) ) return $value;
    return is_bool( $data[$index] ) ? (int) $data[$index] : $data[$index];
}

add_filter( 'woocommerce_ship_to_different_address_checked', 'tutoraspire_get_saved_ship_to_different' );

function tutoraspire_get_saved_ship_to_different( $checked ) {
    $data = WC()->session->get( 'checkout_data' );
    if ( ! $data || empty( $data['ship_to_different_address'] ) ) return $checked;
    return true;
}

You may also like