• Hi,

    We have setup a WooCommerce product for Constable Care Club Membership, as a one off payment.

    Customers who purchase this product are then set up as a Constable Care Club Member within a custom UM role. This also allows them to access certain information that other users cannot see.

    The question I have is, is it possible to link to product purchase with automatically creating the user role for them?

    As I understand it, WooCommerce will set up all new users as Customers. We then have to manually change the roles for users who purchased the membership product to the custom UM role.

    Is there any way we can automate this?

    Or alternatively, is it possible for existing ‘Customers’ to visit the UM sign up link for the club membership, fill out the details and have their role updated from Customer to Club Member? This way we can include a link to the sign up form on the order confirmation email for the product.

    I hope this makes sense. Thanks in advance.

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Aswin Giri

    (@aswingiri)

    Hello @glideagency

    You can use this code to set a user role:
    UM()->roles()->set_role( $user_id, 'role-key' );

    You can run this line of code after the customer is created in woocommerce. Here is an example of what you can do:

    add_action( 'woocommerce_created_customer', function($customer_id, $new_customer_data, $password_generated){
     UM()->roles()->set_role( $customer_id, 'role-key' );
    }, 10, 3 ); 

    For woocommerce hooks and functions, you will have to look into woocommerce.

    Thread Starter GlideAgency

    (@glideagency)

    @aswingiri thanks for your response, and sorry for the delay in getting back to you.

    Can you just confirm, since I also need to check for the purchase of a specific product, and not all products, can that check be done in the above add_action as well?

    Or does that need to be defined elsewhere and referenced here? Are you able to confirm how I can run that check first and then apply the set_role function to only those users?

    Thanks for your help.

    Plugin Support Aswin Giri

    (@aswingiri)

    Hello @glideagency

    You can use the same hook but to know how to check if the user has purchased a product, I suggest you contact woo-commerce support as I can only answer your question related to Ultimate members.

    add_action( 'woocommerce_created_customer', function($customer_id, $new_customer_data, $password_generated){
     if(user has purchased product){ // check with woocommerce to know about this function
          UM()->roles()->set_role( $customer_id, 'role-key' );
     }
    }, 10, 3 );
    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hello @glideagency

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??

    Regards,

    Thread Starter GlideAgency

    (@glideagency)

    Hi @ultimatemembersupport after much back and forth with WooCommerce, I have finally worked out the function required to check if the user has bought a product.

    The reality is, WooCommerce can only check if an existing customer has purchased a product, and cannot see Guest purchases.

    So first I had to disable Guest checkout for this product, so the customer needs to add a Username and Password during checkout of this product.

    This has worked well, and I have now added your code to the function I created, however upon testing, it is not doing anything, and users are still being saved as Customer after purchasing this specific product.

    Here is the code below:

    function check_customer_bought_product()
    {
        global $woocommerce;
        $user_id = get_current_user_id();
        $current_user= wp_get_current_user();
        $customer_email = $current_user->email;
    
        if ( wc_customer_bought_product( $customer_email, $user_id,'14427')) {
            UM()->roles()->set_role( $customer_id, 'um_constable-care-club-member' );
            return true;
        }
    
        return false;
    }

    Please let me know your thoughts.

    Plugin Support Ultimate Member Support

    (@ultimatemembersupport)

    Hi @glideagency

    Could you please try adding this code to clear the user cache and see if this resolves the issue?

    UM()->user()->remove_cache( $user_id );

    Here’s where I added the above code with additional fixes to the wrong ID variable:

    function check_customer_bought_product()
    {
        global $woocommerce;
        $user_id = get_current_user_id();
        $current_user= wp_get_current_user();
        $customer_email = $current_user->email;
    
        if ( wc_customer_bought_product( $customer_email, $user_id,'14427')) {
            UM()->roles()->set_role( $user_id, 'um_constable-care-club-member' );      
            UM()->user()->remove_cache( $user_id );
            return true;
        }
    
        return false;
    }
    Thread Starter GlideAgency

    (@glideagency)

    Hi @ultimatemembersupport I have added this code in but the result is the same.

    The order for the product goes through fine, but the user is added with the Customer role only.

    Can you please advise?

    Thread Starter GlideAgency

    (@glideagency)

    Hi @ultimatemembersupport can you please respond to the above?

    Thanks

    @glideagency

    Do you have an add_action linking your function to Woocommerce in your last code snippet above?

    You can try this code snippet:

    add_action( 'woocommerce_created_customer', 'woocommerce_created_customer_set_um_role', 10, 3 );
    
    function woocommerce_created_customer_set_um_role( $user_id, $new_customer_data, $password_generated ) {
    
        if ( wc_customer_bought_product( $new_customer_data['user_email'], $user_id, '14427' )) {
            UM()->roles()->set_role( $user_id, 'um_constable-care-club-member' );      
            UM()->user()->remove_cache( $user_id );
        }
    }
    • This reply was modified 2 years, 5 months ago by missveronica.
    • This reply was modified 2 years, 5 months ago by missveronica.
    Thread Starter GlideAgency

    (@glideagency)

    Hi @missveronicatv, thanks for your response.

    I don’t actually have the add action section at the moment.

    I will try your code snippet and let you know if that works.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Create account upon product purchase?’ is closed to new replies.