0

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');
    }
    ...

}

1 Answer 1

0

Note: 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.

7
  • Ah, of course, I am doing the same... Just missed adding in my sample code. My bad. Apologies Oct 19, 2020 at 7:50
  • Did you add the add_action() code in the correct file and place?
    – Sally CJ
    Oct 19, 2020 at 8:26
  • Yes, I am add_action() in the correct place. I am not sure if WordPress allows or not for it. The callback function is working fine. Adding items to the cart successfully. If I use the wp_mail within the code as shown in the sample, it works but not the do_action Oct 19, 2020 at 8:29
  • Are all those add_action() calls in the same file, can you show how you call the AJAX callback? Or try with the one in my answer - put them in the theme functions file, or in the main plugin file if you're writing a plugin.
    – Sally CJ
    Oct 19, 2020 at 8:32
  • 1
    Let us continue this discussion in chat.
    – Sally CJ
    Oct 19, 2020 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.