If statement inside Wordpress Contact Form 7 E-mail Template - wordpress

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.

Related

Gravity forms conditional logic based on number of values selected

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 -
}
}

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

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.

Drupal filter is not working properly

I'm not sure how to ask it, so if you need anymore additional information, please ask for it!
Situation
I've got a website in three languages. I got a lot of customer cases online each connected to a sector (depending in which sector they belong). Each sector and reference has it's own unique nid.
In my template.php it's stated like this:
if ('sector' == $vars['node']->type) {
$lang = '/'.$vars['language'].'/';
$key_path = $_SERVER['REQUEST_URI'];
$key_path = substr_count($key_path, $lang) ? substr($key_path, strlen($lang)) : $key_path;
if (strpos($key_path, '?')) $key_path = substr_replace($key_path, '', strpos($key_path, '?'));
if (strpos($key_path, 'sectors-references') === 0) {
$view = views_get_view('references');
if (!empty($view)) {
$view->set_arguments((int)$vars['node']->nid);
$vars['content']['suffix'] = $view->render();
}
}
}
And yet, every sector shows me the same references... What do I have to change to get the correct reference under the right sector?
Usually arguments are passed to set_arguments using an array, if you pass a non-array the argument will probably be ignored which is why you're always getting the same result. Try:
$view->set_arguments(array((int)$vars['node']->nid));

Drupal return number of results in a View

I have a view in Drupal that filters my content. It brings back 7 rows. All I want to return is the number or results returned(7). Is this possible?
I tried using the View result counter but it returns a number for each results
1
2
3
4
5
6
7
I just need the 7 part.
So in SQL I would do a select count(*)
what you can do is to activate php for the views header/footer and add the following snippet to it:
<?php
$view = views_get_current_view();
print $view->total_rows;
?>
This will print the total number of rows.
If you need the result as a field, you could use the "Views custom field" module, add a php field and run the same snippet.
Regards
Mike
If you want to get the count outside the view you can use this
$view = views_get_view('MY_VIEW_NAME');
$view->set_display('MY_DISPLAY'); // like 'block_1'
$view->render();
print sizeof($view->result);
Note : Do not use this within a view. It will be an overhead. If you are using it within view check the other answers.
If using views_get_view in Views 3, you can use this snippet:
$view = views_get_view('MY_VIEW');
$view->set_display('MY_DISPLAY');
// Execute first
$result = $view->preview('MY_DISPLAY');
// Output result only if rows > 0
if (count($view->result) > 0) {
print $result;
}
This works well for me and deals with the pager issues. Put this function in your custom module, rename / format as needed, and call it from your views-view--*view_name_goes_here*.tpl.php files.
function get_view_rowcount(){
$view = views_get_current_view();
$page_total = count($view->result);
if(isset($view->total_rows)){
return "<strong>Displaying " . $page_total . " of " . $view->total_rows . " total rows.</strong>";
} else {
return "<strong>Displaying " . $page_total . " of " . $page_total . " total rows.</strong>";
}
}
With Views 3 you may need to do
$view->get_total_rows = TRUE;
$total_items = $view->query->pager->get_total_items();
with drupal 7 - Under the pager options you have the option to
Expose items per page
When checked, users can determine how many items per page show in a view

Resources