Skip Navigation

[Resolved] Building an online pricing tool & enquiry taker

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

Problem: I would like to build an online pricing tool that can be used to submit enquiries. The values in the pricing tool should be defined using custom fields on the parent post.

Solution: Use CRED generic fields to allow the User to select options, then use the CRED API hook cred_save_data to perform the necessary calculations.

If more than one option in a single generic field can contain the same value, then you must use an abstraction so that each option has a different value in the form. Then convert that abstraction in the PHP code.

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

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

Last updated by nickT-2 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#626009

Hi,

I'm using Views and Types on my website with woocommerce with great success. I'm now thinking about the next phase of development which is to have a live pricing model where people can select various options on the product and a rough price/quote is given and then they can request a more detailed quote.

Is something like this possible with the combination of Types, Views and Cred.

To help save time, are there any app templates, showcases or examples you might be able to point me to.

Many thanks

#626026

Hi, I can help you set up a CRED form that will allow your site visitors to select various options and submit a request. That request can be set up to trigger automated email notifications, if necessary. Calculating a quote based on the selected options will probably require custom code, as Toolset doesn't have many advanced mathematical features built-in. I can provide you with code examples that show how you can perform your own calculations based on the form options selected, using PHP and the CRED API.

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

For example, here is a custom function that calculates a date range based on the start and end dates selected in the CRED form:

//Calculate the difference from a start date and end date sent after the CRED form submission.
add_action('cred_save_data', 'calculate_days_func',10,2);
function calculate_days_func($post_id, $form_data) {
    if ($form_data["id"]==1160 || $form_data["id"]==1184)
    {
        if(isset($_POST['wpcf-start-date']['datepicker'])){
            $start_date = new DateTime(date('Y-m-d', $_POST['wpcf-start-date']['datepicker']));
        }
        if(isset($_POST['wpcf-end-date']['datepicker'])){
            $end_date = new DateTime(date('Y-m-d', $_POST['wpcf-end-date']['datepicker']));
        }
        $datediff = $start_date->diff($end_date);
        update_post_meta($post_id, 'wpcf-duration', $datediff->days + 1);
    }
}

The example above uses datepicker fields, but for most field types you can access the field value in the $_POST superglobal like this:

$_POST['wpcf-fieldslug']

Let me know if you think this will work for your site, and we can discuss in more detail.

#626349

Hi
Thanks for your help & suggestions.

Just a thought .. gravity forms seems to have a few pricing and calculation fields. Does types work with gravity forms? In particular, is it possible to have custom cost fields sitting on the post using wptypes and to insert them into the gravity form so it calculates the costs?

#626665

To some degree, yes. For example, you can use Types field shortcodes to dynamically populate form field values. In the example below, we are automatically populating a field in a Gravity Form called "gravityfield" with the value of a Types custom field called "typesfield" applied to the current post.

[gravityform id="1" title="false" description="false" field_values="gravityfield=[types field='typesfield'][/types]"]

This is a very simple example, but more advanced features like repeating fields and serialized data structures like checkboxes groups may not work as well. You can also use hooks and URL parameters in Gravity forms, as described here:
https://docs.gravityforms.com/using-dynamic-population/
More about the types field shortcode here:
https://toolset.com/documentation/customizing-sites-using-php/functions/

#627065

Cool thank you Christian.

I've had a few messages back and forth with Gravity and think it might be best and be more flexible to the design I had in mind if I build in CRED.

Thanks for your example code ... I'm about to build a prototype and see how far I can get .. so will be back in a day or two :o)

#627147

Sounds good. I will mark this ticket as pending an update from you. No need to reply right now. The ticket will remain open for 30 days.

#627342

Hi Christian,

I've started to build my CRED form and custom fields. Please can I check the correct way of setting this up for what I would like to do.

What I'm trying to do is:
On each of my holiday package pages I would like to embed this CRED form, which will act as an immediate quote builder and then send a request if the browser wants to book it.

So on each package post I would have a number of custom fields:
-- flight price with a number of options (from London, from Manchester, from Glasgow) as a radio button or dropdown
-- hotel grade with a number of options (basic, 3 star, 4 star) as a radio button or dropdown
-- car rental with a number of options (standard, SUV, Luxury) as a radio button or dropdown
-- list of activities as a checkbox

For each of the above options I would have numeric data and a shortcode which then displays the total cost as the browser selects different options.

What I'm stuck on:
- for the custom fields (on the Package posts) that will contain the pricing info ... do these need to be single numeric fields or can I do a dropdown/radio button field and then be able to add a value for each option e.g. from London, from Manchester etc. per package?

