1

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).

  • 1
    Have you tried this one https://stackoverflow.com/questions/12272733/woocommerce-get-variation-product-price or this https://wordpress.stackexchange.com/questions/296193/get-woocommerce-product-price-by-id? – iismaell Sep 30 '21 at 02:25

1 Answers1

0

You can also get a price base on variation id like this -

$price = get_post_meta($variation_id, '_price', true);
echo $price;

Might be can help you to fix for temporary