Skip Navigation

[Resolved] cred commerce, I need form submit with product variations

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

Problem: I have a CRED Commerce form that includes a select field where Users can choose a custom variation option. I would like to use that custom variation option to determine which product should be added to the User's cart.

Solution: First you need to modify the generic select field so that each option has a unique value.

Then, set up a custom field on the Listing post type that will hold the value of the Listing Option selection. Add that field to the CRED form, and hide it using CSS. Write custom JavaScript to copy the value from the Listing Option field into this hidden field before the form is submitted. Then you can access the Listing Option value in your cred_commerce_add_product_to_cart callback and do whatever you need to do with it to determine which product should be added to the cart. For example, this line in the code above will let you access the custom field value:

$pid = get_post_meta($post_id, 'wpcf-your-hidden-field-slug',true); // get the value of the Listing Option field

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/

This support ticket is created 6 years, 7 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.

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 5 replies, has 2 voices.

Last updated by Christian Cox 6 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#804959

Tell us what you are trying to do?
I thought I had this resolved but the content was not being saved. https://toolset.com/forums/topic/adding-jqueary-to-content-template-doesnt-work-but-does-when-i-install-on-theme/ I can make the button disappear, but not submit both content and woocomerce variation selection for the product.

I would like my user to fill out the Cred Form with the content needed for the page. On that same page I have woocomerce product variable that they need to collect in order to get the price correct.

What is the link to your site?
hidden link

#806468

Can you share all the JavaScript and PHP code you're using? Can you tell me where the WooCommerce Variation option is in the form?

#806686

I pulled out the JavaScript since it wasn't working.

I'm using woocommerce views to pull the product variations underneath.

That page is built using a custom content template.

I can make you a login, so you can see what's going on internally.

#806690

Also is it possible to have their woocommerce order contain a link with their listing for associate the two?

#806760

Private reply fields are enabled here.

#807024

This code in your theme's functions.php file looks like it was written to work by finding the Product parent of the Listing post being created by CRED:

add_filter('cred_commerce_add_product_to_cart','change_product_to_cart',10,3);
function change_product_to_cart($product_id, $form_id, $post_id) {
    if($form_id == 198){
        $pid = get_post_meta($post_id, '_wpcf_belongs_product_id',true); // get the parent product ID
        if($pid){
            $product_id = $pid;
        }
    }
    return $product_id;
}

However, your CRED form does not contain a parent Product field. So I guess that you want to use the value of the Listing Option select field instead, am I right? Well first you need to modify that select field so that each option has a unique value.

Then, you should set up a custom field on the Listing post type that will hold the value of the Listing Option selection. Add that field to the CRED form, and hide it using CSS. Write custom JavaScript to copy the value from the Listing Option field into this hidden field before the form is submitted. Then you can access the Listing Option value in your cred_commerce_add_product_to_cart callback and do whatever you need to do with it to determine which product should be added to the cart. For example, this line in the code above will let you access the custom field value:

$pid = get_post_meta($post_id, 'wpcf-your-hidden-field-slug',true); // get the value of the Listing Option field