I have a function that checks if the WooCommerce cart is empty, and if it is, adds a button to the page header that links to my shop page. However, this code only works on page load, but won’t update (and remove the button) after adding to cart via AJAX. How do I detect if an item has been added to the cart?
function shop_button_when_empty_cart() {
if ( WC()->cart->get_cart_contents_count() == 0 ) { ?>
<a href="/shop"><button>Your cart is empty, go to shop</button></a>
<?php
}
}
add_action( 'storefront_header', 'shop_button_when_empty_cart', 40 );