Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #628225

    ChadThiele
    Participant

    I love the super cool looking WooCommerce cart in the menu — the integration is awesome… but it is confusing my members to have the cart visible when there are no items in it.

    Is there some way to hide this cart when there is nothing in it?

    Thanks!

    #628254

    Prasant Rai
    Moderator

    Hello Chad,

    Thanks for writing in!

    Regretfully, this particular customization request is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. Having said that X theme is fully compatible with WooCommerce but we can’t change the core functionality of the plugin since developers of WooCommerce update the plugin regularly and some of the update might compatibility issues. But I could point you in the right direction with the understanding that it would ultimately be your responsibility to take it from here. We request you to kindly walk-through following thread:

    https://wordpress.org/support/topic/make-it-hide-with-0

    Thank you for your understanding.

    #629335

    ChadThiele
    Participant

    What file in the X theme handles the output of this cart? I can’t seem to find it — to override.

    Thanks.

    #629370

    Thai
    Moderator

    Hi There,

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    Add this code at your child theme’s functions.php

    add_action( 'wp_footer', 'x_hide_cart' );
    function x_hide_cart(){
    	if ( WC()->cart->get_cart_contents_count() == 0 ) {
    		?>
    		<style type="text/css">.x-menu-item-woocommerce{display: none;}</style>
    		<?php
    	}
    }

    Hope this helps.

    #629401

    ChadThiele
    Participant

    I found it… and actually, this is something you could probably build into the customizer. In woocommerce.php, I used WC()->cart->cart_contents_count (which you already used in the same location).

    I simply wrapped the output, to only output if there are more than 0 items in the cart:

          if (WC()->cart->cart_contents_count > 0) {
            return $cart_output;
          }

    Thanks!

    #629658

    Rue Nel
    Moderator

    Hi Chad,

    We certainly appreciate the feedback! This is something we can add to our list of feature requests. This way it can be taken into consideration for future development. All of these items are discussed with our team internally and prioritized based on the amount of interest a particular feature might receive. Thanks!

    If you did some changes in woocommerce.php file, those changes will be overwritten when an X update is released. It is not safe. Please make sure that all your customizations is being placed inside your child theme’s functions.php file. For this issue, you can also make use of this code in your child theme’s functions.php file

    //
    // Outputs a navigation item with the cart.
    //
    
    if ( ! function_exists( 'x_woocommerce_navbar_menu_item' ) ) :
      function x_woocommerce_navbar_menu_item( $items, $args ) {
    
        if ( X_WOOCOMMERCE_IS_ACTIVE && x_get_option( 'x_woocommerce_header_menu_enable', '' ) == '1' ) {
          if ( $args->theme_location == 'primary' && WC()->cart->cart_contents_count > 0 ) {
            $items .= '<li class="menu-item current-menu-parent x-menu-item x-menu-item-woocommerce">'
                      . '<a href="' . x_get_cart_link() . '" class="x-btn-navbar-woocommerce">'
                        . x_woocommerce_navbar_cart()
                      . '</a>'
                    . '</li>';
          }
        }
    
        return $items;
    
      }
      add_filter( 'wp_nav_menu_items', 'x_woocommerce_navbar_menu_item', 9999, 2 );

    Hope this helps. Please let us know how it goes.

    #879554

    MacMartino
    Participant

    I’m sorry – I had the same question and pasted your code on functions.php,
    but this gives an unexpected end of line error. I think some brackets are missing (?)

    #879562

    MacMartino
    Participant

    Chad had just a simple line of code:
    if (WC()->cart->cart_contents_count > 0) {
    return $cart_output;
    }

    But i cannot seem to find out where to put this code in woocommerce.php..

    #879957

    Paul R
    Moderator

    Hi,

    Sorry, there was a syntax error in the code.

    You can try this instead.

    
      function x_woocommerce_navbar_menu_item( $items, $args ) {
    
        if ( X_WOOCOMMERCE_IS_ACTIVE && x_get_option( 'x_woocommerce_header_menu_enable', '' ) == '1' ) {
          if ( $args->theme_location == 'primary' && WC()->cart->cart_contents_count > 0 ) {
            $items .= '<li class="menu-item current-menu-parent x-menu-item x-menu-item-woocommerce">'
                      . '<a href="' . x_get_cart_link() . '" class="x-btn-navbar-woocommerce">'
                        . x_woocommerce_navbar_cart()
                      . '</a>'
                    . '</li>';
          }
        }
    
        return $items;
    
      }
      add_filter( 'wp_nav_menu_items', 'x_woocommerce_navbar_menu_item', 9999, 2 );
    

    Hope that helps.

    #890379

    MacMartino
    Participant

    Is there a specific place to put it in woocommerce.php? or just at the end?

    #891205

    Paul R
    Moderator

    Hi,

    You need to add the code in your child theme’s functions.php file located at wp-content/themes/x-child

    Please add it after this line

    // Additional Functions
    // =============================================================================

    #995138

    MacMartino
    Participant

    If i copy the code and put it in the functions.php of the Child theme, I get an error:
    Parse error: syntax error, unexpected end of file in /home/bpr.nu/public_html/koot15.nl/wp-content/themes/x-child/functions.php on line 38

    Any idea how to fix this?

    So, my file contains:
    <?php

    // =============================================================================
    // FUNCTIONS.PHP
    // —————————————————————————–
    // Overwrite or add your own custom functions to X in this file.
    // =============================================================================

    // =============================================================================
    // TABLE OF CONTENTS
    // —————————————————————————–
    // 01. Enqueue Parent Stylesheet
    // 02. Additional Functions
    // =============================================================================

    // Enqueue Parent Stylesheet
    // =============================================================================

    add_filter( ‘x_enqueue_parent_stylesheet’, ‘__return_true’ );

    // Additional Functions
    // =============================================================================

    function x_woocommerce_navbar_menu_item( $items, $args ) {

    if ( X_WOOCOMMERCE_IS_ACTIVE && x_get_option( ‘x_woocommerce_header_menu_enable’, ” ) == ‘1’ ) {
    if ( $args->theme_location == ‘primary’ && WC()->cart->cart_contents_count > 0 ) {
    $items .= ‘<li class=”menu-item current-menu-parent x-menu-item x-menu-item-woocommerce”>’
    . ‘
    . x_woocommerce_navbar_cart()
    . ‘

    . ‘‘;
    }
    }

    return $items;

    #995180

    Christopher
    Moderator

    Hi there,

    Please update your code to :

    <?php
    
    // =============================================================================
    // FUNCTIONS.PHP
    // —————————————————————————–
    // Overwrite or add your own custom functions to X in this file.
    // =============================================================================
    
    // =============================================================================
    // TABLE OF CONTENTS
    // —————————————————————————–
    // 01. Enqueue Parent Stylesheet
    // 02. Additional Functions
    // =============================================================================
    
    // Enqueue Parent Stylesheet
    // =============================================================================
    
    add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
    
    // Additional Functions
    // =============================================================================
    
    function x_woocommerce_navbar_menu_item( $items, $args ) {
    
        if ( X_WOOCOMMERCE_IS_ACTIVE && x_get_option( 'x_woocommerce_header_menu_enable', '' ) == '1' ) {
          if ( $args->theme_location == 'primary' && WC()->cart->cart_contents_count > 0 ) {
            $items .= '<li class="menu-item current-menu-parent x-menu-item x-menu-item-woocommerce">'
                      . '<a href="' . x_get_cart_link() . '" class="x-btn-navbar-woocommerce">'
                        . x_woocommerce_navbar_cart()
                      . '</a>'
                    . '</li>';
          }
        }
    
        return $items;
    
      }
      add_filter( 'wp_nav_menu_items', 'x_woocommerce_navbar_menu_item', 9999, 2 );

    Hope it helps.

    #1009319

    MacMartino
    Participant

    Thanks Christopher, you ROCK!

    #1009455

    Nabeel A
    Moderator

    Glad we could help 🙂

    Cheers!