- If it's only possible by using single numeric fields .. is it then possible to display the options as a dropdown/radio button on the CRED form?

Thanks

#627348

Quick update - I don't know if this is a possible solution:
- To have a new post type called 'Enquires' which the CRED form is set to create
- On this 'Enquires' post to create the dropdown and radio button questions e.g. Flights from, Accommodation type, Car options etc.
- And then in the CRED form design can I insert the 'Package' posts pricing data as the above question options?

#627463

For each of the above options I would have numeric data and a shortcode which then displays the total cost as the browser selects different options.
It sounds like you're describing a system that calculates values while the User is interacting with the form, even before the form is submitted. Am I understanding correctly? If so, then this will require custom code that falls outside the scope of the support we provide here in the forums. CRED does not have a JavaScript API available, so you would be required to handle these form interactions using custom JS. In the example I provided earlier (https://toolset.com/forums/topic/building-an-online-pricing-tool-enquiry-taker/#post-626026) you can see how to calculate values after the CRED form is submitted. In other words, the price will not be updated in real-time as the user selects flight and accommodation options. It will only be calculated when the form is submitted. Let me know if that's okay for your site.

#627482

Hi,

Sure .. displaying the total after submit would be fine for now. Maybe displaying the total on the page as the user selects is a nice to have and possible future development.

So to go with the option of displaying the total after submit .. my first question is how to get the package's custom field values into the CRED form options.

My CRED form at the moment looks like this (I've also listed the custom fields from the package post for reference):

[credform class='cred-form cred-keep-original']

These are some test fields on the page:<br>
Flights price - [types field='flights-price' format='FIELD_NAME: FIELD_VALUE'][/types]<br>
Hotel Basic price - [types field='hotel-price-basic' format='FIELD_NAME: FIELD_VALUE'][/types]<br>
Hotel 3 star price - [types field='hotel-price-3-star' format='FIELD_NAME: FIELD_VALUE'][/types]<br>
Hotel 4 star price - [types field='hotel-price-4-star' format='FIELD_NAME: FIELD_VALUE'][/types]<br>


	[cred_field field='form_messages' value='' class='alert alert-warning']

	<div class="form-group">
		<label>Flights from</label>
		[cred_field field='flights-from' post='holiday-enquiry' value='' urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Accommodation</label>
		[cred_field field='accommodation' post='holiday-enquiry' value='' urlparam='' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Car Rental</label>
		[cred_field field='car-rental' post='holiday-enquiry' value='' urlparam='' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Activities</label>
		[cred_field field='activities' post='holiday-enquiry' value='' urlparam='' output='bootstrap']
	</div>

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

#627541

Okay so let me restate that to make sure I understand correctly. You would like to add some custom fields on the Package post type. On the single Package page, you would like to display a CRED form that creates Enquiry posts. In that CRED form, you would like to include input fields, like a select or radio, that have options with values specified by the custom fields in the current Package. Is that accurate?

If so, then the only real way to accomplish this is to use generic fields. A standard CRED field like a select or radio must have predefined options and values. Those must be defined as custom fields on the Enquiry post type - the post type created or edited by that CRED form. If you want to use dynamic options and values, you must use generic fields. Here is an example of a generic select field that uses the value of a custom field "order", applied to the current page, in an option:

[cred_generic_field field='my-generic-select-1' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"[types field='order' output='raw' id='$current_page'][/types]","label":"Order of the custom field on the current page"},
{"value":"2","label":"Order 2"}
]
}
[/cred_generic_field]

Then you can access the selected generic field value in a CRED hook like this:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
  // get the value of my-generic-select-1 field
    $generic_val = $_POST['my-generic-select']
    // your code continues ...
  }
}
#627542

Hi,

Thanks for the demo code. Would this look correct using for example my 3 fields for accommodation cost .

So on my Package page I would have the following custom fields:
- 'hotel-price-basic'
- 'hotel-price-3-star'
- 'hotel-price-4-star'

Would the code you mentioned look like this:

The Cred Question

[cred_generic_field field='hotel-type' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"[types field='hotel-price-basic' output='raw' id='$current_page'][/types]","label":"Order of the custom field on the current page"},
{"value":"[types field='hotel-price-3-star' output='raw' id='$current_page'][/types]","label":"Order 2"},
{"value":"[types field='hotel-price-4-star' output='raw' id='$current_page'][/types]","label":"Order 3"},
]
}
[/cred_generic_field]

