Put Custom cart icon with the cart count to your theme (on Header)
These are 3 steps to add a WooCommerce cart icon with the cart count to your theme. The number of items will be updated automatically with AJAX as items are added to cart.
To avoid errors, this will first check if WooCommerce is active. That way, it doesn’t hurt to add this to any theme regardless of whether WooCommerce plugin is active or not.
- Add the HTML to your theme’s template file where you want the cart
icon to appear. The cart icon will only appear if WooCommerce is active.
You can either add the above directly into your theme template, or you can add it with one of your theme’s template hooks. For example, if your theme’s header has a hook like,
123456789101112<?phpif( in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins') ) ) ) {$count= WC()->cart->cart_contents_count;?><aclass="cart-contents"href="<?php echo WC()->cart->get_cart_url(); ?>"title="<?php _e( 'View your shopping cart' ); ?>"><?phpif($count> 0 ) {?><spanclass="cart-contents-count"><?phpechoesc_html($count); ?></span><?php}?></a><?php } ?>do_action( 'your_theme_header_top' );, then you can add the above code to your functions file like this:
12345678910111213141516171819/*** Add Cart icon and count to header if WC is active*/functionmy_wc_cart_count() {if( in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins') ) ) ) {$count= WC()->cart->cart_contents_count;?><aclass="cart-contents"href="<?php echo WC()->cart->get_cart_url(); ?>"title="<?php _e( 'View your shopping cart' ); ?>"><?phpif($count> 0 ) {?><spanclass="cart-contents-count"><?phpechoesc_html($count); ?></span><?php}?></a><?php}}add_action('your_theme_header_top','my_wc_cart_count'); - Add the following to your functions. This ensures the cart will be updated immediately as items are added to the cart.
1234567891011121314151617181920/*** Ensure cart contents update when products are added to the cart via AJAX*/functionmy_header_add_to_cart_fragment($fragments) {ob_start();$count= WC()->cart->cart_contents_count;?><aclass="cart-contents"href="<?php echo WC()->cart->get_cart_url(); ?>"title="<?php _e( 'View your shopping cart' ); ?>"><?phpif($count> 0 ) {?><spanclass="cart-contents-count"><?phpechoesc_html($count); ?></span><?php}?></a><?php$fragments['a.cart-contents'] = ob_get_clean();return$fragments;}add_filter('woocommerce_add_to_cart_fragments','my_header_add_to_cart_fragment'); - Add this CSS to use WooCommerce’s native shopping cart icon. Adjust
as needed to fit nicely into your theme. You can also use your own icon
from a custom icon font. To use a different icon, change ‘WooCommerce’
to the name of your icon font, and change the
e01don line 3 to your icon’s Unicode. For example, if you already use Font Awesome, change line 2 tofont-family:FontAwesome;, and line 3 tocontent: "\f07a";to use Font Awesome’s cart icon. Also, you may want to edit line 15 and 16 below to change the color of the cart count and background color of the bubble.
1234567891011121314151617181920212223.cart-contents:before {font-family:WooCommerce;content:"\e01d";font-size:28px;margin-top:10px;font-style:normal;font-weight:400;padding-right:5px;vertical-align:bottom;}.cart-contents:hover {text-decoration:none;}.cart-contents-count {color:#fff;background-color:#2ecc71;font-weight:bold;border-radius:10px;padding:1px6px;line-height:1;font-family:Arial,Helvetica,sans-serif;vertical-align:top;}
https://isabelcastillo.com/woocommerce-cart-icon-count-theme-header#comment-46509


Comments
Post a Comment