Archived topic
Show WooCommerce price field using query loop?
7 replies · Started by Phil on October 2, 2022
Hi,
Is there a way to show a woocommerce price field using the generate blocks query loop?
I've got a query loop that shows the image and product name but I can't see how to show any of the woocommerce fields.
Thanks,
Phil
Hi Phil,
You can use a Dynamic GB Headline Block.
Just set the Source to Post Meta, and the field name to _regular_pricefor instance.
Reference: https://docs.generateblocks.com/article/headline-overview/#dynamic-data
Great, thanks Fernando.
Is there a good way to format the field as currency, including the thousands separator?
Try adding my-query-price to the Class list of the Headline Block in its Advanced setting section. Then add this PHP snippet:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-query-price' ) !== false ) {
$content = '$'. number_format($content);
}
return $content;
}, 10, 3);
Replace $ with your currency symbol.
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
thanks Fernando, perfect. :-)
Your welcome Phil! :)
Hi David, it seems like the PHP code above to display the format is not working. I 've tried it locally. And is it also possible to display a sale badge above the right of the product image?
Hi there,
can you share a link to where we can see the issue ?