Skip Navigation

[Resolved] Add more than one product to cart with cred form

This support ticket is created 6 years 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Tim Elliott 6 years ago.

Assisted by: Nigel.

Author
Posts
#1154749

Tell us what you are trying to do?
I have a post form that creates a new post when submitted. This form adds a product to the cart using the "The form specifies the product according to the value of this custom field" option. This works OK.

I want the user to also be able to select another option for the product - I had a look at using a variable product but that doesn't seem easy to do.

So I've created another select field that has product ids as the values. So when the form is submitted I want the product with id the same as the value of this select to be added to the cart as well. Could you give me a hand with what I need to do to achieve this. I think I will need to use:
cred_submit_complete
wc_add_order_item

This is the select html (1426, 1427, 1428 are the product ids):
<select id="cred_form_844_1-select-7-1543411137" class="form-control wpt-form-select form-select select" output="bootstrap" preset_value="" urlparam="" value_escape="" make_readonly="" placeholder="" select_text="" data-wpt-type="select" name="wpcf-display-ads-select">
<option value="0" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_844_1_cred_form_844_1-select-7-1543411137" data-wpt-name="wpcf-display-ads-select" selected="selected">No Display Ads</option>
<option value="1426" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_844_1_cred_form_844_1-select-7-1543411137" data-wpt-name="wpcf-display-ads-select">Display Ads: 7 Days</option>
<option value="1427" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_844_1_cred_form_844_1-select-7-1543411137" data-wpt-name="wpcf-display-ads-select">Display Ads: 14 Days</option>
<option value="1428" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-id="cred_form_844_1_cred_form_844_1-select-7-1543411137" data-wpt-name="wpcf-display-ads-select">Display Ads: 30 Days</option>
</select>

Is there any documentation that you are following?
I've searched the forums for similar and found this:
https://toolset.com/forums/topic/buy-more-then-1-product-with-cred-commerce/
This says you were looking at making adding multiple products possible back in 2016 but I guess it hasn't happened yet.

Is there a similar example that we can see?
No

What is the link to your site?
It's hidden at the moment. I can give you access if necessary.

#1154772

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2018-11-28 at 15.59.57.png

Hi Tim

"when the form is submitted I want the product with id the same as the value of this select to be added to the cart as well"

It has to be "as well", not "instead of"?

i.e. multiple products?

I'm guessing you are aware of the option to specify the product that gets added according to the value of a field in the form?

#1154804

Hi Nigel

Yes, "as well". I have already set "The form specifies the product according to the value of this custom field" to use a different field in the form.

I need to add additional products if the user selects them.

Initially I had 2 options for the user. It's purchasing a job listing so we have selects for:
Job Listing Length: 7, 14, 30, 45 days
Featured Listing: 0, 7, 14 days

I have created a product for each of the 11 available combinations
I also created another hidden field with the 11 available combinations, and the value as the product id:
Job 7 : Featured 0
Job 14 : Featured 0
Job 30 : Featured 0
Job 45 : Featured 0
Job 7 : Featured 7
Job 14 : Featured 7
Job 30 : Featured 7
Job 45 : Featured 7
Job 14 : Featured 14
Job 30 : Featured 14
Job 45 : Featured 14

I use jQuery to select items in a hidden field depending on the values of the 2 visible fields to get a product ID and the cred form adds one of these 11 products to the cart when the form is submitted.

Now my client wants them to be able to select and pay extra for another 2 options as well.
Display ads: 0, 7, 14, 30 days
Newsletter: No, Yes

I could extend the same method to cover all four dropdowns, but this would mean creating about 80 products and the js to select them would be a bit of a nightmare.

I thought I should just be able to add an additional product to the cart depending on the value of each of the other 2 dropdowns.

So the process would be something like:
-when form is submitted
-Check the form id to only do it for this form
-prodid3 = value of dropdown 3
-prodid4 = value of dropdown 4
-If prodid3 != 0 Add prodid3 to the cart using woocommerce hook
-If prodid4 != 0 Add prodid4 to the cart using woocommerce hook

Does that make it clearer?
Thanks for your help
Tim

#1154807
Job submit form.jpg

Hi Nigel,

This image might help.

Tim

#1155303

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tim

I did a little testing and you should be able to use the cred_commerce_after_add_to_cart hook to run code which will use the WooCommerce add_to_cart method to add the relevant additional products to the cart.

So the cred_commerce_after_add_to_cart hook will run when you submit the form and the form adds the indicated product to the cart.

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

I dumped the two available arguments to the error log, as well as the $_POST object to see if it still contained details of the form submission, which it does:

[29-Nov-2018 09:18:05 UTC] *** cred_commerce_after_add_to_cart ***
[29-Nov-2018 09:18:05 UTC] *** $form_id ***
[29-Nov-2018 09:18:05 UTC] 18
[29-Nov-2018 09:18:05 UTC] *** $post_id ***
[29-Nov-2018 09:18:05 UTC] 83
[29-Nov-2018 09:18:05 UTC] _POST: Array
(
    [wpcf-short-message] => Boom
    [wpcf-adults] => 3
    [form_submit_1] => Submit
    [_cred_cred_wpnonce_cred_form_18] => 9587551e51
    [_cred_cred_prefix_post_id] => 83
    [_cred_cred_prefix_cred_container_id] => 19
    [_cred_cred_prefix_form_id] => 18
    [_cred_cred_prefix_form_count] => 1
)

So you have the post_id of the post that was just created by the form submission, and if the form saves the various fields as post meta you can retrieve them using get_post_meta.

But you shouldn't need to, because they are available right there in the $_POST object (even if you are not saving the fields to post_meta).

So you can perform whatever logic you need to determine which extra products to add to the cart.

I found this tutorial which offers a good explanation of that:

hidden link

#1155337

Hi Nigel

Thanks for your help. I thought I marked this as resolved yesterday but my post obviously didn't submit...

I solved it using this function:

add_action('cred_submit_complete', 'my_success_action',10,2);
function my_success_action($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==844)
    {
		global $woocommerce;
        // get the value of the id in the display ads and newsletter fields
		$featured_product = get_post_meta( $post_id, 'wpcf-featured-select', true );
		$display_product = get_post_meta( $post_id, 'wpcf-display-ads-select', true );
		$newsletter_product = get_post_meta( $post_id, 'wpcf-newsletter-select', true );
		if ( $featured_product != 0 ) {
			// add product to cart
			$woocommerce->cart->add_to_cart($featured_product);
		}
		if ( $display_product != 0 ) {
			// add product to cart
			$woocommerce->cart->add_to_cart($display_product);
		}
		if ( $newsletter_product != 0 ) {
			// add product to cart
			$woocommerce->cart->add_to_cart($newsletter_product);
		}
    }
}

So using cred_submit_complete meant that the values for the extra dropdowns had been saved to the new product and I could just pull them out using get_post_meta and add to cart.