And then the hock:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
  // get the value of hotel-type field
    $generic_val = $_POST['hotel-type']
    $generic_val = $_POST['flights']
    $generic_val = $_POST['car-type']
    // your code continues ...
  }
}

Please can I ask .. what should the '12345' be that you've put in? Is that the ID of the CRED form or something else.
Also .. at the end of the hock would I list the other generic questions as well like how I've done it above?

Please can I check where does this hock code go? in the functions.php file or in js box on the CRED form page ... I'm think this looks like php so therefore the functions.php file.

Thanks

#627551
{"value":"[types field='hotel-price-basic' output='raw' id='$current_page'][/types]","label":"Order of the custom field on the current page"},
{"value":"[types field='hotel-price-3-star' output='raw' id='$current_page'][/types]","label":"Order 2"},
{"value":"[types field='hotel-price-4-star' output='raw' id='$current_page'][/types]","label":"Order 3"},
]
}
[/cred_generic_field]

^ This looks okay, but you will probably change the labels to something like "Basic", "3 Stars", and "4 stars" instead of "Order of the custom field on the current page", "Order 2", and "Order 3".

Please can I ask .. what should the '12345' be that you've put in? Is that the ID of the CRED form or something else.
Yes, 12345 is the ID of the CRED form. If you want to apply the same code to more than one CRED form, you can use a comma-separated list of form IDs like 12345, 67890, 98765.

Also .. at the end of the hock would I list the other generic questions as well like how I've done it above?
Basically yes, but you'll probably use different variable names for each value as shown here:

$hotel_val = $_POST['hotel-type']
$flights_val = $_POST['flights']
$car_val = $_POST['car-type']

I'm think this looks like php so therefore the functions.php file.
You are correct, it goes in the functions.php file.

#627731
CRED FORM - NPT.JPG

Hi Christian,

I've added the generic fields and the options are not showing up.

Would it be easier if I give you access to have a look?

#627759
CRED FORM - NPT2.JPG

Hi again,

I've managed to get the questions to show. It must have been something in the copy and paste from the code this page. I manually wrote out the code (exactly as you had it) and now the fields are showing. (screenshot attached).

So I've put the CRED form questions in like this:

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']


<label>Flights from</label>

[cred_generic_field field='flights' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"[types field='flights-from-london' output='raw' id='$current_page'][/types]","label":"Fly from London"},
{"value":"[types field='flights-from-mancheswter' output='raw' id='$current_page'][/types]","label":"Fly from Manchester"},
{"value":"[types field='flights-from-midlands' output='raw' id='$current_page'][/types]","label":"Fly from the Midlands"},
{"value":"[types field='flights-from-scotland' output='raw' id='$current_page'][/types]","label":"Fly from the Scotland"}
]
}
[/cred_generic_field]

<br><br>

<label>Accommodation</label>
[cred_generic_field field='hotel-type' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"[types field='hotel-price-basic' output='raw' id='$current_page'][/types]","label":"Basic"},
{"value":"[types field='hotel-price-3-star' output='raw' id='$current_page'][/types]","label":"3 Star"},
{"value":"[types field='hotel-price-4-star' output='raw' id='$current_page'][/types]","label":"4 Star"}
]
}
[/cred_generic_field]

<br><br>

<label>Car Rental</label>
[cred_generic_field field='car-type' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[
{"value":"[types field='car-rental-standard' output='raw' id='$current_page'][/types]","label":"Standard"},
{"value":"[types field='car-rental-suv' output='raw' id='$current_page'][/types]","label":"Standard SUV"},
{"value":"[types field='car-rental-luxury' output='raw' id='$current_page'][/types]","label":"Luxury Car or Large SUV"}
]
}
[/cred_generic_field]

<br><br>

	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

I've put in the hock in the functions.php like this (but I had to add a ';' character at the end of each 'get the value...' section for the code not to break the site ... hope that looks correct):

		add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data) {
  $forms = array( 6831 );
  if ( in_array( $form_data['id'], $forms ) )
  {
  // get the values from custom fields
    $flights_val = $_POST['flights'];
    $hotel_val = $_POST['hotel-type'];
    $car_val = $_POST['car-type'];
  }
}

Please can I now check what I need to do to calculate the sections and output the total. From looking at your earilier code example it looks like I need two things:
1. the php code that calcualtes the values
2. create a shortcode for displaying the total figure - for use on the page or email template after submit is clicked

Is there any code examples of how to add up the fields in CRED?

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.