Digital experiences for all disciplines
New Landing › How can we help? › Atelier › Hide zero items in cart
New Landing › How can we help? › Atelier › Hide zero items in cart
- This topic has 7 replies, 4 voices, and was last updated 8 years by Kyle – SUPPORT.
-
Posted in: Atelier
-
June 7, 2016 at 5:49 pm #273001
Hi,
is it possible already – or adding a bit of code – to only show number of items in header cart if 1 or over?
June 8, 2016 at 8:07 am #273099Hi,
Please paste this code at functions.php of child theme:-function sf_get_cart() { $cart_output = ""; // Check if WooCommerce is active if ( sf_woocommerce_activated() ) { global $woocommerce, $sf_options; $show_cart_count = false; if ( isset( $sf_options['show_cart_count'] ) ) { $show_cart_count = $sf_options['show_cart_count']; } if ( sf_theme_opts_name() == "sf_atelier_options" ) { $cart_total = '<span class="menu-item-title">' . __( "Cart" , "swiftframework" ) . '</span>'; } else { $cart_total = WC()->cart->get_cart_total(); } $cart_count = $woocommerce->cart->cart_contents_count; $cart_count_text = sf_product_items_text( $cart_count ); $cart_count_text_alt = sf_product_items_text( $cart_count, true ); $view_cart_icon = apply_filters( 'sf_view_cart_icon', '<i class="ss-view"></i>' ); $checkout_icon = apply_filters( 'sf_checkout_icon', '<i class="ss-creditcard"></i>' ); $go_to_shop_icon = apply_filters( 'sf_go_to_shop_icon', '<i class="ss-cart"></i>' ); $cart_count_text_alt = ($cart_count_text_alt=='0')?'':$cart_count_text_alt; if ( $show_cart_count ) { $cart_output .= '<li class="parent shopping-bag-item"><a class="cart-contents" href="' . $woocommerce->cart->get_cart_url() . '" title="' . __( "View your shopping cart", "swiftframework" ) . '">'. apply_filters( 'sf_header_cart_icon', '<i class="ss-cart"></i>' ) . $cart_total . '<span class="num-items cart-count-enabled">' . $cart_count_text_alt . '</span></a>'; } else { $cart_output .= '<li class="parent shopping-bag-item"><a class="cart-contents" href="' . $woocommerce->cart->get_cart_url() . '" title="' . __( "View your shopping cart", "swiftframework" ) . '">'. apply_filters( 'sf_header_cart_icon', '<i class="ss-cart"></i>' ) . $cart_total . '<span class="num-items">' . $cart_count_text_alt . '</span></a>'; } $cart_output .= '<ul class="sub-menu">'; $cart_output .= '<li>'; $cart_output .= '<div class="shopping-bag">'; $cart_output .= '<div class="loading-overlay"><i class="sf-icon-loader"></i></div>'; if ( $cart_count != "0" ) { $cart_output .= '<div class="bag-header">' . $cart_count_text . ' ' . __( 'in the cart', 'swiftframework' ) . '</div>'; $cart_output .= '<div class="bag-contents">'; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); $price = apply_filters( 'woocommerce_cart_item_price', $woocommerce->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); $product_title = $_product->get_title(); $product_short_title = ( strlen( $product_title ) > 25 ) ? substr( $product_title, 0, 22 ) . '...' : $product_title; if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) { $cart_output .= '<div class="bag-product clearfix">'; $cart_output .= '<figure><a class="bag-product-img" href="' . get_permalink( $cart_item['product_id'] ) . '">' . $_product->get_image() . '</a></figure>'; $cart_output .= '<div class="bag-product-details">'; $cart_output .= '<div class="bag-product-title"><a href="' . get_permalink( $cart_item['product_id'] ) . '">' . apply_filters( 'woocommerce_cart_widget_product_title', $product_short_title, $_product ) . '</a></div>'; $cart_output .= '<div class="bag-product-price">' . __( "Unit Price:", "swiftframework" ) . ' ' . $price . '</div>'; $cart_output .= '<div class="bag-product-quantity">' . __( 'Quantity:', 'swiftframework' ) . ' ' . $cart_item['quantity'] . '</div>'; $cart_output .= '</div>'; $cart_output .= '<a href="#" class="remove remove-product" data-ajaxurl="'.admin_url( 'admin-ajax.php' ).'" data-product-id="'. $cart_item['product_id'] .'" data-product-qty="'. $cart_item['quantity'] .'" title="' . __( 'Remove this item', 'swiftframework' ) . '">×</a>'; $cart_output .= '</div>'; } } $cart_output .= '</div>'; if ( sf_theme_opts_name() == "sf_atelier_options" ) { $cart_output .= '<div class="bag-total">'; if ( class_exists( 'Woocommerce_German_Market' ) ) { $cart_output .= '<span class="total-title">' . __( "Total incl. tax", "swiftframework" ) . '</span>'; } else { $cart_output .= '<span class="total-title">' . __( "Total", "swiftframework" ) . '</span>'; } $cart_output .= '<span class="total-amount">' . WC()->cart->get_cart_total() . '</span>'; $cart_output .= '</div>'; } $cart_output .= '<div class="bag-buttons">'; $cart_output .= '<a class="sf-button standard sf-icon-reveal bag-button" href="' . esc_url( $woocommerce->cart->get_cart_url() ) . '">'.$view_cart_icon.'<span class="text">' . __( 'View cart', 'swiftframework' ) . '</span></a>'; $cart_output .= '<a class="sf-button standard sf-icon-reveal checkout-button" href="' . esc_url( $woocommerce->cart->get_checkout_url() ) . '">'.$checkout_icon.'<span class="text">' . __( 'Proceed to checkout', 'swiftframework' ) . '</span></a>'; $cart_output .= '</div>'; } else { $cart_output .= '<div class="bag-empty">' . __( 'Your cart is empty.', 'swiftframework' ) . '</div>'; } $cart_output .= '</div>'; $cart_output .= '</li>'; $cart_output .= '</ul>'; $cart_output .= '</li>'; } return $cart_output; }
Thanks
MohammadJuly 14, 2016 at 1:28 pm #281261This reply has been marked as private.July 15, 2016 at 6:43 am #281405Hi,
Please paste this code also at functions.php of child theme.function sf_woo_header_add_to_cart_fragment( $fragments ) { global $woocommerce, $sf_options; ob_start(); $show_cart_count = false; if ( isset( $sf_options['show_cart_count'] ) ) { $show_cart_count = $sf_options['show_cart_count']; } if ( sf_theme_opts_name() == "sf_atelier_options" ) { $cart_total = '<span class="menu-item-title">' . __( "Cart" , "swiftframework" ) . '</span>'; } else { $cart_total = WC()->cart->get_cart_total(); } $cart_count = $woocommerce->cart->cart_contents_count; $cart_count_text = sf_product_items_text( $cart_count ); $cart_count_text_alt = sf_product_items_text( $cart_count, true ); $view_cart_icon = apply_filters( 'sf_view_cart_icon', '<i class="ss-view"></i>' ); $checkout_icon = apply_filters( 'sf_checkout_icon', '<i class="ss-creditcard"></i>' ); $go_to_shop_icon = apply_filters( 'sf_go_to_shop_icon', '<i class="ss-cart"></i>' ); $extra_class = ""; if ( $cart_count != "0" ) { $extra_class .= "cart-not-empty "; } ?> <li class="parent shopping-bag-item <?php echo $extra_class; ?>"> <?php $cart_count_text_alt = ($cart_count_text_alt=='0')?'':$cart_count_text_alt; if ( $show_cart_count ) { ?> <a class="cart-contents" href="<?php echo esc_url($woocommerce->cart->get_cart_url()); ?>" title="<?php _e( 'View your shopping cart', 'swiftframework' ); ?>"> <?php echo apply_filters( 'sf_header_cart_icon', '<i class="ss-cart"></i>' ); ?><?php echo $cart_total; ?><span class="num-items cart-count-enabled"><?php echo $cart_count_text_alt; ?></span></a> <?php } else { ?> <a class="cart-contents" href="<?php echo esc_url($woocommerce->cart->get_cart_url()); ?>" title="<?php _e( 'View your shopping cart', 'swiftframework' ); ?>"><?php echo apply_filters( 'sf_header_cart_icon', '<i class="ss-cart"></i>' ); ?><?php echo $cart_total; ?><span class="num-items"><?php echo $cart_count_text_alt; ?></span></a> <?php } ?> <ul class="sub-menu"> <li> <div class="shopping-bag" data-empty-bag-txt="<?php _e( 'Your cart is empty.', 'swiftframework' ); ?>" data-singular-item-txt="<?php _e( 'item in the cart', 'swiftframework' ); ?>" data-multiple-item-txt="<?php _e( 'items in the cart', 'swiftframework' ); ?>"> <div class="loading-overlay"><i class="sf-icon-loader"></i></div> <?php if ( $cart_count != "0" ) { ?> <div class="bag-header"><?php echo $cart_count_text; ?> <?php _e( 'in the cart', 'swiftframework' ); ?></div> <div class="bag-contents"> <?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { ?> <?php $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); $product_title = $_product->get_title(); $price = apply_filters( 'woocommerce_cart_item_price', $woocommerce->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); ?> <?php $variation_id_class = ''; if ( $cart_item['variation_id'] > 0 ) $variation_id_class = ' product-var-id-' . $cart_item['variation_id']; if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) { ?> <div class="bag-product clearfix product-id-<?php echo $cart_item['product_id']; ?><?php echo $variation_id_class; ?>"> <figure><a class="bag-product-img" href="<?php echo get_permalink( $cart_item['product_id'] ); ?>"><?php echo $_product->get_image(); ?></a> </figure> <div class="bag-product-details"> <div class="bag-product-title"> <a href="<?php echo get_permalink( $cart_item['product_id'] ); ?>"> <?php echo apply_filters( 'woocommerce_cart_widget_product_title', $product_title, $_product ); ?></a> </div> <div class="bag-product-price"><?php _e( "Unit Price:", "swiftframework" ); ?> <?php echo $price; ?></div> <div class="bag-product-quantity"><?php _e( 'Quantity:', 'swiftframework' ); ?> <?php echo $cart_item['quantity']; ?></div> </div> <a href="#" class="remove-product remove" data-ajaxurl="<?php echo admin_url( 'admin-ajax.php' ); ?>" data-product-id="<?php echo $cart_item['product_id'];?>" data-variation-id="<?php echo $cart_item['variation_id'];?>" data-product-qty="<?php echo $cart_item['quantity'];?>" title="<?php echo __( 'Remove this item', 'swiftframework' ); ?>">×</a> </div> <?php } ?> <?php } ?> </div> <?php if ( sf_theme_opts_name() == "sf_atelier_options" ) { ?> <div class="bag-total"> <?php if ( class_exists( 'Woocommerce_German_Market' ) ) { ?> <span class="total-title"><?php _e( "Total incl. tax", "swiftframework" ); ?></span> <?php } else { ?> <span class="total-title"><?php _e( "Total", "swiftframework" ); ?></span> <?php } ?> <span class="total-amount"><?php echo WC()->cart->get_cart_total(); ?></span> </div> <?php } ?> <div class="bag-buttons"> <a class="sf-button standard sf-icon-reveal bag-button" href="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>"> <?php echo $view_cart_icon; ?> <span class="text"><?php _e( 'View cart', 'swiftframework' ); ?></span> </a> <a class="sf-button standard sf-icon-reveal checkout-button" href="<?php echo esc_url( $woocommerce->cart->get_checkout_url() ); ?>"> <?php echo $checkout_icon; ?> <span class="text"><?php _e( 'Proceed to checkout', 'swiftframework' ); ?></span> </a> </div> <?php } else { ?> <div class="bag-empty"><?php _e( 'Your cart is empty.', 'swiftframework' ); ?></div> <?php } ?> </div> </li> </ul> </li> <?php $fragments['.shopping-bag-item'] = ob_get_clean(); return $fragments; }
Thanks
MohammadJuly 15, 2016 at 9:01 am #281428No change from posting either in functions.php
It looks like there’s an if/else statement about showing cart count – is this a setting somewhere? As this span is never shown – <span class=”num-items cart-count-enabled”>
July 15, 2016 at 9:57 am #281444This can be done with a this CSS. Add it to Theme Options => Custom CSS.
li.parent.shopping-bag-item:not(.cart-not-empty) { display: none!important; }
Mohammad’s code is correct also, but it likely needs a product added/removed from the cart to trigger the ajax to refresh the cart contents.
July 15, 2016 at 10:55 am #281460Ah I see, it’s not quite what I’m after as it hides the whole cart.
Instead,
if cart quantity is > 0 then show cart count
if cart quantity is 0 then don’t show cart count
I can use li.parent.shopping-bag-item:not(.cart-not-empty) span.num-items {display: none!important;}
However, once items are removed from the cart it still shows count of 0 until the page is refreshed.
July 15, 2016 at 4:03 pm #281610Hi
Unfortunately this is a difficult customisation to make due to the complexity of AJAX, and is therefore beyond our scope of support. You will likely need a custom function to achieve this, we recommend you seek a freelance developer to help you with this
– Kyle
-
Posted in: Atelier
You must be logged in and have valid license to reply to this topic.