I've added a custom checkbox in the checkout page of my WooCommerce shop for the optional newsletter subscription:
add_action( 'woocommerce_after_order_notes', 'add_checkout_newsletter_subscribe', 9 );
function add_checkout_newsletter_subscribe() {
woocommerce_form_field( 'checkout_newsletter', array(
'type' => 'checkbox',
'class' => array('form-row checkout-newsletter-box'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => false,
'label' => 'Subscribe me.',
));
}
I've created a similar checkbox for the My Account page:
add_action( 'woocommerce_edit_account_form', '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 me.', 'woocommerce' ),
'clear' => true,
), get_user_meta(get_current_user_id(), 'newsletter-account', true ) );
}
add_action( 'woocommerce_save_account_details', 'save_checkbox_value_to_account_details', 10, 1 );
function save_checkbox_value_to_account_details( $user_id ) {
$value = isset( $_POST['newsletter-account'] ) ? '1' : '0';
update_user_meta( $user_id, 'newsletter-account', $value );
}
Now I'm looking for a way to connect the two checkboxes.
What I want to obtain is that if a customer flag the checkbox in the checkout page and it is a registered user, also the checkbox in the "My Account" page should be flagged.
I think I need to use the woocommerce_edit_account_form hook but unfortunately I've not other clues on how to obtain such a result.
Can you guys point me in the right direction?
Thank you very much for your help!
'.__('Invoice Number').': '.$value.'
';` with `if( 1 == get_user_meta( $order->get_user_id(), 'newsletter-account', true ) ) { echo ''.__('Newsletter').': Yes
'; } else { echo ''.__('Newsletter').': No
'; }`… – LoicTheAztec Jul 24 '20 at 10:05