Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Block Element (Custom Hook) Not Displaying on Empty Cart Page

  • Hi there,

    I’m trying to add a block element using Elements, with the goal of displaying it on the “Cart is empty” page. I’ve selected:

    Hook: Custom Hook – woocommerce_cart_is_empty
    Location: Pages – Cart

    I followed the instructions provided by David in this thread: Empty Cart Page Template, but the block is not appearing.

    I also tried using a hook element with the same settings, but it still doesn’t display.

    Additionally, I attempted using a custom PHP snippet that should display a random message on the empty cart page, but again, nothing appeared.

    I’ve cleared the cache and even disabled caching entirely, but the issue persists.

    Any advice on what might be causing this or how to resolve it?

    Thanks!

  • Hi there,

    your site is using the Woocommerce Blocks for the Cart page, which means those hooks mentioned in the other topic do not exist. As they are only present in the classic PHP Templates.

    The new blocks are way more complicated to integrate with as they support very few of the legacy hooks, and now require Javascript slot and fill methods to manipulate them. Which is something you would need woo support or a developer for.

    For reference , here is the docs for extending Woo blocks:
    https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/third-party-developers/extensibility/README.md#hooks-actions-and-filters

    Alternatively you could either:

    A. not use the Woo checkout block

    B. use a redirect for the empty cart page , here’s an example snippet for that:

    
    add_action('template_redirect', 'redirect_if_cart_empty');
    
    function redirect_if_cart_empty() {
        if (is_cart() && WC()->cart->is_empty()) {
            $redirect_url = home_url('/shop'); // Change '/shop' to the URL of your desired page
            
            wp_safe_redirect($redirect_url);
            exit;
        }
    }
    
  • Hi,

    Thanks for the snippet and clarification about the WooCommerce Blocks for the Cart page. I wasn’t aware that there were new Woo blocks.

    Considering the complexity of integrating with the new blocks and the limited support for legacy hooks, the alternative of using a redirect for the empty cart page seems like the better option.

    However, I have an issue where, if I’m already on the cart page with items and I click to remove an item, the empty cart page appears instead of triggering the redirect.

  • That would require some Javascript. But I am not sure if the following still works with the new cart blocks:

    
        <script type="text/javascript">
            jQuery(function($) {
                $(document.body).on('updated_wc_div', function() {
                    if ($('.cart-empty').length > 0) {
                        window.location.href = '/shop';  // Change '/shop' to your desired URL
                    }
                });
            });
        </script>
    

    You can hook it into your cart page in the wp_footer took.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.