Davide
Davide

Reputation: 87

Save custom checkout field value as user data on WooCommerce

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!

Upvotes: 2

Views: 2711

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

The following will show the subscription checkbox on checkout page, if customer hasn't subscribe yet, and it will save it as user meta data on submission:

// Display custom checkout field on checkout page
add_action( 'woocommerce_after_order_notes', 'add_checkout_newsletter_subscribe', 9 );
function add_checkout_newsletter_subscribe() {

    $user_id    = (int) get_current_user_id();
    $newsletter = get_user_meta( $user_id, 'newsletter-account', true );

    // If newsletter hasn't been subscribed yet we show the subscription checkbox on checkout
    if ( $user_id > 0 && ! $newsletter ) :

    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'),
    )  );

    endif;
}

// Save custom checkout field value as user meta data
add_action('woocommerce_checkout_update_customer','custom_checkout_checkbox_update_customer', 10, 2 );
function custom_checkout_checkbox_update_customer( $customer, $data ){
    if ( ! is_user_logged_in() || is_admin() )
        return;

    // Update user meta data
    if ( isset($_POST['checkout_newsletter']) )
        update_user_meta( $customer->get_id(), 'newsletter-account', '1' );
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


It works with your question code for 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 );
}

If the checkout option has been ticked when placing an order, the checkbox on edit account form will be checked too…

Upvotes: 3

Related Questions