Reputation: 1362
When creating the PHP for an AJAX function in my woocommerce checkout page, I use a $.post. What all needs to be in the PHP (via $.post) in order to call $woocommerce->session->set
I tried the following in the php of the $.post
global $woocommerce;
$woocommerce->session->set('_delivery_loading', $temp);
However the PHP is reporting that it does not know what $woocommerce->session->set is.
How does the PHP that is the target of a $.post (from AJAX) know about woocommerce?
Here is my callback php (test.php):
<?php
global $woocommerce;
$temp = $_GET["pass_var"];
$woocommerce->session->set('_delivery_loading', $temp);
?>
And here is my jQuery in my checkout.php
<script>
jQuery(document).ready(function($){
$("#myradiobutton").change(function(){
var padded_variable = "0";
$.get('test.php',{pass_var:passed_variable}, function(data) {
console.log(data);
});
location.reload();
});
});
Upvotes: 2
Views: 1614
Reputation: 1362
So I discovered the answer.
Turns out that your PHP callback has to include
include($_SERVER["DOCUMENT_ROOT"] . "/wp-blog-header.php");
at the top in order to call WC functions.
Upvotes: 2