1

I'm working on a site that uses the Events Tickets plugin, and I need to get the price of a ticket (Woo product) by ID. I'm using wc_get_product( $ticketID )->get_price(); to to do this.

Adding the price to the page using the code below gives the correct price (e.g. 30):

echo '<input type="hidden" id="ticketpricetest" value="' . wc_get_product( 28612 )->get_price() . '">';

However, when I try and get the price using the same method within an Ajax call it returns 300:

function get_ticket_price() {
  $ticketID = $_REQUEST['ticketID'];
  echo wc_get_product( $ticketID )->get_price();
}
add_action( 'wp_ajax_get_ticket_price', 'get_ticket_price' );
add_action( 'wp_ajax_nopriv_get_ticket_price', 'get_ticket_price' );

I've also tried using get_regular_price but that also returns 300.

Any ideas what's going on here?

1
  • Are you certain $ticketID is correct? Are you passing it as an integer? Dec 2, 2021 at 14:55

1 Answer 1

1

It's important to exit the function when using the wp_ajax hooks. Hopefully this fixes it.

function get_ticket_price() {
  $ticketID = $_REQUEST['ticketID'];
  echo wc_get_product( $ticketID )->get_price();

  // Don't forget to stop execution afterward.
  wp_die();
}
add_action( 'wp_ajax_get_ticket_price', 'get_ticket_price' );
add_action( 'wp_ajax_nopriv_get_ticket_price', 'get_ticket_price' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.