admin管理员组文章数量:1319022
I want to perform some actions when ajax tasks done successfully. For instance, if the item added to the cart, I want to send an email. Also may perform a few more actions.
Setting up do_action('prefix_item_added_to_cart', $args);
doesn't recognized and so add_action
doesn't perform a task, in my case sending email.
If I write code procedural code to send an email, that works fine but using do_action
.
Does not work
// ajax callback function
function my_ajax_callback() {
...
// add item to cart
$cart =. new Cart();
$cart_id = $cart->add_item($params);
// if item added successfully
if($cart_id){
// perform action
do_action('prefix_item_added_to_cart', $args);
}
...
}
// action callback
function send_email_to_user($args) {
// send email notification
wp_mail('set params for email');
}
// action
add_action('prefix_item_added_to_cart', 'send_email_to_user', $args);
Works
function my_ajax_callback() {
...
// add item to cart
$cart =. new Cart();
$cart_id = $cart->add_item($params);
// if item added successfully
if($cart_id){
// send email notification
wp_mail('set params for email');
}
...
}
I want to perform some actions when ajax tasks done successfully. For instance, if the item added to the cart, I want to send an email. Also may perform a few more actions.
Setting up do_action('prefix_item_added_to_cart', $args);
doesn't recognized and so add_action
doesn't perform a task, in my case sending email.
If I write code procedural code to send an email, that works fine but using do_action
.
Does not work
// ajax callback function
function my_ajax_callback() {
...
// add item to cart
$cart =. new Cart();
$cart_id = $cart->add_item($params);
// if item added successfully
if($cart_id){
// perform action
do_action('prefix_item_added_to_cart', $args);
}
...
}
// action callback
function send_email_to_user($args) {
// send email notification
wp_mail('set params for email');
}
// action
add_action('prefix_item_added_to_cart', 'send_email_to_user', $args);
Works
function my_ajax_callback() {
...
// add item to cart
$cart =. new Cart();
$cart_id = $cart->add_item($params);
// if item added successfully
if($cart_id){
// send email notification
wp_mail('set params for email');
}
...
}
Share
Improve this question
edited Oct 19, 2020 at 7:50
pixelngrain
asked Oct 19, 2020 at 7:07
pixelngrainpixelngrain
1,3901 gold badge23 silver badges50 bronze badges
1 Answer
Reset to default 0Note: OP and me resolved the question/issue via chat — and despite the actual problem source is not known for sure, in the actual AJAX callback used by OP, there was actually no (or maybe OP had forgotten to make the) do_action()
call. :)
Original Revised Answer (for reference):
If you have something like so in your actual code, i.e. the actions are registered properly:
add_action( 'wp_ajax_<your action>', 'my_ajax_callback' ); // for logged-in users
add_action( 'wp_ajax_nopriv_<your action>', 'my_ajax_callback' ); // for all other users
add_action( 'prefix_item_added_to_cart', 'send_email_to_user' );
Then I don't see why would the callback function is not getting called — unless of course, if the conditional (if ( $cart_id )
) returns false
.
But what's that $args
in your add_action()
call? That parameter should be an integer, which is the callback priority.
本文标签: actionsdoaction won39t work in ajax callback
版权声明:本文标题:actions - do_action won't work in ajax callback 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742050847a2418055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论