- This topic has 22 replies, 3 voices, and was last updated 5 years, 1 month ago by Tom.
-
AuthorPosts
-
February 8, 2019 at 10:26 am #805290Pedro
Hello Tom! The problem is that I want him to be discriminated against haha.
I have tried with the code of the article that you sent me, and it works, the problem is that when there is nothing in the cart it is hidden.
Is there any way that the cart is displayed when there is nothing added?
February 8, 2019 at 12:37 pm #805372PedroHello?
February 8, 2019 at 2:32 pm #805424PedroCan u help me please?
February 8, 2019 at 4:53 pm #805465TomLead DeveloperLead DeveloperTry this:
function tu_custom_wc_cart_link() { ob_start(); $no_items = ''; if ( ! WC()->cart->get_cart_contents_count() > 0 ) { $no_items = 'no-items'; } ?> <a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="cart-contents" title="<?php esc_attr_e( 'View your shopping cart','generate-woocommerce' ); ?>"> <?php echo sprintf ( _n( '<span class="number-of-items ' . $no_items . '">%d</span>', '<span class="number-of-items ' . $no_items . '">%d</span>', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> </a> <?php return ob_get_clean(); } function tu_custom_wc_menu_cart( $nav, $args ) { if ( $args->theme_location == 'primary' && generatepress_wc_get_setting( 'cart_menu_item' ) ) { return sprintf( '%1$s <li class="wc-menu-item %4$s" title="%2$s"> %3$s </li>', $nav, esc_attr__( 'View your shopping cart','generate-woocommerce' ), tu_custom_wc_cart_link(), is_cart() ? 'current-menu-item' : '' ); } // Our primary menu isn't set, return the regular nav return $nav; } function tu_custom_wc_mobile_cart_link() { if ( function_exists( 'generatepress_wc_get_setting' ) && ! generatepress_wc_get_setting( 'cart_menu_item' ) ) { return; } ?> <div class="mobile-bar-items wc-mobile-cart-items"> <?php do_action( 'generate_mobile_cart_items' ); ?> <?php echo tu_custom_wc_cart_link(); ?> </div><!-- .mobile-bar-items --> <?php } add_filter( 'woocommerce_add_to_cart_fragments', 'tu_wc_cart_link_fragment' ); function tu_wc_cart_link_fragment( $fragments ) { global $woocommerce; $fragments['.cart-contents span.number-of-items'] = ( WC()->cart->get_cart_contents_count() > 0 ) ? '<span class="number-of-items">' . wp_kses_data( WC()->cart->get_cart_contents_count() ) . '</span>' : '<span class="number-of-items no-items"></span>'; return $fragments; } add_action( 'after_setup_theme','tu_remove_wc_cart_item' ); function tu_remove_wc_cart_item() { remove_filter( 'wp_nav_menu_items','generatepress_wc_menu_cart', 10, 2 ); add_filter( 'wp_nav_menu_items','tu_custom_wc_menu_cart', 10, 2 ); remove_action( 'generate_inside_navigation','generatepress_wc_mobile_cart_link' ); remove_action( 'generate_inside_mobile_header','generatepress_wc_mobile_cart_link' ); add_action( 'generate_inside_navigation','tu_custom_wc_mobile_cart_link' ); add_action( 'generate_inside_mobile_header','tu_custom_wc_mobile_cart_link' ); }
Then add this CSS:
span.number-of-items.no-items { font-size: 0; } span.number-of-items.no-items:before { font-size: 14px; }
While we try to answer topics as fast as possible, it can take us up to 12 hours sometimes (usually much less).
February 8, 2019 at 5:10 pm #805467PedroHello Tom! If it worked, however the cart is shown with a “0”.
Is it possible that only the cart icon is displayed? And as the articles are added, the numbers appear.
Thank you!
February 8, 2019 at 5:23 pm #805475TomLead DeveloperLead DeveloperI just edited the code above, and included some CSS. Can you try now?
February 8, 2019 at 5:33 pm #805482PedroIt worked perfectly, thank you very much!
February 8, 2019 at 6:20 pm #805504TomLead DeveloperLead DeveloperYou’re very welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.