Skip Navigation

[Resolved] Toolset commerce: product depending on userrole

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Commerce Form that should add different Products to the User's cart based on their assigned role on the site. One role should receive a free Product, and the other role should receive a paid Product.

Solution: In the Form editor, choose the option "Always use the same product regardless of inputs". Select the paid product here to use as the default. Then add this custom code to your child theme's functions.php file:

add_filter( 'cred_commerce_add_product_to_cart', 'different_product_by_role', 10, 3 );
function different_product_by_role( $product_id, $form_id, $post_id ) {
    global $current_user;
  $user_roles = $current_user->roles;
  if ( in_array('klant_abonnement', (array) $user_roles) && $form_id == 12345 ) {
        $product_id = 45678;
    }
    return $product_id;
}

Replace 12345 with the numeric ID of this Commerce Form, and replace 45678 with the numeric ID of the free Product for klant_abonnement Users.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart

This support ticket is created 6 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by martijnH 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#963533
Edit Post Form ‹ Recrahome — WordPress - Google Chrome.jpg

I have a Toolset commerce CRED form, with one Woocommerce product assigned. This is al working well.

Now I would like to make submitting the form for free, depending on the (logged in) userrole. So I created another product (with a price of €0) and would like to let the product depend on the userrole (userrole 'klant_abonnement' = free, userrole 'klant_incidenteel' = €70).

I don't have a clue how to do this. Already tried some things with generic fields, but I can't even select a custom field (see screenshot).

Thanks for your help!
Martijn Hoogma

#1003295

Hi, this will require a bit of custom code using our Commerce API function cred_commerce_add_product_to_cart. I can help you implement it. In the Form editor, choose the option "Always use the same product regardless of inputs". Select the paid product here to use as the default. Then add this custom code to your child theme's functions.php file:

add_filter( 'cred_commerce_add_product_to_cart', 'different_product_by_role', 10, 3 );
function different_product_by_role( $product_id, $form_id, $post_id ) {
    global $current_user;
  $user_roles = $current_user->roles;
  if ( in_array('klant_abonnement', (array) $user_roles) && $form_id == 12345 ) {
        $product_id = 45678;
    }
    return $product_id;
}

Replace 12345 with the numeric ID of this Commerce Form, and replace 45678 with the numeric ID of the free Product for klant_abonnement Users.

https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart

#1069125

Thanks Christain. Problem solved...