You can adjust what items are shown in the menu via the wp_get_nav_menu_items
. This is the same filter that I am using in my Nav Menu Roles plugin. Adapting from that, we can loop through the menu items, and check if any of them are links to the “checkout” page. From there we can test if the cart is empty. If both are true, then we remove the item from the menu.
function wpa_120740_exclude_menu_items( $items ){ //var_dump( $items );
global $woocommerce;
if( is_admin() || ! function_exists( 'woocommerce_get_page_id' ) ) return $items;
// Iterate over the items to search and destroy
foreach ( $items as $key => $item ) {
if( $item->object_id == woocommerce_get_page_id( 'checkout' ) ){
$cart_contents = $woocommerce->cart->get_cart();
if( empty ( $cart_contents ) ){
unset( $items[$key] ) ;
}
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'wpa_120740_exclude_menu_items' );