skip to Main Content

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?

2

Answers


  1. 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

    Login or Signup to reply.
  2. I tried this approach:

    function is_checkout_block() {
        $page_to_check = wc_get_page_id ('checkout');
        $content = get_post($page_to_check)->post_content;
        return strpos($content, 'block') !== false;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search