我在 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 );
}
现在我正在寻找一种连接这两个复选框的方法。
我想要获得的是,如果客户在结账页面中标记了复选框并且该客户是注册用户,那么“我的帐户”页面中的复选框也应该被标记。
我认为我需要使用 woocommerce_edit_account_form Hook ,但不幸的是我没有关于如何获得这样的结果的其他线索。
你们能给我指出正确的方向吗?
非常感谢您的帮助!
最佳答案
如果客户尚未订阅,以下内容将在结账页面显示订阅复选框,并在提交时将其保存为用户元数据:
// 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 );
}
如果下订单时勾选了结帐选项,编辑帐户表单上的复选框也会被选中...
关于php - 将自定义结账字段值保存为 WooCommerce 上的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57728051/