Skip Navigation

[Resolved] CRED Commerce workflow – only purchase if checkbox is checked

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

Problem: I have a CRED Commerce form that allows Users to pay a fee and create a post. In some cases, the fee is not required. I would like to use a checkbox in the form to indicate whether or not the fee should be charged. If the checkbox is not checked, the User should not be charged, but the post should still be created. If the checkbox is checked, the User should have the fee product added to their cart with CRED Commerce, and the payment workflow in CRED Commerce should be followed normally.

Solution:
- Set up this form to always add the registration product to the User's cart, regardless of any input. We will override this with code when needed.
- Replace the generic radio control with a generic checkbox like this:

[cred_generic_field field='addfee' type='checkbox' class='' urlparam='']
{
"required":0,
"validate_format":0,
"checked":0,
"default":"1",
"label":"Add Handling Fee"
}
[/cred_generic_field]

- Add the following custom code to your child theme's functions.php file:

add_action( 'cred_commerce_before_add_to_cart', 'ts_only_buy_if_checked', 10, 2 );
function ts_only_buy_if_checked( $form_id, $post_id ) {
    $forms = array( 123, 456 );
    if( in_array( $form_id, $forms) ) {
      if( !isset( $_POST['addfee'] ) || $_POST['addfee'] != 1 ) {
        wp_redirect( 'http://yoursite.com/somepage' );
        exit();
      }
    }
}

Replace 123, 456 with a comma-separated list of any CRED form IDs where you want to apply this modification. Change the URL to redirect the User to some other destination on your site. You have access to the ID of the post that was added or edited in the $post_id variable. Now if the checkbox is not checked, the User will be redirected without placing the item in their cart.

You can call get_permalink using the $post_id parameter in your callback to redirect to the post that was created by CRED. Here is the updated redirect code:

...
wp_redirect( get_permalink( $post_id ) );
...

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_before_add_to_cart
https://developer.wordpress.org/reference/functions/wp_redirect/
https://developer.wordpress.org/reference/functions/get_permalink/

This support ticket is created 6 years, 10 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 7 replies, has 3 voices.

Last updated by Steve 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#617026

I have a form where artists are applying to be in a exhibition. The client has a registration fee which im collecting using cred commerce. However under certain circumstances the fee is waved.

I have a radio button generic field that holds the registration product. However if it is left unchecked. The customers still get taken to woocommerce and get a empty cart message

Is there someway I could make the UX better here when the radio button is unselected?

The form is at
hidden link

feel free to fill it out- its not live yet

[cred_generic_field field='Purchase2' type='radio' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"default":[],
"options":[
{"value":"15808","label":"Add Handling Fee"}
]
}
[/cred_generic_field]

#617189

Hi, I think it's standard to use a checkbox rather than a radio control for an opt-in like this. Here's how it could work:
- Set up this form to always add the registration product to the User's cart, regardless of any input. We will override this with code when needed.
- Replace the generic radio control with a generic checkbox like this:

[cred_generic_field field='addfee' type='checkbox' class='' urlparam='']
{
"required":0,
"validate_format":0,
"checked":0,
"default":"1",
"label":"Add Handling Fee"
}
[/cred_generic_field]

- Add the following custom code to your child theme's functions.php file:

add_action( 'cred_commerce_before_add_to_cart', 'ts_only_buy_if_checked', 10, 2 );
function ts_only_buy_if_checked( $form_id, $post_id ) {
    $forms = array( 123, 456 );
    if( in_array( $form_id, $forms) ) {
      if( !isset( $_POST['addfee'] ) || $_POST['addfee'] != 1 ) {
        wp_redirect( '<em><u>hidden link</u></em>' );
        exit();
      }
    }
}

Replace 123, 456 with a comma-separated list of any CRED form IDs where you want to apply this modification. Change the URL to redirect the User to some other destination on your site. You have access to the ID of the post that was added or edited in the $post_id variable. Now if the checkbox is not checked, the User will be redirected without placing the item in their cart.

More about this CRED Commerce API hook here:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_before_add_to_cart

#617194

Christian thanks for that solution. however it doesn't appear that checkboxes are supported. I first created the form that way but when i use a check box I dont get the drop down to select "The form specifies the product according to the value of this custom field"

So ive changed back to a radio.

Unfortunately the form bypasses the cart no matter what now.

Lastly how would i redirect to the post created by the form-as opposed to a static url. Is that possible

#617226

I dont get the drop down to select "The form specifies the product according to the value of this custom field"
Maybe I misunderstood what you need here. In the scenario I was describing, you will not choose this option. Instead, you will choose "Always this product, regardless of form inputs" and select your registration fee product. On the front-end, if the generic checkbox box is checked, the registration fee product will be added to the User's cart and he or she can continue to checkout. If the box is not checked, the code I provided will redirect the User to another page without adding anything to their cart. Have I misunderstood? Is there more than one product option we have not discussed? If so, please explain in more detail why it is necessary to choose a product in this custom field.

#617230

Ok im sorry thats a big DUH for me- i originally conceived of there being options which of course are now not needed and the check box and function work fine.

Thanks ever so much and especially for weekend work

Would there be a way i could make the redirection goto the post created by the submission? That would be ideal though not necessary.

Best

#617550

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Steve,

As Christian, is currently on vacation however he will be back tomorrow to continue assisting with this.

Thanks,
Shane

#617902

Yes, you can call get_permalink using the $post_id parameter in your callback. Here is the updated redirect code:

...
wp_redirect( get_permalink( $post_id ) );
exit();
...

Documentation for these functions:
https://developer.wordpress.org/reference/functions/wp_redirect/
https://developer.wordpress.org/reference/functions/get_permalink/
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_before_add_to_cart

#618269

Thanks again Christian- perfect

Best