Problem: I would like to use Forms Commerce to programmatically add different products to the User's cart depending on the location of the Form they submit.
Solution: Choose the option "Always this product, regardless of inputs" and select any product. Then add a generic hidden field to the Form. Set the default value of the hidden field to be the current post ID, like this:
[cred_generic_field field='current-post-id' type='hidden' class='' urlparam=''] { "required":0, "validate_format":0, "default":"[wpv-post-id id='$current_page']" } [/cred_generic_field]
Then you can use custom code with the Forms Commerce API to manipulate the product by ID.
add_filter( 'cred_commerce_add_product_to_cart', 'which_product_hook', 10, 3 ); function which_product_hook( $product_id, $form_id, $post_id ) { if ( $form_id == 123 && isset($_POST['current-post-id']) ) { $product_id = 456; // or use the toolset_get_related_post API to get the parent / grandparent of the current post } return $product_id; }
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_add_product_to_cart
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
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, 1 month ago.
Assisted by: Christian Cox.