Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using woocommerce plugin in my WordPress setup. I want the Checkout in the menu bar should appear only when there is something in the cart. How can I do this?

share|improve this question
1  
what is the question? –  Mark Kaplun Oct 31 '13 at 7:07
    
If I understand correctly, the OP would like to show a Checkout menu item when there is something in the cart. @Rekha Haridoss could you confirm? The question and especially title should be edited if that is the case. –  helgatheviking Nov 1 '13 at 20:56
add comment

1 Answer

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' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.