dongyan8929 2018-03-06 09:27
浏览 89
已采纳

如果在WooCommerce中选中了checkout custom复选框,则更改用户角色

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

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

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".

Where am I going wrong? Cheers

  • 写回答

1条回答 默认 最新

  • dongqiabei7682 2018-03-06 11:10
    关注

    Here is the correct way to do it:

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

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算