I'm using Woocommerce Bookings with only a base cost filled. No other rules. How do I display product price in a loop like this?
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
ID is: <?php the_ID(); ?>
Title is: <?php the_title(); ?>
Price: <?php echo esc_html( get_post_meta( get_the_ID(), '_regular_price', true ) ); ?>
<?php endwhile; wp_reset_query(); ?>
The price field didn't show anything
UPDATE
another code I tried:
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = get_post_meta( $product_id, '_wc_booking_pricing', true);
?>
<?php echo $product_pricing; ?>
<?php echo $base_price;?>
both also return the value of zero
ANOTHER TRY
<?php
global $woocommerce;
$product = new WC_Product_Booking($productID);
$product = wc_get_product( $product_id );
$base_price = $product->get_price();
$product_data = $product->get_data();
$product_pricing = $product_data['pricing'];
foreach($product_pricing as $key => $princing ){
$pricing_type = $princing['type'];
$pricing_base_cost = $princing['base_cost'];
$pricing_base_modifier = $princing['base_modifier'];
$pricing_cost = $princing['cost'];
$pricing_modifier = $princing['modifier'];
$pricing_from = $princing['from'];
$pricing_to = $princing['to'];
}
$pricing_data = get_post_meta( $product_id, '_wc_booking_pricing', false); ?>
<?php echo $pricing_data; ?>
nothing works :( they're all either showing blanks or showing zero. Can anyone help point in the right direction?
many thanks
Try (from the docs):
$product = new WC_Product_Booking($productID);
//don't know if you need 'echo'
echo $product->get_price_html()
If not, I think the logic of 2nd snippet it's ok, but probably productID != postID, so another solution would be:
My answer's too long for being posted in the comments.
Are you sure that you are set correctly the product from wp-admin?
In order to do the second suggestion, you need access to the database. Get the ID of the post(product) in wp-admin when you create/modify it, you should have in the url something like {URL}/wp-admin/id=123.
Once you have the ID go in wp_post_meta table in the DB and look for all the fields with that post_id, and check if you can find the correct price.
Once you find it, you can call get_post_meta(correctID,correctKey)
It seems like you're pulling the right meta key, could be related with you using esc_url()
, when you should be using just esc_html()
. I'd try that.