简体   繁体   English

在 WooCommerce 中的结帐和我的帐户之间同步时事通讯自定义复选框

[英]Sync newsletter custom checkbox between checkout and my account in WooCommerce

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).我想将 WooCommerce 结帐页面中的自定义复选框字段与“我的帐户”页面中的类似字段(当用户登录时)同步。

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:我想我需要使用 woocommerce_checkout_create_order 但我不知道如何编码:

//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.任何建议表示赞赏。

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挂钩woocommerce_checkout_create_order用于订单元数据,您需要的是将您的结帐newsletter-account字段值保存/更新为用户元数据……

So is better to use dedicated woocommerce_checkout_update_customer hook.所以最好使用专用的woocommerce_checkout_update_customer钩子。

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).代码位于活动子主题(或活动主题)的 functions.php 文件中。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Woocommerce 3中在结帐和我的帐户上启用自定义字段 - Enable a Custom Field on checkout and on My account In Woocommerce 3 将用户自定义帐户字段添加到 WooCommerce 结帐 - Add user custom account field to WooCommerce checkout 在Woocommerce注册和结帐页面中添加自定义复选框 - Add a custom checkbox in Woocommerce registration and checkout pages 在 WooCommerce My account 和 Checkout 上自定义地址字段 - Customize addresses fields on WooCommerce My account and Checkout 如何在 WooCommerce 结帐中编辑我的帐户部分? - How to edit my account section in WooCommerce checkout? 如何移动 Woocommerce 结帐页面上的创建帐户复选框 - How to move the create account checkbox on Woocommerce checkout page 隐藏自定义结帐字段不出现在 WooCommerce 我的帐户编辑地址 - Hide custom checkout fields from appearing in WooCommerce my account edit address 自定义复选框显示或隐藏Woocommerce结帐中的自定义通知 - Custom checkbox show or hide custom notice in Woocommerce checkout 如何从 WooCommerce 结帐时保存自定义复选框字段 state? - How to save from WooCommerce checkout a custom checkbox field state? 如果在 WooCommerce 中选中了结帐自定义复选框,则更改用户角色 - Change user role if checkout custom checkbox is checked in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM