Reputation: 2077
In the WooCommerce cart, you can set the default to ship to the billing address. The user can then check a box to enable a different shipping address. For returning customers it is a nuisance that you have to check the box again. I would like to remember the state of the checkbox for logged in users, so that not only the address information is shown, but also the state of the checkbox is the same as for the last order.
Upvotes: 0
Views: 403
Reputation: 2077
There are snippets published where you can add extra fields to shipping or billing, and let WooCommerce handle the storage. Other examples show custom fields where the data is stort in post. This is maintained on the order, but not available on the next order.
So I answered my own question above with the following. First have an action in functions.php of your child theme that saves the state of the checkbox in the user meta:
/**
* Update the user meta with checkbox setting
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
$order = new WC_Order( $order_id );
update_user_meta( $order->user_id , 'shipping_different',$_POST['ship_to_different_address']) ;
}
Then add an action to retrieve the stored checkbox value and update the checkbox:
/*
* Get the user meta to set the checkbox if needed
*/
add_action( 'woocommerce_after_checkout_billing_form', 'my_checkout_fields', 10,1 );
function my_checkout_fields( $checkout ) {
$user_id = get_current_user_id();
if ($user_id !=0 ) {
if (get_user_meta($user_id, 'shipping_different', true ) == 1)
add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );
}
}
Comments? Improvements?
Upvotes: 1