将自定义结帐字段值保存为 WooCommerce 上的用户数据

Save custom checkout field value as user data on WooCommerce

我在我的 WooCommerce 商店的结帐页面中添加了一个自定义复选框,用于订阅可选的时事通讯:

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

    }

我为“我的帐户”页面创建了一个类似的复选框:

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 );
}

现在我正在寻找连接两个复选框的方法。

我想要获得的是,如果客户在结帐页面标记复选框并且它是注册用户,则还应标记 "My Account" 页面中的复选框。

我想我需要使用 woocommerce_edit_account_form 钩子,但不幸的是我没有关于如何获得这样的结果的其他线索。

你们能给我指出正确的方向吗?

非常感谢您的帮助!

以下将在结帐页面显示订阅复选框,如果客户尚未订阅,它将在提交时将其保存为用户元数据:

// 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' );
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。


它适用于我的帐户页面的问题代码:

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 );
}

如果在下订单时勾选了结帐选项,编辑帐户表单上的复选框也会被选中...