簡體   English   中英

登錄后用戶重定向,具體取決於其在WooCommerce中的角色

[英]User redirect after login, depending on its role in WooCommerce

在Woocommerce中,如果用戶購買某種產品,我會使用代碼來改變角色。

add_action( 'woocommerce_order_status_processing', 'change_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_id == '3422, 3423, 3424' ) {
        update_user_meta( $order->user_id, 'paying_customer', 1 );
        $user = new WP_User( $order->user_id );

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

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

登錄到網站后,如果用戶“訂閱者”並且購物車中有某個“訂閱”產品,我需要將系統重定向到結帳頁面。

我很高興你的幫助!


更新:我將保留此變體。 它可以幫助其他用戶。

add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect($redirect_to, $request, $user)
{

global $woocommerce;
$items = $woocommerce->cart->get_cart();
$ids_to_check = array(3422, 3423, 3424);

foreach ($items as $item => $values) {
$product_id = wc_get_product($values['data']->get_id());

if (in_array($product_id, $id_to_check)) {
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (in_array('subscriber', $user->roles)) {
            // redirect them to another URL, in this case, the homepage
            $url = get_permalink(get_option('woocommerce_checkout_page_id'));

            $redirect_to = $url;
        } 
    }

}

}

return $redirect_to;
}

你的if statement永遠不會返回true,因為你檢查產品id是否等於'3422, 3423, 3424'你的產品永遠不會等於你需要做的就是將這些id存儲在數組中並檢查是否數組中的產品ID,如果是,則執行您的代碼。

嘗試以下方法:

add_action('woocommerce_order_status_processing', 'change_role_on_purchase');
function change_role_on_purchase($order_id)
{

    $order = new WC_Order($order_id);
    $items = $order->get_items();
    $ids_to_check = array(3422, 3423, 3424, 15);
    foreach ($items as $item) {
        $product_name = $item['name'];
        $product_id = $item['product_id'];
        $product_variation_id = $item['variation_id'];


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

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

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

        }

    }

}

要在用戶具有特定角色時重定向用戶,如果您使用的是Woocommerce登錄頁面,則可以使用以下內容:

add_filter('woocommerce_login_redirect', 'my_login_redirect', 20, 2);


function my_login_redirect($redirect, $user)
{

    if (in_array('subscriber', $user->roles)) {
        // redirect them to another URL, in this case, the homepage

        $url = get_permalink(get_option('woocommerce_checkout_page_id'));

        $redirect = $url;

    }

    return $redirect;

}

如果您使用的是WordPress默認登錄表單,則可以使用以下功能:

add_filter('login_redirect', 'my_login_redirect', 20, 3);


function my_login_redirect($redirect_to, $request, $user)
{
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (in_array('subscriber', $user->roles)) {
            // redirect them to another URL, in this case, the homepage
            $url = get_permalink(get_option('woocommerce_checkout_page_id'));

            $redirect_to = $url;
        }
    }

    return $redirect_to;
}

請注意, woocommerce_order_status_processing不保證訂單已付款。 它只是意味着它所說的。 如果您根據未結訂單更改用戶角色,則可以通過更改用戶角色來確保未付款的客戶可以訪問您提供的任何內容。

暫無
暫無

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

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