簡體   English   中英

根據 WooCommerce 主動訂閱更改用戶角色

[英]Change user role based on WooCommerce active subscription

我有一個 Woocommerce 商店,其中包含可變訂閱和 3 種訂閱變體:訂閱-a、訂閱-b 和訂閱-c。 我還添加了 3 種新類型的用戶角色:訂閱者-a、訂閱者-b 和訂閱者-c。

我正在嘗試創建一個 function,當客戶購買、升級或降級訂閱並且訂閱處於活動狀態時,並行用戶角色將分配給該客戶。

例如:如果客戶購買訂閱-a,一旦激活,用戶角色訂閱者-a 將被分配。 如果客戶從訂閱 b 升級到訂閱 c,一旦激活,用戶角色將有機會從訂閱者 b 到訂閱者 c。

我嘗試了類似線程中提供的 function 但它對我不起作用: Woocommerce change user role on purchase

function change_role_on_purchase( $order_id ) {

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {
        $product_name = $item['name'];
        $product_id = $item['product_id'];
        $product_variation_id = $item['variation_id'];

        if ( $order->user_id > 0 && $product_variation_id == '416' ) {
            update_user_meta( $order->user_id, 'paying_customer', 1 );
            $user = new WP_User( $order->user_id );

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

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

add_action( 'woocommerce_subscription_status_active', 'change_role_on_purchase' );

更新: @LoicTheAztec 的解決方案效果很好。 我現在正在嘗試在取消訂閱時刪除角色。 這就是我得到的,不確定這是否是最優雅的方式,但它有效。

add_action('woocommerce_subscription_status_cancelled', 'cancelled_subscription_remove_role', 10, 1);

function cancelled_subscription_remove_role($subscription) {
    $user_id = $subscription->get_user_id();
    $user = new WP_User($user_id);
    $user->remove_role('subscriber_a');
    $user->remove_role('subscriber_b');
    $user->remove_role('subscriber_c');
}

更新以處理切換訂閱(升級或降級訂閱)

您可以嘗試使用woocommerce_subscription_status_updated相關掛鈎。在下面的代碼中,您將為每個變體 ID 設置相關的用戶角色在設置數組中(注釋代碼)

// Custom function for your settings - Variation id per user role
function variation_id_per_user_role_settings(){
    // Settings: set the variation ID as key with the related user role as value
    return array(
        '417'  => 'subscriber-a',
        '418'  => 'subscriber-b',
        '419'  => 'subscriber-c',
    );
}

// Custom function that check item and change user role based on variation ID
function check_order_item_and_change_user_role( $item, $user, $settings ){
    $product = $item->get_product(); // The product object
    
    // Only for variation subscriptions
    if( $product->is_type('subscription_variation') ) {
        $variation_id = $item->get_variation_id(); // the variation ID
        $user_role    = $settings[$variation_id]; // The right user role for the current variation ID
        
        // If current user role doesn't match with the right role
        if( ! in_array( $user_role, $user->roles) ) {
            // Remove "subscriber" user role (if it is set)
            if( in_array('subscriber', $user->roles) ) {
                $user->remove_role( 'subscriber' );
            }

            // Remove other user roles (if they are set)
            foreach ( $settings as $key_id => $value_role ) {
                if( in_array($value_role, $user->roles) && $user_role !== $value_role ) {
                    $user->remove_role( $value_role );
                }
            }

            // Set the right user role (if it is not set yet)
            $user->set_role( $user_role );
        }
    }
}

// On first purchase (if needed)
add_action( 'woocommerce_subscription_status_updated', 'active_subscription_change_user_role', 100, 3 );
function active_subscription_change_user_role( $subscription, $new_status, $old_status ) {
    // When subscrition status is updated to "active"
    if ( $new_status === 'active' ) {
        // Get the WC_Order Object from subscription
        $order = wc_get_order( $subscription->get_parent_id() );

        // Get an instance of the customer WP_User Object
        $user = $order->get_user();

        // Check that it's not a guest customer
        if( is_a( $user, 'WP_User' ) && $user->ID > 0 ) {
            // Load settings
            $settings = variation_id_per_user_role_settings();

            // Loop through order items
            foreach ( $subscription->get_items() as $item ) {
                check_order_item_and_change_user_role( $item, $user, $settings );
            }
        }
    }
}

// On switched purchased subscription
add_action( 'woocommerce_order_status_changed', 'switched_subscription_change_user_role_on_order_status_change', 100, 4 );
function switched_subscription_change_user_role_on_order_status_change( $order_id, $old_status, $new_status, $order ) {
    // When order status is updated to 'processing' or 'completed' status
    if ( in_array( $new_status, array('processing','completed') ) ) {
        // Get an instance of the customer WP_User Object
        $user = $order->get_user();
    
        // Check that it's not a guest customer
        if( is_a( $user, 'WP_User' ) && $user->ID > 0 ) {
            // Load settings
            $settings = variation_id_per_user_role_settings();
    
            // Loop through order items
            foreach ( $order->get_items() as $item ) {
                check_order_item_and_change_user_role( $item, $user, $settings );
            }
        }
    }
}

代碼進入您的活動子主題(或活動主題)的functions.php 文件。 它可以工作。


或者您也可以嘗試使用woocommerce_subscription_status_active掛鈎:

// Custom function for your settings - Variation id per user role
function variation_id_per_user_role_settings(){
    // Settings: set the variation ID as key with the related user role as value
    return array(
        '417'  => 'subscriber-a',
        '418'  => 'subscriber-b',
        '419'  => 'subscriber-c',
    );
}

// Custom function that check item and change user role based on variation ID
function check_order_item_and_change_user_role( $item, $user, $settings ){
    $product = $item->get_product(); // The product object

    // Only for variation subscriptions
    if( $product->is_type('subscription_variation') ) {
        $variation_id = $item->get_variation_id(); // the variation ID
        $user_role    = $settings[$variation_id]; // The right user role for the current variation ID

        // If current user role doesn't match with the right role
        if( ! in_array( $user_role, $user->roles) ) {
            // Remove "subscriber" user role (if it is set)
            if( in_array('subscriber', $user->roles) ) {
                $user->remove_role( 'subscriber' );
            }

            // Remove other user roles (if they are set)
            foreach ( $settings as $key_id => $value_role ) {
                if( in_array($value_role, $user->roles) && $user_role !== $value_role ) {
                    $user->remove_role( $value_role );
                }
            }

            // Set the right user role (if it is not set yet)
            $user->set_role( $user_role );
        }
    }
}

// On first purchase (if needed)
add_action( 'woocommerce_subscription_status_active', 'active_subscription_change_user_role', 100 );
function active_subscription_change_user_role( $subscription ) {
    // Get the WC_Order Object from subscription
    $order = wc_get_order( $subscription->get_parent_id() );

    // Get an instance of the customer WP_User Object
    $user = $order->get_user();

    // Check that it's not a guest customer
    if( is_a( $user, 'WP_User' ) && $user->ID > 0 ) {
        // Load settings
        $settings = variation_id_per_user_role_settings();

        // Loop through order items
        foreach ( $subscription->get_items() as $item ) {
            check_order_item_and_change_user_role( $item, $user, $settings );
        }
    }
}

// On switched purchased subscription
add_action( 'woocommerce_order_status_changed', 'switched_subscription_change_user_role_on_order_status_change', 100, 4 );
function switched_subscription_change_user_role_on_order_status_change( $order_id, $old_status, $new_status, $order ) {
    // When order status is updated to 'processing' or 'completed' status
    if ( in_array( $new_status, array('processing','completed') ) ) {
        // Get an instance of the customer WP_User Object
        $user = $order->get_user();

        // Check that it's not a guest customer
        if( is_a( $user, 'WP_User' ) && $user->ID > 0 ) {
            // Load settings
            $settings = variation_id_per_user_role_settings();

            // Loop through order items
            foreach ( $order->get_items() as $item ) {
                check_order_item_and_change_user_role( $item, $user, $settings );
            }
        }
    }
}

代碼進入您的活動子主題(或活動主題)的functions.php 文件。 它應該有效。

文檔: 訂閱操作掛鈎參考

來自LoicTheAztec的位更新代碼。 可與訂閱中的不同產品一起使用。

<?php
// Custom function for your settings - Variation id per user role
function variation_id_per_user_role_settings()
{
    // Settings: set the variation ID as key with the related user role as value
    return array(
        //monthly subscriptions
        '530'  => 'subscriber',
        '740'  => 'subscriber-extended',
        '741'  => 'subscriber-advanced',
        //now to the yearly
        '536'  => 'subscriber',
        '739'  => 'subscriber-extended',
        '738'  => 'subscriber-advanced',
    );
}

// Custom function that check item and change user role based on variation ID
function check_order_item_and_change_user_role($item, $user, $settings)
{
    $product = $item->get_product(); // The product object

    // Only for variation subscriptions
    $variation_id = $item['product_id']; // the variation ID
    $user_role    = $settings[$variation_id]; // The right user role for the current variation ID

    // If current user role doesn't match with the right role
    if (!in_array($user_role, $user->roles)) {
        // Remove "subscriber" user role (if it is set)
        if (in_array('subscriber', $user->roles)) {
            $user->remove_role('subscriber');
        }

        // Remove other user roles (if they are set)
        foreach ($settings as $key_id => $value_role) {
            if (in_array($value_role, $user->roles) && $user_role !== $value_role) {
                $user->remove_role($value_role);
            }
        }

        // Set the right user role (if it is not set yet)
        $user->set_role($user_role);
    }
}

// On first purchase (if needed)
add_action('woocommerce_subscription_status_updated', 'active_subscription_change_user_role', 100, 3);
function active_subscription_change_user_role($subscription, $new_status, $old_status)
{
    // When subscrition status is updated to "active"
    if ($new_status === 'active') {
        // Get the WC_Order Object from subscription
        $order = wc_get_order($subscription->get_parent_id());

        // Get an instance of the customer WP_User Object
        $user = $order->get_user();

        // Check that it's not a guest customer
        if (is_a($user, 'WP_User') && $user->ID > 0) {
            // Load settings
            $settings = variation_id_per_user_role_settings();

            // Loop through order items
            foreach ($subscription->get_items() as $item) {
                check_order_item_and_change_user_role($item, $user, $settings);
            }
        }
    }
}

// On switched purchased subscription
add_action('woocommerce_order_status_changed', 'switched_subscription_change_user_role_on_order_status_change', 100, 4);
function switched_subscription_change_user_role_on_order_status_change($order_id, $old_status, $new_status, $order)
{
    // When order status is updated to 'processing' or 'completed' status
    if (in_array($new_status, array('processing', 'completed'))) {
        // Get an instance of the customer WP_User Object
        $user = $order->get_user();

        // Check that it's not a guest customer
        if (is_a($user, 'WP_User') && $user->ID > 0) {
            // Load settings
            $settings = variation_id_per_user_role_settings();

            // Loop through order items
            foreach ($order->get_items() as $item) {
                check_order_item_and_change_user_role($item, $user, $settings);
            }
        }
    }
}

//Remove subscriber role if subscription is not active
add_action('woocommerce_subscription_status_updated', 'switched_subscription_change_user_role_on_order_status_change_inactive', 100, 5);
function switched_subscription_change_user_role_on_order_status_change_inactive($subscription, $new_status, $old_status)
{
    if ($new_status !== 'active') {
        // Get an instance of the customer WP_User Object
        $order = wc_get_order($subscription->get_parent_id());
        $user = $order->get_user();
        // Check that it's not a guest customer
        if (is_a($user, 'WP_User') && $user->ID > 0) {
            $settings = variation_id_per_user_role_settings();
            foreach ($settings as $key_id => $value_role) {
                if (in_array($value_role, $user->roles)) {
                    $user->remove_role($value_role);
                }
            }
            $user->set_role('customer');
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM