[Resolved] Add more than one product to cart with cred form
This support ticket is created 5 years, 12 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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
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
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.
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:
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:
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.