0

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

0

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");
}
  • Thanks for your reply, my function is already placed in the add_action similar to what you mentioned but unfortunately the output got cached since I'm using W3 Total cache page caching so i though that calling that function via ajax can solve that issue and return the dynamic output of that function – arabtornado Sep 21 '18 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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