Skip Navigation

[Resolved] User form linked to CRED Commerce User Role

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1339657

Hi there,

I have a CRED User form. This has Commerce enabled (via the check box). It is linked to a product in Woocommerce.

The user fills out the user form, then completes the related purchase.

The problem I have is that WordPress is ignoring the User Role defined in the form and on checkout is assigning the new user as "Customer" rather than "Custom Role".

I need a solution that will allow different user roles to be assigned depending on which form they use.

Many thanks

Paul

#1339825

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you try to use the Toolset Forms commerce hook "cred_commerce_after_order_completed" and set the required role you wanted to current user:
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

#1340869

Please could you provide the code needed for the hook . I imagine it's fairly simple but not sure myself?

Many thanks

#1340991

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please try to use the following code and add it to your current theme's functions.php file and try to resolve your issue;

add_action('cred_commerce_after_order_completed', 'func_update_user_role_custom',10,1);
  function func_update_user_role_custom( $data ) {
    if ( isset($data['extra_data'][0]['cred_form_id']) && $data['extra_data'][0]['cred_form_id'] == 9999) {
        $user_id = $data['user_id'];
        $user = get_userdata( $user_id );
        $user->set_role( 'role-slug' );
    }
}

where:
- Please change 9999 with your original Form ID
- please change role-slug with your original role you wanted to assign

More info:
=> https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

#1341033

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Please feel free to mark resolve this ticket 🙂

#1341377

This creates the folowing error. Is this a BUG with CRED Commerce?

[16-Sep-2019 15:34:50 UTC] PHP Notice: Object of class WP_Error could not be converted to int in /home/silicondsdemo/public_html/wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/models/UserForms.php on line 487
[16-Sep-2019 15:34:50 UTC] PHP Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php:52
Stack trace:
#0 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/event/base.php(36): CRED_Commerce_Forms_Meta_Handler->get_forms_meta_data('cred_meta')
#1 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/event/onorderchange.php(11): CRED_Commerce_Event_Base->execute()
#2 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/Form_Handler.php(213): CRED_Commerce_Event_OnOrderChange->execute()
#3 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/Plugin_Base.php(47): CRED_Commerce_Form_Handler->onOrderChange(Array)
#4 /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/plugins/woocommerce/CRED_Woocommerce.php(195): CRED_Commerce_Plugin_Base->dispatch('_cred_order_sta...', Array)
#5 /home/silicondsdemo/public_html/wp-includes/class-wp-hook. in /home/silicondsdemo/public_html/wp-content/plugins/cred-commerce/classes/CRED_Commerce_Forms_Meta_Handler.php on line 52

#1341681

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - but I need to check on your install why its showing such error. Can you please share problem URL and access details.

I have set the next reply to private which means only you and I have access to it.