Archived topic
Woocommerce customization
22 replies · Started by Pedro on February 6, 2019
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?
Hello?
Can u help me please?
Try 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).
Hello 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!
I just edited the code above, and included some CSS. Can you try now?
It worked perfectly, thank you very much!
You're very welcome :)