[英]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.