I have a custom select tag on the Woocommerce cart which is setup as shown below. Everything seems to be working except that once you update the quantity on the cart, any further changes to the select dropdown box is no longer detected so it doesn't save the newly select value. Is there some way to reset it (without reloading the page) so that additional change notifications are captured by the javascript code?
functions.php
// recreate proceeded to checkout button to be a form so custom cart checkout has $_POST data
add_action( 'woocommerce_proceed_to_checkout', 'add_agent_before_proceed_to_checkout', 15 );
function add_agent_before_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
wc_get_template('cart/cart-add-agent.php');
}
cart-add-agent.php
<form id="checkout_form" method="POST" action="<?php echo esc_url ( wc_get_checkout_url() ); ?>">
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
<div style="margin-top:5px">
<select id="agent" name="agent">
<option value="Agent1">Checkout with Agent1</option>
<option value="Agent2">Checkout with Agent2</option>
<option value="Agent3">Checkout with Agent3</option>
</select>
</div>
</form>
Bottom of cart.php
<script type="text/javascript">
jQuery(document).ready(function() {
var item = window.localStorage.getItem('agent') || "Agent1";
document.getElementById('agent').value=item;
jQuery('#agent').on('change', function() {
var item = jQuery(this).val();
console.log("saving selected item " + item);
window.localStorage.setItem('agent', item);
});
});
jQuery( document.body ).on( 'updated_cart_totals', function() {
var item = window.localStorage.getItem('agent') || "Agent1";
document.getElementById('agent').value=item;
console.log("selected item " + item);
});
</script>
Can you please try this, I believe it it working fully.
<script>
jQuery(document).ready(function($){
var session_agent = sessionStorage.getItem('agent');
console.log('get session value page loaded ' + session_agent);
if (session_agent == null ){
console.log('create agent not in session set to Agent1');
sessionStorage.setItem('agent', 'Agent1');
$('#agent').val('Agent1');
}else{
$('#agent').val(session_agent);
}
$( document ).on( 'change', '#agent', function() {
sessionStorage.setItem('agent', $(this).val());
console.log('update value in session ' + $(this).val());
});
$( document ).ajaxComplete( function() {
var session_agent = sessionStorage.getItem('agent');
console.log('ajax completed get session value and set dropdown ' + session_agent);
$('#agent').val(session_agent);
});
});
</script>
I have left the console.log in there to help you with checking it. Obviously these need to be removed in the final version
You could change it probably to use window.localStorage also but I had already begun this approach, so I left it using session instead. Someone else might be able to advise of the pros and cons.
Related
As of now, I have tried doing this
In the contact form, under form tab I have added these lines-
<label>
[email* your-email default:get id:textarea placeholder "Email address"] </label>
<div style="text-align: center;">[submit id:button "Get started now"]</div>
in functions.php file I have added following lines
add_action( 'wp_footer', ’redirect_to_new page’);
function ’redirect_to_new page() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
If ( '2023' == event.detail.contactFormId ) {
location = 'https://app.techinme.com/#/registration?email=[your-email]’;
}
}, false );
</script>
<?PHP
}
when the user clicks on submit button on the form they are directed to the new page but I am unable to populate the email ID in the new form present in the new page. Can someone please guide me further?
Cheers!
This is not tested but should get you going in the right direction.... Below I updated your code to grab the email from the form and append it to the location url:
<?php
add_action( 'wp_footer', 'redirect_to_new_page');
function redirect_to_new_page() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
if ( '2023' == event.detail.contactFormId ) {
// get email
var email = event.detail.inputs.filter(function(item){
return item.name === 'your-email';
})[0].value;
location = 'https://app.techinme.com/#/registration?email=' + email;
}
}, false );
</script>
<?php
}
I'm trying to get the value of each checked box that has been selected as true. Each time I select a checkbox it grabs the value of that checkbox instead of all the selected checkboxes.
Path: talentList.html
<fieldset>
<div class="checkbox">
<label>
<input name="specialisation" type="checkbox" value="Accounting Firm"> Accounting Firm
</label>
</div>
<div class="checkbox">
<label>
<input name="specialisation" type="checkbox" value="Accounting in Industry"> Accounting in Industry
</label>
</div>
</fieldset>
Path: talentList.js
Template.talentList.events({
'change [name="specialisation"]': function ( event, template ) {
let specialisation = event.target.value;
template.candidateListFilter.set( specialisation );
}
});
There is only one target set in the event handler so event.target.value will be a scalar instead of an array. You need to iterate over the array of checkboxes.
Template.talentList.events({
'change [name="specialisation"]': function ( event, template ) {
$.each($('[name="specialisation"]'),function(i,cb){
let specialisation = cb.value;
template.candidateListFilter.set( specialisation );
});
}
});
To be honest this seems like an odd pattern. If you want to update a document whenever a checkbox is checked/unchecked you shouldn't have to set the state of all the other checkboxes at the same time, you should be able to just poke the one you want.
Not sure if this is correct. It creates an object of all selected options.
'change [name="specialisation"]': function ( event, template ) {
$(document).ready(function(){
var specialisation = $('input[name="specialisation"]:checked').map(function(){
return $(this).val();
});
var specialisationListArray = specialisation.get();
template.candidateListFilter.set( specialisationListArray );
});
},
I'm using Advanced Custom Fields on my WordPress site and I have a datepicker start-date & end-date that I'm trying to set min/max on when one is selected. From jQuery's datepicker date-range I'm trying to add the onClose option.
I currently have the following code based off the custom javascript fields page but it's not working.
<?php function takeover_scripts() { ?>
<script type="text/javascript">
(function($) {
var startDate = $('.acf-field-568d4b2968a3e');
acf.add_filter('date_picker_args', function( args, startDate ){
// do something to args
args.changeMonth = false;
args.onClose = function( selectedDate ) {
$( ".acf-field-568d4bbd68a3f" ).datepicker( "option", "minDate", selectedDate );
}
// return
return args;
});
var endDate = $('.acf-field-568d4bbd68a3f');
acf.add_filter('date_picker_args', function( args, endDate ){
// do something to args
args.changeMonth = true;
args.onClose = function( selectedDate ) {
$( ".acf-field-568d4b2968a3e" ).datepicker( "option", "minDate", selectedDate );
}
// return
return args;
});
})(jQuery);
</script>
<?php }
add_action('acf/input/admin_footer', 'takeover_scripts'); ?>
I'm not sure if I need to target the id of the end-date input or the field number or if I even have the start-date field selected correctly. If anyone has any experience with this please let me know what I'm setting/selecting wrong.
Here's the markup for the two fields :
<div class="acf-field acf-field-date-picker acf-field-568d4b2968a3e" style="width:25%;" data-name="start_date" data-type="date_picker" data-key="field_568d4b2968a3e" data-required="1" data-width="25">
<div class="acf-label">
<label for="acf-field_568d479e68a3b-0-field_568d4b2968a3e">Start Date <span class="acf-required">*</span></label>
</div>
<div class="acf-input">
<div class="acf-date_picker acf-input-wrap" data-display_format="MM d, yy" data-first_day="0">
<input id="acf-field_568d479e68a3b-0-field_568d4b2968a3e" class="input-alt" type="hidden" name="acf[field_568d479e68a3b][0][field_568d4b2968a3e]" value="">
<input type="text" value="" class="input active hasDatepicker" id="dp1452127570218">
</div>
</div>
</div>
<div class="acf-field acf-field-date-picker acf-field-568d4bbd68a3f" style="width:25%;" data-name="end_date" data-type="date_picker" data-key="field_568d4bbd68a3f" data-required="1" data-width="25">
<div class="acf-label">
<label for="acf-field_568d479e68a3b-0-field_568d4bbd68a3f">End Date <span class="acf-required">*</span></label>
</div>
<div class="acf-input">
<div class="acf-date_picker acf-input-wrap" data-display_format="MM d, yy" data-first_day="0"><input id="acf-field_568d479e68a3b-0-field_568d4bbd68a3f" class="input-alt" type="hidden" name="acf[field_568d479e68a3b][0][field_568d4bbd68a3f]" value=""><input type="text" value="" class="input active hasDatepicker" id="dp1452127570219"></div>
</div>
</div>
Thanks
P.S. : I also have this on ACF's support located here.
A little late to this, but I finally figured it out.
So it turns out the .datepicker() function needs to be called on the input field with the class .hasDatepicker and not the input field with the acf id.
I have the following working:
$('.acf-field-568d4b2968a3e').find('.hasDatepicker').datepicker('option', 'minDate', minDateVar );
Hope this helps someone!
I have...
a dynamic populated select box
several input boxes
a submit button
form fields are loaded initially using cookies
several dynamic populated divs
I want...
start loading the content of my DIVs after all FORM elements have been loaded completely (= filled with data, select boxes are populated)
Sample code:
<script type="text/javascript">
$(document).ready(function() {
// Populate <select>
var options ='';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + i + '">' + i + '</option>';
}
$("select#myid").html(options);
})
...
</script>
<form>
<select id="myselect></select>
<input id="mytext" type="text" value="" />
<input type="submit" value="Search" />
</form>
<% foreach( MyElement element in MyListing) { %>
<div>
<script type="text/javascript">
$(document).ready(function() {
DoSomething($(select#myid).val());
})
</script>
</div>
<% } %>
Any help is very appreciated.
Edited for the extra information:
jQuery(function($) { // note that this is equivalent to $(document).load()
// if we are here, then all your page and form elements have loaded.
// Populate <select> as per your code above
$('div').each(function(index) { // perhaps give them a class?
$(this).load(<<someURL>>);
// it's not clear from your question how you intend to get the
// dynamic content, ie: what url to use?
});
});
I would like to pass information to an iframe via post. (Could be jquery or javascript that executes the post, it doesn't really matter).
The information cannot be sent via querystring as I do not have access to change the way the page brought in by the iframe is.
This data will determine the layout of the content in the iframe so how can I make it so that after the post is sent the iframe is updated? (possibly refresh?)
I wrote a blog post about doing this with jQuery to upload a file using a hidden iframe. Here's the code:
Here is the HTML for the form:
<div id="uploadform">
<form id="theuploadform">
<input type="hidden" id="max" name="MAX_FILE_SIZE" value="5000000" >
<input id="userfile" name="userfile" size="50" type="file">
<input id="formsubmit" type="submit" value="Send File" >
</form>
The DIV in which to allow jQuery to create the iframe you can hide it with a little CSS:
<div id="iframe" style="width:0px height:0px visibility:none">
</div>
The DIV in which to show the results of the callback:
<div id="textarea">
</div>
The jQuery code:
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#formsubmit").click(function() {
var userFile = $('form#userfile').val();
var max = $('form#max').val();
var iframe = $( '<iframe name="postframe" id="postframe" class="hidden" src="about:none" />' );
$('div#iframe').append( iframe );
$('#theuploadform').attr( "action", "uploader.php" )
$('#theuploadform').attr( "method", "post" )
$('#theuploadform').attr( "userfile", userFile )
$('#theuploadform').attr( "MAX_FILE_SIZE", max )
$('#theuploadform').attr( "enctype", "multipart/form-data" )
$('#theuploadform').attr( "encoding", "multipart/form-data" )
$('#theuploadform').attr( "target", "postframe" )
$('#theuploadform').submit();
//need to get contents of the iframe
$("#postframe").load(
function(){
iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
$("div#textarea").html(iframeContents);
}
);
return false;
});
});
</script>
I used a php app like this uploader.php to do something with the file:
<?php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$maxfilesize = $_POST[MAX_FILE_SIZE];
if ($maxfilesize > 5000000) {
//Halt!
echo "Upload error: File may be to large.<br/>";
exit();
}else{
// Let it go
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print('File is valid, and was successfully uploaded. ');
} else {
echo "Upload error: File may be to large.<br/>";
}
chmod($uploadfile, 0744);
?>
There's more there than you need, but it illustrates the concept in jQuery.
I don't have the code handy but my team accomplished this purely in Javascript. As I recall it went something like this:
function postToPage() {
var iframe = document.getElementById('myIFrame');
if (iframe) {
var newForm = '<html><head></head><body><form...> <input type="hidden" name="..." value="..." /> </form><script type=\"text/javascript\">document.forms[0].submit();</scrip' + 't></body></html>';
iframe.document.write(newForm); //maybe wrong, find the iframe's document and write to it
}
}