在 WooCommerce 中购买产品后更改用户角色 [英] Changing user role after purchasing a products in WooCommerce

查看:61
本文介绍了在 WooCommerce 中购买产品后更改用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的网站上建立两个计划(付费).如果用户购买黄金计划,它应该创建一个用户(角色)黄金并给他 20% 的旅行套餐折扣.如果用户购买白金 wp 应为该客户创建白金"用户角色.现在我在网上找到了代码,但它不起作用:

I have a need to build two plans (paid) on my site. If the user buys Gold Plan it should create a user (role) Gold and give him 20% discount on travel packages. If user buys platinum wp should create 'Platinum' user role for that customer. Now I have found the code online but it does not work:

    add_action( 'woocommerce_order_status_completed', 
    'wpglorify_change_role_on_purchase' );

    function wpglorify_change_role_on_purchase( $order_id ) {

    // get order object and items
    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    $product_id = 85; // that's a specific product ID

    foreach ( $items as $item ) {

            if( $product_id == $item['product_id'] && $order->user_id ) {
                $user = new WP_User( $order->user_id );

                // Remove role
                $user->remove_role( 'customer' ); 

                // Add role
                $user->add_role( 'gold' );
            }

    }

    $product_id = 86; // that's a specific product ID

    foreach ( $items as $item ) {

            if( $product_id == $item['product_id'] && $order->user_id ) {
               $user = new WP_User( $order->user_id );

                // Remove role
                $user->remove_role( 'customer' ); 

                // Add role
                $user->add_role( 'platinum' );
            }

    }

现在我已将此代码放在当前活动(子)主题的 function.php 文件中,但是当我测试它并购买产品时,wordpress 继续创建客户"用户.我的代码有问题吗?

Now I have put this code in function.php file of a current active (child) theme but when I test it and buy the product wordpress keeps on creating 'customer' user. Is something wrong with my code?

推荐答案

更新

您的代码已过时且存在一些错误.尝试以下操作,这将在订单完成时根据购买的产品更改用户角色(已完成"状态):

Your code is outdated and with some mistakes. Try the following, that will change the user role based on purchased product when order is completed ("completed" status):

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase', 10, 2 );
function wpglorify_change_role_on_purchase( $order_id, $order ) {
    $gold_product_id      = 85; // specific product ID for "gold" user role
    $platinium_product_id = 86; // specific product ID for "platinium" user role

    if( $user_id = $order->get_customer_id() ) {
        // Get the WP_User Object
        $wp_user = new WP_User( $user_id );

        foreach ( $order->get_items() as $item ) {

            // For "gold" user role
            if ( $gold_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
                $user->remove_role( 'customer' ); // Remove 'customer' user role
                $user->add_role( 'gold' ); // Add 'gold' user role
            }

            // For "platinum" user role
            elseif ( $platinium_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
                $user->remove_role( 'customer' ); // Remove 'customer' user role
                $user->add_role( 'platinum' ); // Add 'platinum' user role
            }
        }
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.现在应该可以使用了.

Code goes in function.php file of your active child theme (or active theme). It should work now.

更新:当您使用以下代码自动完成订单时:

Update: As you are using the following code to autocomplete orders:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) { 
        return; 
    } 
    $order = wc_get_order( $order_id ); 
    $order->update_status( 'completed' ); 
}

您可以在其中包含基于特定产品的用户角色更改.所以试试下面的代码将替换你现有的函数:

You can include in it the user role change based on specific products. So try the following code will replace your existing function:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) { 
        return; 
    } 

    // Get an instance of the WC_Order Object
    $order = wc_get_order( $order_id ); 

    // Only for logged in "customer" user role
    if ( current_user_can( 'customer' ) ) {
        $gold_product_id      = 85; // specific product ID for "gold" user role
        $platinium_product_id = 86; // specific product ID for "platinium" user role


        $user_id = $order->get_customer_id(); // The user Id

        // Get the WP_User Object
        $wp_user = new WP_User( $user_id );

        foreach ( $order->get_items() as $item ) {

            // For "gold" user role
            if ( $gold_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
                $user->remove_role( 'customer' ); // Remove 'customer' user role
                $user->add_role( 'gold' ); // Add 'gold' user role
            }

            // For "platinum" user role
            elseif ( $platinium_product_id == $item->get_product_id() && $order->get_user_id() > 0 ) {
                $user->remove_role( 'customer' ); // Remove 'customer' user role
                $user->add_role( 'platinum' ); // Add 'platinum' user role
            }
        }
    }
    $order->update_status( 'completed' ); 
}

代码位于活动子主题(或活动主题)的 function.php 文件中.这也应该有效,将两个功能合二为一.

Code goes in function.php file of your active child theme (or active theme). This should also work, merging both functions in one.

这篇关于在 WooCommerce 中购买产品后更改用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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