Gravity forms conditional logic based on number of values selected - wordpress

I have a form built with gravity forms that asks 5 questions, each with 2 answers.
Each answer corresponds to a category, and so I want to be able to add some conditional logic so that the confirmation sends to a different page depending on which category has more answers.
Form is here: https://90daysfromretirement.com/medicare-advantage-vs-medicare-supplement/
The two categories are "Advantage" and "Supplement" - so dependng on the answer, so whichever category has more responses (3 or greater) then the confirmation will redirect them to the appropriate page.
Problem is I can't figure out how to Count the values for each and run logic off it. I don't think it's possible with any gravity forms add-ons I've found, and I haven't been able to put the code together to do it.
Any suggestions?

Since you only have two options, you really only need to know the count of one and the total number of questions to know which value has been selected more.
For example, if you have 10 questions and "Advantage" choices are selected 6 times, you know that Advantage has more than "Supplement".
Here's a snippet that should get you started. It will take a group of Radio Button fields, count the number of times a specified value has been selected, and populate that count into a Number field.
You can then use that Number field to apply conditional logic on your confirmations to redirect to the appropriate page (e.g. If Count is greater than 5, redirect to Advantage page. If Count is less than 6, redirect to Supplement page).
/**
* Gravity Wiz // Gravity Forms // Value Counter
* https://gravitywiz.com/
*
* Count the number of times a given value has been selected in a group of fields and populate that number into a Number field.
* This snippet is designed to target a Number field and count selected values in Checkbox and Radio Button fields.
*
* This snippet works best with our free [GF Custom Javascript](https://gravitywiz.com/gravity-forms-custom-javascript/) plugin.
*/
// Replace the "1", "2" and "3" with field IDs of fields that should have their selected values counted. If you are using the
var $radios = jQuery( '#field_GFFORMID_1, #field_GFFORMID_2, #field_GFFORMID_3' );
// Replace "4" with the ID of the Number field in which the count should be populated.
var $target = jQuery( '#field_GFFORMID_4' );
// Replace "a" with the value you wish to count if selected.
var countValue = 'a';
function gwRecountValues() {
$target.find( 'input' ).val( $radios.find( 'input:checked[value="' + countValue + '"]' ).length );
}
$radios.on( 'change', function() {
gwRecountValues();
} );
gwRecountValues();
And here's the source which may be updated in the future if other folks use this and experience any issues or need improvements: https://github.com/gravitywiz/snippet-library/commit/d34eb169da937981c4a1ea49bb8a36df023bff1b
The one thing missing from this snippet is PHP validation. You may not be concerned about people altering their count but it is possible without PHP validation.

You could try adding the following to your functions.php file
add_action( "gform_after_submission_66", "after_submission", 10, 2 ); //replace 66 with your form number
function after_submission($entry, $form ){
$supplement_answers = array("milk", "apple", "pasta", "fries", "oats"); //replace array items with the supplement answers
$advantage_answers = array("red", "blue", "pink", "purple", "yellow"); //replace array items with the advantage answers
$field_nums = array("2","3","6","7","9"); //replace these field numbers with your own -- order then to align with answers above
$count_fields = count($field_nums);
$a = 0;
$b = 0;
for($i=0; $i<$count_fields; $i++){
if($entry[$field_nums[$i]] == $supplement_answers[$i]){
$a = $a + 1;
}else if($entry[$field_nums[$i]] == $advantage_answers[$i]){
$b = $b + 1;
}
}
if($a > $b){
header('Location: https://website/supplements_page'); //replace url
}else if($a < $b){
header('https://website/advantage_page'); //replace url -
}
}

Related

How to get total rows in Drupal 8 view with pagination?

In Drupal 8, I use a view with pagination. There are 10 items per page.
In hook_views_pre_render(ViewExecutable $view) {..} I want to get the number of items to use in another function. However, $result = 10 and $total_rows = 10 due to the pager setting. The actual item number is between 500 and 2000.
As a workaround I tried a temporary view.
$view2 = Views::getView('my_view');
$view2->setDisplay('page_1');
$view2->setItemsPerPage(15000);
$view2->execute();
This works. view2->total_rows returns the desired value. But I don't want to use a fixed value in setItemsPerPage. 0 or null (for unlimited items per page) returns no result for total_rows.
What can I do to get the correct number of views-items?
In your hook_views_pre_render function, instead of calling view->SetDisplay and view->setItemsPerPage, you can execute the view (even with pager settings) and get the total rows/results:
$view = Views::getView('my_view');
$view->execute('page_1');
$rows = $view->total_rows;
$rows will provide the total count that you are looking for.
And get_total_rows needs to be set to true for total_rows to operate.
$view = Views::getView('my_view');
$view->get_total_rows = TRUE;
$view->execute('page_1');
$rows = $view->total_rows;

