【问题标题】:Change user role if checkout custom checkbox is checked in WooCommerce如果在 WooCommerce 中选中结帐自定义复选框,则更改用户角色
【发布时间】:2018-08-14 02:36:39
【问题描述】:

我正在尝试向我的 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_customer”而不是“客户”。

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

当我删除wholesale_checkbox if 语句时,上述功能通过将客户保存为“wholesale_customer”来工作。但如果包含 if 语句,它总是将角色保存为“客户”。

我哪里错了?干杯

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce user-roles


    【解决方案1】:

    这是正确的做法:

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 太棒了!谢谢堆工作的魅力第一次尝试。希望其他人会发现这和我一样有用。
    • 还有一个问题@LoicTheAztec - 如何在帐户注册页面添加具有相同功能的相同复选框?
    • @jvandelaar 可能是this answer 会告诉你方法
    • 感谢@LoicTheAztec,它帮助我完成了项目的下一部分。但是仍然无法使相同的复选框起作用。似乎 ", $checkout->get_value('wholesale_checkbox')" 行破坏了下面的表格的其余部分。任何想法为什么会这样?
    • @jvandelaar 我已经更新了我的答案,因为在这种情况下实际上不需要$checkout-&gt;get_value('wholesale_checkbox'),可以用''替换
    猜你喜欢
    • 2018-06-17
    • 1970-01-01
    • 2017-01-08
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2013-08-30
    • 2023-03-23
    相关资源
    最近更新 更多