Use WooCommerce functions in PHP file

Viewed 169

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.

1 Answers

I am not 100% certain on this but requiring the wp-load.php file within your php script should bring in the required functionality:

require_once("wp-load.php"); //change path as required 
Related