繁体   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