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 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 6 years, 4 months ago.
Assisted by: Christian Cox.