To add Product Price block in WPGridBuilder
Include a product price block in WPGridBuilder layouts.
// Register a custom block for display priduct price (html)
if (!function_exists('awdr_display_product_price_html')) {
function awdr_display_product_price_html() {
// To get the post object.
$object = wpgb_get_object();
if (!isset($object->post_type)) {
return;
}
// To get the product object.
$product = function_exists('wc_get_product') ? wc_get_product($object->ID) : null;
if (!is_object($product) || !method_exists($product, 'get_price_html')) {
return;
}
// To display price (html).
echo $product->get_price_html();
}
// Register block for price (html)
add_filter('wp_grid_builder/blocks', function($blocks) {
$blocks['awdr_product_price'] = [
'name' => 'Product Price',
'render_callback' => 'awdr_display_product_price_html',
];
return $blocks;
});
}