Why i can’t get custom fields value or post ID via Ajax?

I wrote a code that generate a link based on the visitor location and it worked perfectly but i discovered that the generated code get cached since i’m using a full page caching so i though to solve that issue i can use ajax to load that link. I used the below code which worked perfectly in getting some variables that i need such as location variable and link domain variable etc.. however i’m unable to get the WooCommerce custom field data or even the product id it just return blank.

I’m using this code to get the custom field which worked perfectly when used directly in function however can’t get it to work in ajax

$uk_asin = get_post_meta(get_post()->ID, “wccaf_uk_asin”, true );

and i used that code on this link to do the ajax

I added the first snippet in a function and hooked it using add_action on functions.php to view it on my product page and the second snippet on the functions.php as well.

I would really appriciate if the answer was simple since i’m still very new to coding

Read more here:: Why i can’t get custom fields value or post ID via Ajax?

Leave a Reply

Your email address will not be published. Required fields are marked *