如果在WooCommerce中选中了checkout custom复选框,则更改用户角色 [英] Change user role if checkout custom checkbox is checked in WooCommerce
问题描述
我正在尝试将自定义功能添加到我的子主题functions.php文件中,其中在结帐页面上的帐单详细信息表单的底部添加了一个复选框.
I'm trying to add a custom function to my child-themes functions.php file where a checkbox is added to the bottom of the billing details form on the checkout page.
此复选框询问客户是否要成为批发客户.
This checkbox asks the customer if they'd like to become a wholesale customer.
function customise_checkout_field_with_wholesale_option($checkout) {
echo '<div id="wholesale_checkbox_wrap">';
woocommerce_form_field('wholesale_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Would you like to apply for a wholesale account?'),
'placeholder' => __('wholesale'),
'required' => false,
'value' => true
), $checkout->get_value('wholesale_checkbox'));
echo '</div>';
}
这很好,但是我在下一部分中遇到了麻烦.
This works fine, however I'm having trouble with this next part..
我希望客户用户角色如果选中复选框,则另存为"wholesale_customer"而不是"customer".
I would like the customers user role to save as "wholesale_customer" instead of "customer" if they check the checkbox.
add_action('woocommerce_after_checkout_billing_form', 'customise_checkout_field_with_wholesale_option');
function wholesale_customer( $order_id ) {
$order = new WC_Order( $order_id );
if (isset($_POST['wholesale_checkbox'])) {
if ($order->user_id > 0) {
$user = new WP_User($order->user_id);
// Remove role
$user->remove_role('customer');
// Add role
$user->add_role('wholesale_customer');
}
}
}
add_action( 'woocommerce_thankyou', 'wholesale_customer' );
当我删除批发_复选框if语句时,上述功能通过将客户另存为批发客户"而起作用.但是,如果包含if语句,它将始终将角色保存为客户".
The above function works by saving the customer as a "wholesale_customer" when I remove the wholesale_checkbox if statement. But with the if statement included, it always saves the role as "customer".
我要去哪里错了?干杯
推荐答案
以下是正确的方法:
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_with_wholesale_option' );
function custom_checkout_field_with_wholesale_option( $checkout ) {
if( current_user_can( 'wholesale_customer' ) ) return; // exit if it is "wholesale customer"
echo '<div id="wholesale_checkbox_wrap">';
woocommerce_form_field('wholesale_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Would you like to apply for a wholesale account?'),
'placeholder' => __('wholesale'),
'required' => false,
'value' => true
), '');
echo '</div>';
}
// Conditionally change customer user role
add_action( 'woocommerce_checkout_update_order_meta', 'wholesale_option_update_user_meta' );
function wholesale_option_update_user_meta( $order_id ) {
if ( isset($_POST['wholesale_checkbox']) ) {
$user_id = get_post_meta( $order_id, '_customer_user', true ); // Get user ID
if( $user_id > 0 ){
$user = new WP_User($user_id);
$user->remove_role('customer');
$user->add_role('wholesale_customer');
}
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试并可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
这篇关于如果在WooCommerce中选中了checkout custom复选框,则更改用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!