I have created a ajax file handle after click add_to_cart. but my problem is my ajax file always run before woocommerce file (like my picture). How to run my custom ajax after woocommerce ajax add_to_cart ?
However its not a good practice, you can try the below code :
$('.add_to_cart_button').on('click',function(){
/** write your code here **/
});
Hope this helps
So I realize this post is old but I am experience the same problem. I used setTimeout to delay the ajax request by 500ms
jQuery(document).ready(function($) {
console.log('ajax-minicart.js loaded');
// What event do we want our AJAX to fire on? ADDING A PRODUCT TO CART.
$('.ajax_add_to_cart').on('click', function () {
// Fix
setTimeout( function () {
console.log('clicked');
// What data do we need to send? URL TO AJAX, ACTIONS
$.post(my_ajax_obj.ajax_url, {
_ajax_nonce: my_ajax_obj.nonce,
action: 'update_minicart',
}, function(response) {
// What do we want to do with response? REFRESH CART
console.log(response);
$('.cart-container').html(response);
});
}, 500);
});
});
I'm not sure of the stability or future-proofing of this fix but it works for now. I'd appreciate any information on why this happens myself.
Related
iam a beginner level wordpress developer ..now at halfway on creating newsletter plugin administration panel. In this admin panel iam using jquery.post ajax for submitting the form.Unfortunately the ajax is not working. But i am using the same wordpress ajax in my site's front end for taking email id from users and it's working fine.
i have verified all the code..unfortunately i couldn't find what's wrong in my code.
my jquery script
jQuery(document).ready(function(){
jQuery("#saveValue").click(function(){
jQuery("#apiData").submit();
});
jQuery("#apiData").submit(adminSettingSave('#apiData'));
});
function adminSettingSave(secti){
return function(){
var form_data = jQuery(secti).serialize();
form_data += '&action=settings-save';
alert(secti);
jQuery.post(ajaxurl, form_data, function (response) {
alert(response);
});
}
}
wordpress function
add_action( 'wp_ajax_settings-save', 'settings_save');
function settings_save()
{
die();
}
but this is not working, instead of this its reloading the page
please help me
How about change
jQuery("#apiData").submit(adminSettingSave('#apiData'));
to
jQuery("#apiData").submit(function() {adminSettingSave('#apiData'); return false});
I think your page reload because your submit event initialization do not work. The submit event listener function also need returning false to prevent default form action.
I'm working on a shopping cart and I'm having some difficulties with a concept.
Basicly, I remove two articles in a certain condition and it works perfectly.
My problem is that I want to redirect the user instantly if these two articles are removed from the cart.
When I enter the window.location, it is refreshing the page but it's not updating the cart from the ajax call.
So what I want to achieve is that after these two ajax calls, i want to be redirected but also, the ajax call should do their things in the delete_item.php :)
I'm using jQuery inside a normal javascript file on a certain function.
function deleted(id, pozitie) {
var msg = 0;
$(document).ready(function(){
$('.' + id).each(function() {
$(this).remove();
$('.sm').remove();
ajaxpage("delete_item.php?id="+pozitie+"&ord="+ordrno,"error");
ajaxpage("delete_item.php?id="+(pozitie+1)+"&ord="+ordrno,"error");
window.location="http://mypage.com/offer";
msg = 1;
});
});
Use a callback function and redirect your page if your ajax call return success. Hope it works.
ajaxpage("delete_item.php?id="+pozitie+"&ord="+ordrno,"error",callbackfn);
Callback funfction:
function callbackfn(){
window.location="http://mypage.com/offer";
}
I am trying to get this jquery tool to work with my website for file uploads: https://github.com/blueimp/jQuery-File-Upload and I have it working for the most part. The only issue I am having is trying to find a place where I can make it redirect to a new page to display the results of the upload instead of displaying them on the same page. I want to have the option for the users to do things with the images and files they just uploaded, and a new page makes more sense for this than using the same page over and over and over again...
Can anyone tell me from experience of understanding something in the documentation/code that I didn't grasp on how I can do this? Thanks :) Please note that i want the page to be redirected at the end of ALL of the uploads and not after the first if there are four more. I will be storing the data in a PHP session to access on the next page.
Should be like this (in this case redirecting to http://stackoverflow.com):
<script>
$(function () {
$('#fileupload').bind('fileuploadstop', function (e) {/* ... */});
});
</script>
Check out the docs.
<script>
$(function () {
$('#fileupload').fileupload({
// ... additional parameters
complete: function (e, data) {
window.location = "http://www.stackoverflow.com";
}
});
});
</script>
The plugin has an events called onComplete and onCompleteAll. Use them.
I have some problems with redirecting/reloading after a successful ajax call.
Here is the situation:
I have item for deletion saved in an array. When I click on a button it calls for PHP file via ajax, and after success I need to reload the page. But I have some problem doing this.
I searched the internet and couldn't find a working solution.
I have PHP file which goes through the array deleting item by item from the database.
foreach($arrayVals as $key=>$val)
{
//bla bla
}
Also, I have jQuery part:
$("#button").live("click",function(){
$.ajax({
url, data, type... not important
success: function(html){
location.reload();
}
});
});
I mean, the code works, but not good. It does delete items, but not all of them and then it reloads the page.
Like, if I have 10 items to delete, it deletes like 6-7, and 3-4 items stay undeleted.
It acts like it reloads the page too soon, like PHP file does not have enough time to process everything :D
BrixenDK is right.
.ajaxStop() callback executed when all ajax call completed. This is a best place to put your handler.
$(document).ajaxStop(function(){
window.location.reload();
});
You use the ajaxStop to execute code when the ajax are completed:
$(document).ajaxStop(function(){
setTimeout("window.location = 'otherpage.html'",100);
});
use this Reload page
success: function(data){
if(data.success == true){ // if true (1)
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 5000);
}
}
Using the ajaxSuccess to reload the page after ajax success.
$(document).ajaxSuccess(function(){
window.location.reload();
});
I'd like to add a simple functionality to my pages, where a user will see a "follow" button and by clicking it a db record will be created (userID and pageID). I'll handle query on the backend, I suppose. I think I need to do it in AJAX, but I havebn't done much with AJAX. I was also thinking that updating the button status from FOLLOW to FOLLOWING (or something similar) I could do with jQuery, with some sort of toggle, while the request is being processed on the background.
Am I on the right track with this?
You're on the right track.
I've created an example which uses a button like <input type="image" class="follow">. When I user clicks on it it sends a request to the server (url). On success it updates the button image.
$('input[type=image].follow').click(function() {
var button = $(this);
var current_img = $(button).attr('src');
var current_alt = $(button).attr('alt');
$(button).attr('src', '/style/icons/ajax-loader.gif');
$(button).attr('alt', 'Requesting data from the server...');
$.ajax({
url: url of script the processes stuff (like db update),
type: 'POST',
data: {},
dataType: "json",
error: function(req, resulttype, exc)
{
$(button).attr('src', '/style/error.png');
$(button).attr('alt', 'Error while updating!');
window.setTimeout(function() {
$(button).attr('src', current_img);
$(button).attr('alt', current_alt);
}, 3000);
},
success: function(data)
{
$(button).attr('src', '/style/followed.png');
$(button).attr('alt', 'Followed');
}
});
return false;
});
Above is just some example code. Change it at your will. Have fun with it.
AJAX is right, jQuery makes ajax easy.
//Post with jQuery (call test.php):
$.post('test.php', function(data) {
//Do something with result data
});
It sounds like you are on the right track here. If you're working with a smaller application then using an AJAX request and creating your record would be easiest using a Java servlet and putting for example some JDBC code in your doGet or doPost method to perform the database operations.
At the same time your onSuccess method for your AJAX request can call the jQuery code necessary to update your button. Good Luck!