Skip to content

Instantly share code, notes, and snippets.

@jakewhiteley
Last active September 29, 2021 21:05
Show Gist options
  • Save jakewhiteley/0044f54ec89b5bb19c3c to your computer and use it in GitHub Desktop.
Save jakewhiteley/0044f54ec89b5bb19c3c to your computer and use it in GitHub Desktop.
Uses the WC session manager to ensure checkout details are not lost after a customer returns after leaving the checkout page.
<?php
add_action( 'woocommerce_after_checkout_validation', 'set_persitent_checkout' );
add_filter( 'woocommerce_checkout_get_value', 'get_persistent_checkout', 10, 2 );
add_action( 'woocommerce_ship_to_different_address_checked', 'get_persitent_ship_to_different' );
function set_persitent_checkout ( $a )
{
$arr = array();
foreach ( $a as $key => $value )
if ( ! empty($value) )
$arr[$key] = $value;
WC()->session->set( 'form_data', $arr );
return $a;
}
function get_persistent_checkout ( $value, $index )
{
$data = WC()->session->get('form_data');
if ( ! $data || empty($data[$index]) )
return $value;
return is_bool($data[$index]) ? (int) $data[$index] : $data[$index];
}
function get_persitent_ship_to_different ( $value )
{
$data = WC()->session->get('form_data');
if ( ! $data || empty($data['ship_to_different_address']) )
return $value;
return is_bool($data['ship_to_different_address']) ? (int) $data['ship_to_different_address'] : $data['ship_to_different_address'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment