【问题标题】:Changing user role after purchasing a products in WooCommerce在 WooCommerce 中购买产品后更改用户角色
【发布时间】:2019-12-29 13:54:59
【问题描述】:

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

    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 会继续创建“客户”用户。 我的代码有问题吗?

【问题讨论】:

    标签: php wordpress woocommerce product user-roles


    【解决方案1】:

    更新

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

    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 文件中。它现在应该可以工作了。


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

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

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

    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 文件中。这也应该有效,将两个功能合并为一个。

    【讨论】:

    • 感谢您的回答。我尝试了代码,但系统仍将新用户注册为“客户”,而不是按所需角色注册。我使用“会员”插件创建了用户角色“黄金”和“白金”。还有什么问题?
    • 我正在使用此代码自动完成订单:/** * 自动完成所有 WooCommerce 订单。 */ add_action('woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');功能 custom_woocommerce_auto_complete_order( $order_id ) { if ( ! $order_id ) { return; } $order = wc_get_order( $order_id ); $order->update_status('完成'); }
    • @Jessica217 对不起,我的错,我的代码有一些错误……我已经更新了我的答案并添加了第二个替代方案。试试吧,我希望它会工作。
    • 再次感谢。不幸的是,由于某种原因它没有工作。 WP 仅以“客户”身份注册黄金和白金用户角色的新购买
    • @Jessica217 我不能做更多,因为我无法对其进行真实测试。
    猜你喜欢
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2019-01-12
    • 2018-04-10
    • 2021-02-01
    • 1970-01-01
    • 2014-11-15
    • 2015-06-21
    相关资源
    最近更新 更多