I want to make an AJAX call to a PHP file where I can use a WooCommerce function. What I want is something like this:
var price = 20.40;
$.ajax({
type: 'POST',
url: '/get_formatted_price.php',
dataType: "text",
data: {
price: price
},
success: function(data) {
var formattedPrice = data;
console.log(formattedPrice);
}
});
My PHP file (get_formatted_price.php) is like this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
global $woocommerce;
$price = $_POST["price"];
$price = wc_price($price);
echo $price;
}
Trying to find out why it doesn't work.