Get product object/ID using woocommerce_format_sale_price hook and woocommerce blocks
I’ve created some blocks containing grids of products i.e. "products by category" block.
I want to manipulate the price string using woocommerce_format_sale_price hook. The problem is I can’t access these products or their ID’s in these blocks. Before adding these blocks I used global $product
without problems to manipulate the price strings on the rest of the site. With these blocks using ‘global $product’ returns NULL. Is there any other way to access product data or product ID of products inside these blocks using this filter?
This snippet worked for the rest of the site but it won’t work with these blocks.
add_filter( 'woocommerce_format_sale_price', 'xyz_filter_woocommerce_format_sale_price', 10, 3 );
function xyz_filter_woocommerce_format_sale_price( $price, $regular_price, $sale_price ) {
global $product;
$product_id = $product->get_id();
}
Leave an answer
You must login or register to add a new answer .