How to get total number of rows in a drupal view alter hook?

I've to avoid duplicate search result in a view, so what I am trying to alter the view using pre render hook. and removing the duplicates it's working fine but the problem is in the count of result. it shows the count from the query executed and this include the duplicated item too. also, I enabled the pagination with limit of 5 in a page. then the count seems to be strange it's taking the count of the elements showing in each page
function search_helper_views_pre_render(\Drupal\views\ViewExecutable $view) {
if ($view->id() == "all_news" || $view->id() == "all_publications" || $view->id() == "all_events" || $view->id() == "global_search") {
$unique_nids = $d_nids = $new_results = array();
// Loop through results and filter out duplicate results.
foreach($view->result as $key => $result) {
if(!in_array($result->nid, $unique_nids)) {
$unique_nids[] = $result->nid;
}
else {
unset($view->result[$key]);
}
}
$view->total_rows = count($view->result);
//$view->pager->total_items = count($view->result);
$view->pager->updatePageInfo();
}
}
the expected output of the $view->total_rows must be the total count of result instead of count of elements shown in the page.
You totaly done it in wrong way. as you see ( and it's clear from its name ), it's hook__views_pre_render it runs before the rendering. So its really hard to manipulate the views results and counter, pagination there.
As I see in your query you just remove duplicate Nids , so you can easily do it by Distinct drupal views feature.
Under Advanced, query settings, click on settings.
You will get this popup, now checkmark Distinct
Could do
$difference = count($view->result) - count($new_result);
$view->total_rows = $view->total_rows - $difference;
BTW Distinct setting doesn't always work, see https://www.drupal.org/project/drupal/issues/2993688

If statement inside Wordpress Contact Form 7 E-mail Template

I'm trying to edit E-mail 2 in Contact Form 7 Wordpress plugin. I have an Option tag [salutation](Mr, Mrs), in German Language – [anrede] (Herr, Frau) So, in E-mail 2 template I have to write 2 different texts like:
Sehr geehrter Herr
Sehr geehrte Frau
The first one is when the [anrede] option is 'Herr', and the other one is when the [anrede] option is 'Frau'.
if ([anrede] = "Herr"){
<p>Sehr geehrter Herr</p>
}
else if([anrede] = "Frau"){
<p>Sehr geehrte Frau</p>
}
Thank you in advance,
Jon Kraja
and if statement isn't needed, seems to me is much easier as follows and w/o plugin conditional fields.
Generate a select field with your values
--> [select anrede "Herr|geehrter" "Frau|geehrte"]
Take care, that a pipe symbol (|) is between the values as shown above.
Note: only the left side of the select field is shown on contact form!; the right side is NOT shown and always hidden. BUT you can choose both values with different codes on your Mail:
If you would put left side of value into your mail, use [_raw_anrede] - means "Herr" or "Frau" is given out.
If you would put right side of value into your mail, use [anrede] - means "geehrter" or "geehrte" is given out.
With this you may have a correct salutation depending on "Herr" or "Frau" is choosen at the select field.
You can use the hook 'wpcf7_before_send_mail'.
function hsc_cf7_submit_update_email($cf){
$formID = $cf->id();
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
if (!$submission){
return;
}
if($formID == 'ENTER FORM ID'){
$submitData = $submission->get_posted_data();
$mail = $wpcf7->prop('mail_2');
$mailBody = $mail['body'];
if($submitData['anrede'][0] == 'Herr'){
$mail['body'] = "<p>Sehr geehrter Herr</p>";
$wpcf7->set_properties( array("mail_2" => $mail)) ;
}elseif($submitData['anrede'][0] == 'Frau'){
$mail['body'] = "<p>Sehr geehrte Frau</p>";
$wpcf7->set_properties( array("mail_2" => $mail)) ;
}
}else{
return;
}
}
add_action( 'wpcf7_before_send_mail', 'hsc_cf7_submit_update_email' );
That's not possible with contact form 7 alone, but you can do it with this plugin.

Woocommerce: How-to show ‘Order Total’ amount in Words

On my Woocommerce website, I need to display Total Order Amount in words, which will be shown on Checkout Page, Cheque Payment & on Invoice.
Example: 1590.00 (One thousand five hundred & ninety only)
How can we achieve this?
TIA
You can try number formatter class as mentioned in these threads a and b
Use the filter "woocommerce_cart_totals_order_total_html".
function custom_woocommerce_order_amount_total( $order_total ) {
$number_total = WC()->cart->get_total();
// number formatting goes here;
// using number formatter class
$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
$total_in_words = $f->format($number_total); // Total in words
$order_total = $total_in_words;
return $order_total;
};
add_filter( 'woocommerce_cart_totals_order_total_html', 'custom_woocommerce_order_amount_total' );
You can also try the other hooks like woocommerce_get_formatted_order_total

Adobe Catalyst - 'Attribute Price' adding on to 'Sell Price' Issue

I have set a products sell price at £100. I have also created a 'Size' attribute at 'Large' £120.
But when I view the product and select 'Large' it prices up at £220 (adding the attribute and sell price together) when I'm wanting it at just the £120.
Any thoughts on why I'm getting this issue?
I believe Daut maybe talking about something else but I could be wrong.
When using attributes that you talk about, the price is calculated by default price plus the attribute price. This is why you see £220 as your total, as you have figured out.
In other words, your default price is £100. For your total to be £120, then your Large attribute would actually be £20. When it adds together, your total price will be £120.
When I use attributes with varying cost, I typically write my attribute as:
Large + [then BC inserts the price.]
On the BC App Store, there are a couple plugins (here and here) that assist with using Attributes. Their main purpose, from my understanding, is controling how the information is displayed to customers. I have no experience using either of these but it may help you.
Hi have created my own way around this issue in BC I also shared it on Business Catalyst Forum too. for select dropdown or radio buttons use the code bellow, you need to have a certain code ability to mend it together, it was quite done some time ago but if somebody interested on improving it to a cleaner way be welcome to share.
$(document).ready(function(){
StartDynamicPrice();
DoPriceChange();
});
var el_totalprice='#totalprice';
var el_totalprice_gst='#totalprice_gst';
//var el_attrselect='.catProdAttributeItem select';
// uncomment if you want radio as well
var el_attrselect='.catProdAttributeItem select, .catProdAttributeItem input';
var currencysymbol='£';
Number.prototype.toMoney=function(decimals, decimal_sep, thousands_sep){
var n = this,
c = isNaN(decimals) ? 2 : Math.abs(decimals),
d = decimal_sep || '.',
t = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
sign = (n < 0) ? '-' : '',
i = parseInt(n = Math.abs(n).toFixed(c)) + '',
j = ((j = i.length) > 3) ? j % 3 : 0;
return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
}
DoPriceChange=function(){
var selected='';
var newsubtotal=0;
$.each($(el_attrselect), function(i,e){
selected=$(e).children(':selected').text();
if (selected.indexOf(currencysymbol) != -1){
newsubtotal+=parseFloat(selected.substring(selected.indexOf(currencysymbol)+1));
};
});
newtotal=parseFloat($(el_totalprice).attr('base'))+parseFloat(newsubtotal);
newtotal_gst=newtotal+(newtotal*.10);
$(el_totalprice).html(currencysymbol+newtotal.toMoney());
$(el_totalprice_gst).html(currencysymbol+newtotal_gst.toMoney());
}
StartDynamicPrice=function(){
$(el_totalprice).attr('base',$(el_totalprice).html().replace(currencysymbol,'').replace(' ,','')); // set base price
$(el_attrselect).on('change',function(){
DoPriceChange();
})
}
/// END
Attributes in BC are add-ons. You cannot just get the attribute.
What you need is product grouping.
Group products together
You can create multiple products of the same type and group them together. A customer viewing one product can also see the available variations by selecting another product from the group.
Check how Grouping works in Business Catalyst
From the Actions menu, select Group Products Together.
Move the products from the left panel to the right, select a default product, and click Save.
Note: The default product is the only product that displays in a catalog. All other grouped products are available through the grouped product drop-down menu.

Resources