Archived topic
Show product price in query block
3 replies · Started by Kees on March 9, 2023
Hi,
We are building a website for a hotel. On the home page they want three blocks with information about three different rooms. We can not get the priceof each room in her. Can you tellus how to show the price of each room? Maybe with a shortcode?
Thank you!
Hi there,
when you say product. Is this using Woocommerce or is it a regular post type with the price saved in the post meta ?
Hi,
Yes, it is a WooCommerce product with a queryblock. We use generateblocks pro. So we show 3 products and we need to get the price.
How do we fix this?
Regards,
Niek
Option 1 - the unformatted price:
Use a Headline with > Dynamic Data
Data Source: Current Post
Content Source: Post Meta
Post Meta Field: _price
Option 2 - the formatted price
1. Add this PHP Snippet to your site:
add_filter( 'render_block', function( $block_content, $block ) {
if (
! empty( $block['attrs']['className'] )
&& 'ql-price' === $block['attrs']['className']
) {
$price = get_post_meta( get_the_ID(), '_price', true );
$formatted_price = wc_price( $price );
$block_content = str_replace( 'ql-price', $formatted_price , $block_content );
}
return $block_content;
}, 10, 2 );
2. Add a Headline block to your loop, and give it a static text of: ql-price
And in Advanced > Additional CSS Class(es) add: ql-price
NOTE: in both options we set the _price as the post meta field.
Woo has other values including _regular_price and _sale_price
I have tested both methods on a simple product