将自定义结帐字段值另存为WooCommerce上的用户数据 [英] Save custom checkout field value as user data on WooCommerce

查看:187
本文介绍了将自定义结帐字段值另存为WooCommerce上的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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.

我想我需要使用 woocommerce_edit_account_form 钩子,但不幸的是,我没有其他线索来获得这种结果。

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?

非常感谢您的帮助!

推荐答案

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

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

代码在您的活动子主题(或活动主题)的functions.php文件中)。经过测试并可以使用。

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

如果下订单时勾选了checkout选项,则编辑帐户表单也将被检查...

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

这篇关于将自定义结帐字段值另存为WooCommerce上的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