Reputation: 87
I would like to synhcronize a custom checkbox field in WooCommerce checkout page with a similar one in the My Account page (when the user is logged in).
Here's my code:
// Show a checkbox in checkout page and in My account > Details
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
add_action( 'woocommerce_after_order_notes', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe to my newsletter', 'woocommerce' ),
), get_user_meta(get_current_user_id(), 'newsletter-account', true ) );
}
// Save checkbox value when saved in My account > Details
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_from_account_details', 10, 1 );
function save_checkbox_value_from_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
Those two blocks of code work fine: if in My account > Details I checked the checkbox, the preference is saved and I can see a checked checkbox also in the checkout page.
Now the problem is that I need a way to obtain the same result when I edit my newsletter preference from the checkout page. I think I need to use the woocommerce_checkout_create_order but I haven't any idea on how to code this:
//Save the checkbox value when customer edit it from the checkout page
add_action( 'woocommerce_checkout_create_order', 'save_checkbox_value_from_checkout_page'. 10, 1 );
function save_checkbox_value_from_checkout_page() {
//some code here
}
Any suggestions are appreciated.
Upvotes: 1
Views: 890
Reputation: 254363
Updated
The hook woocommerce_checkout_create_order
is for order meta data and what you need is to save/update your checkout newsletter-account
field value as user meta data…
So is better to use dedicated
woocommerce_checkout_update_customer
hook.
Here is your complete code (tested and working):
add_action( 'woocommerce_edit_account_form', 'display_checkbox_in_account_page' );
add_action( 'woocommerce_after_order_notes', 'display_checkbox_in_account_page' );
function display_checkbox_in_account_page() {
woocommerce_form_field( 'newsletter-account', array(
'type' => 'checkbox',
'class' => array('form-row-wide'),
'label' => __( 'Subscribe to my newsletter', 'woocommerce' ),
), get_user_meta( get_current_user_id(), 'newsletter-account', true ) );
}
// Save/update checkbox value when saved in My account > Details
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_from_account_details', 10, 1 );
function save_checkbox_value_from_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
// Save/update custom checkout field value as user meta data
add_action('woocommerce_checkout_update_customer','custom_checkout_checkbox_update_customer', 100, 2 );
function custom_checkout_checkbox_update_customer( $customer, $data ){
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $customer->get_id(), 'newsletter-account', $value ); // Updated
}
Code goes in functions.php file of your active child theme (or active theme).
Now if you want to grab that information additionally to the order as order meta data you will use:
add_action('woocommerce_checkout_create_order','custom_checkout_checkbox_add_order_meta', 100, 2 );
function custom_checkout_checkbox_add_order_meta( $order, $data ){
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
$order->update_meta_data( 'newsletter-account', $value );
}
Upvotes: 1