Skip Navigation

[Resolved] Product Selection WooCommerce and Forms

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

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

100% of people find this useful.

This support ticket is created 6 years, 1 month 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 sarahK-2 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1166788

I am looking at the form options under "product to buy when selecting this form".

The options are: "Always this product, regardless of inputs"
and: "The form specifies the product according to the value of this custom field"

I want it to select the product automatically for the Client based on the parent (or a grandparent) of the post where the form is inserted. Is this possible?

Thanks in advance

#1167260

Hi, you should choose the option "Always this product, regardless of inputs". 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;
}

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

I can help if you get stuck on any of the API code.

#1167391

As usual, you guys are amazing. I won't get to this for a few days, but this looks very helpful and makes sense.

Thank you so much, I'll let you know if I need additional help when I get back into it.