Php 如果在WooCommerce中选中了“签出自定义”复选框,则更改用户角色

Php 如果在WooCommerce中选中了“签出自定义”复选框,则更改用户角色,php,wordpress,woocommerce,hook-woocommerce,user-roles,Php,Wordpress,Woocommerce,Hook Woocommerce,User Roles,我正在尝试向我的child themes functions.php文件中添加一个自定义函数,其中一个复选框被添加到签出页面上账单详细信息表单的底部 此复选框询问客户是否希望成为批发客户 function customise_checkout_field_with_wholesale_option($checkout) { echo '<div id="wholesale_checkbox_wrap">'; woocommerce_form_field('wholesale_che

我正在尝试向我的child themes functions.php文件中添加一个自定义函数,其中一个复选框被添加到签出页面上账单详细信息表单的底部

此复选框询问客户是否希望成为批发客户

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

}
当我删除wholesale\u复选框if语句时,上述函数将客户保存为“wholesale\u customer”。但是如果包含if语句,它总是将角色保存为“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');
        }
    }
}
add_action('woocommerce_在订单注释之后,'custom_checkout_field_,带有批发选项');
带有批发选项的自定义签出字段($checkout){
if(当前用户\u can('wholesale\u customer'))返回;//如果是“wholesale customer”,则退出
回声';
woocommerce\u表单\u字段('批发\u复选框',数组(
'键入'=>'复选框',
'class'=>array('input-checkbox'),
“label'=>”(您想申请批发账户吗?),
“占位符”=>“‘批发’”,
“必需”=>false,
“值”=>true
), '');
回声';
}
//有条件地更改客户用户角色
添加操作('woocommerce\u checkout\u update\u order\u meta'、'Wholesal\u option\u update\u user\u meta');
功能批发\u选项\u更新\u用户\u元($order\u id){
如果(isset($\u POST['wholesale\u checkbox'])){
$user\u id=get\u post\u meta($order\u id,''u customer\u user',true);//获取用户id
如果($user\u id>0){
$user=新的WP\u用户($user\u id);
$user->remove_角色(“客户”);
$user->add_角色(“批发客户”);
}
}
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试,效果良好。

太棒了!感谢堆工作一个魅力第一次尝试。希望其他人也能像我一样觉得这很有帮助。还有一个问题@LoicTheAztec-我如何才能在帐户注册页面添加具有相同功能的复选框?@jvandelaar May将向您展示WayThank@LoicTheAztec,它帮助了我正在进行的项目的下一部分。但是仍然无法使相同的复选框工作。似乎行“,$checkout->get_value('wholesale_checkbox')”打断了下面表单的其余部分。有什么想法吗?@jvandelar我已经更新了我的答案,因为在这种情况下,
$checkout->get\u value('wholesale\u checkbox')
不是真正需要的,可以替换为
'
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');
        }
    }
}