用户登录后重定向,具体取决于其在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不保证订单已付款。它只是意味着它所说的。如果您根据未结订单更改用户角色,您将面临以下风险:付款未完成的客户可以通过更改用户角色来访问您提供的任何内容。

最新更新