如何根据WooCommerce用户注册的角色和状态重定向?
我需要为重定向设置几个条件:如何根据WooCommerce用户注册的角色和状态重定向?,woocommerce,Woocommerce,我需要为重定向设置几个条件: 用户购买订阅。将其添加到购物车并希望下订单。如果用户已注册,则他将发出订单。如果用户未注册,则向其提供注册,在编辑帐户时填写简短的问卷,然后返回订单结帐页面 如果这很难做到,可以在订单完成后将用户重定向到帐户编辑页面 未注册用户购买特定类别的产品(考虑类别ID),希望下订单。首先,用户被重定向到注册,然后从那里返回订单签出页面 如果用户购买订阅,则自动为其指定“订户”角色,否则保留默认角色“客户”。我有一个密码: add_action( 'woocommerce_
add_action( 'woocommerce_order_status_completed', 'woo_change_role_on_purchase' );
function woo_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = 222; // 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( 'subscriber' );
}
}
}
我在这里找到了csehasib编写的代码-
以及由LoicTheAztec编写的代码:
add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $redirection_url ){
// Change the redirection Url
$redirection_url = get_home_url(); // Home page
return $redirection_url; // Always return something
}
但考虑到用户的角色和注册的存在,我无法编写重定向
UPD:未注册用户从特定类别(考虑ID)购买产品,将其添加到购物车并进入订单处理。如果是注册的,它会下订单。如果没有,请重定向以进行注册,然后返回以进行订购
下单后,如果用户购买了某一类别的产品,请将其重定向到自定义页面“谢谢”,或立即转到个人帐户中帐户“编辑帐户”的编辑页面
原则上,对于未注册的用户,可以在购买产品时自动创建帐户。有必要增加购买某一类别产品的条件。下单后需要进行重定向
我将非常高兴能得到你的帮助 您可以通过插件实现这一点,而无需任何代码。看一看。如果您需要更多的基本功能,他们也会提供专业版。谢谢您的回答。但我不需要限制访问和成为会员。我只需要一个重定向。它能够根据用户角色处理重定向。你会在编辑页面/发布选项中看到该选项。对不起,我不需要这个插件。它不执行我需要的那些功能。
add_filter( 'woocommerce_registration_redirect', 'custom_redirection_after_registration', 10, 1 );
function custom_redirection_after_registration( $redirection_url ){
// Change the redirection Url
$redirection_url = get_home_url(); // Home page
return $redirection_url; // Always return something
}