How to call a function from functions.php with ajax?

by arabtornado   Last Updated September 21, 2018 13:08 PM

I'm wondering how can i call a function from functions.php and print it with Ajax in a specific place (woocommerce_before_add_to_cart_button).

I know i can simply add my function in add_action like what i did below but i though that ajax is the way to go because my function output got cached (full page caching) so i though that ajax can be a solution for that issue.

<?php
  add_action( 'woocommerce_before_add_to_cart_button', 'hello_world', 10);
    function hello_world() 
      {
        $var1 = 'Hello World!';
        echo $var1;
        echo "The time is " . date("h:i:sa");
      }
?>

I would appreciate if the answers was very simplified since i'm totally new to coding. Thanks in advance



Answers 1


You mean need to display the custom code your ajax add to cart button right? . add this below code your current active function.php file

add_action( 'wp_ajax_woocommerce_before_add_to_cart_button', 'hello_world', 10);
add_action( 'wp_ajax_nopriv_woocommerce_before_add_to_cart_button', 'hello_world', 10);
add_action( 'woocommerce_before_add_to_cart_button', 'hello_world', 10);
function hello_world() {
$var1 = 'Hello World!';
echo $var1;
echo "The time is " . date("h:i:sa");
}
Kanewilliam
Kanewilliam
September 21, 2018 12:22 PM

Related Questions



Click loads template via ajax

Updated August 06, 2015 17:03 PM


Multiple Notifications SetInterval

Updated July 11, 2018 00:08 AM

dynamic dependent select dropdown

Updated July 24, 2018 17:08 PM