简体   繁体   English

购买时更改 WooCommerce 中的用户角色

[英]Changing user role in WooCommerce on purchase

I'm working on WooCommerce with two product types.我正在使用两种产品类型开发 WooCommerce。 I need some of them ($products_to_check) to create an specific role account (subscriber) and the rest of them another one (customer, which is the default one for WooCommerce).我需要其中的一些($products_to_check)来创建一个特定的角色帐户(订阅者),其余的另一个(客户,这是 WooCommerce 的默认帐户)。

The thing is, if I have an user who's already a costumer, I don't want him to change its role to subscriber (so I check if he's logged in, which mean he's not a new costumer and have a previous defined role).问题是,如果我有一个已经是客户的用户,我不希望他将其角色更改为订阅者(因此我检查他是否已登录,这意味着他不是新客户并且具有以前定义的角色)。 On the other hand, if a subscriber purchase one of the products not included in $products_to_check, he should get a role upgrade (from subscriber to costumer).另一方面,如果订阅者购买了 $products_to_check 中未包含的产品之一,他应该获得角色升级(从订阅者到客户)。

This is my function, which is not working but I don't really know why.这是我的功能,它不起作用,但我真的不知道为什么。

add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase' );
function change_role_on_purchase( $order_id ) {
    $order = wc_get_order( $order_id );
    $items = $order->get_items();

    $products_to_check = array( ... );

    $user = new WP_User( $order->user_id );
    $user_meta = get_userdata( $order->user_id );
    $user_roles = $user_meta->roles;

    foreach ( $items as $item ) {
        if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
                if (!is_user_logged_in()){
                        $user->set_role( 'subscriber' );
                }
        }
        elseif ( $order->user_id > 0 && !(in_array( $item['product_id'], $products_to_check)) ) {
                if (is_user_logged_in() && in_array('subscriber', $user_roles)){
                        $user->set_role( 'customer' );
                }
        }
    }
}

I'd really appreciate any help on this issue.我真的很感激在这个问题上的任何帮助。 What am I doing wrong?我做错了什么?

There are several things you need to take note here.这里有几件事你需要注意。

  1. Make sure you hook is invoked after or override the WooCommerce's or any other plugins' hooks that set user roles.确保在设置用户角色的 WooCommerce 或任何其他插件的挂钩之后调用或覆盖挂钩。
  2. Do not use your log in condition to check if users are new.不要使用您的登录条件来检查用户是否是新用户。 user_register is the hook to use for newly registered users. user_register是用于新注册用户的钩子。
  3. Your role logic loop here is also flawed.你这里的角色逻辑循环也有缺陷。 You are iterating through all product orders and attempts to set role on each iteration.您正在遍历所有产品订单并尝试在每次迭代中设置角色。 This obviously means that each iteration can change the user role if condition satisfies and can override the previous ones.这显然意味着如果条件满足,每次迭代都可以更改用户角色,并且可以覆盖之前的角色。 (Though it seems like you are hoping your logged in condition will do the trick, I think that's very error prone even if it could maybe work.) (虽然您似乎希望您的登录条件能够解决问题,但我认为即使它可以工作,也很容易出错。)
  4. The WooCommerce subscription plugin actually sets the subscriber role to users that have active subscriptions and customer role for expired subscription users. WooCommerce 订阅插件实际上将订阅者角色设置为具有活动订阅的用户和过期订阅用户的客户角色。 This is configureable but I think that's a more clear assignment.这是可配置的,但我认为这是一个更明确的分配。

So first make sure your hook triggers after or overrides the WooCommerce user role hooks.因此,首先确保您的钩子在 WooCommerce 用户角色钩子之后触发或覆盖。 And I think you can do away completely with the new or old user logic, as the only important thing is whether they purchased a product that's going to upgrade them;而且我认为你可以完全摒弃新的或旧的用户逻辑,因为唯一重要的是他们是否购买了要升级他们的产品; if not, they should have the default role.如果没有,他们应该具有默认角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM