Check programatically if Cart or Checkout Blocks are used in WooCommerce
Check programatically if Cart or Checkout Blocks are used in WooCommerce
How to identify in the backend if the store is using Checkout blocks [classic_checkout] or WooCommerce checkout with shortcode [woocommerce_checkout].
I'm testing a payment gateway and I use enqueue_script to add the JS to the frontend. What is the best way to validate whether a store is using the Blocks-compatible [classic_checkout] block or not, so that scripts can only be queued when necessary?
I finally found the way via has_block_in_page( $page, $block_name ) function located in WC_Blocks_Utils Class. You can use it for Checkout as follows:
// Conditional function that check if Checkout page use Checkout Blocks
function is_checkout_block() {
return WC_Blocks_Utils::has_block_in_page( wc_get_page_id('checkout'), 'woocommerce/checkout' );
}
For cart page, you can use:
// Conditional function that check if Cart page use Cart Blocks
function is_checkout_block() {
return WC_Blocks_Utils::has_block_in_page( wc_get_page_id('cart'), 'woocommerce/cart' );
}
Tested and works
February 13, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
February 13, 2024