I'm trying to get a variation's price from its ID inside an ajax call. I can't figure out why wc_get_product() is returning an empty object.
Here's my Javascript code for the ajax call:
$(document).on('change', '#product-info select', function() {
var variation_id = $(this).find(':selected').val(),
variation_name = $(this).find(':selected').text(),
data = {
action: 'woocommerce_ajax_quantity_change',
variationid: variation_id
};
$.ajax({
type: 'post',
url: wc_add_to_cart_params.ajax_url,
data: data,
success: function(response) {
console.log(response);
}
});
});
And this is my PHP file, where the $variation is returning an empty array:
function woocommerce_ajax_quantity_change() {
$variationid = empty($_POST['variationid']) ? 0 : absint($_POST['variationid']);
$variation = wc_get_product($variationid);
echo wp_send_json($variation);
wp_die();
}
$variationid is fine and returning the right variation's ID (already checked).