I have created the code to loop through the product list and display the price

  $args = array( 'post_type' => 'product', 'posts_per_page' => 100, 'product_cat' => 'hot-deals');
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post(); 

global $product; 


$xml .= '<Original_price>' . $product->get_display_price( $product->get_regular_price() ) . '</Original_price>';
$xml .= '<Discount_price>' . $product->get_display_price() . '</Discount_price>';

echo $product->get_price_html();

endwhile;
wp_reset_query();

get_price_html() works perfectly and display the price like this:

From: $ 621 $ 559

However , I would like to get the price separately

I can get the sales price with

$product->get_display_price()

The problem is, I can not get the original price,

I tried $product->get_regular_price() , that return nothing

And I tried $product->get_display_price( $product->get_regular_price() ), that return the sales price

So how to get the original price ? Thanks a lot.

2 Answers
2

I’m pretty sure that the problem is, that the WP_Query returns post objects, which are not identical to Woocommerces product objects. While you are trying to get the product data by getting the according global, that won’t work, especially because the query you are performing does nothing to that global. Now what I probably would do is, firstly, performing the query with the fields parameter set to ids. Secondly, when looping over the returned array of ids, I would suggest you get the product object with wc_get_product(), which should give you all the information you need.


Note: I answered this to enlighten about the difference in return between WordPress’ and Woocommerces object. And to make clear, while product is a CPT it isn’t necessarily optimally usable with WP’s standard query. There is a strong argument though for your question being off topic, because it is about a third party plugin. Please take a look at our help center to learn more about our site guidelines.

Leave a Reply

Your email address will not be published. Required fields are marked *