Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // my own custom code
- add_filter('add_to_cart_custom_fragments', 'woocommerce_header_add_to_cart_custom_fragment');
- function woocommerce_header_add_to_cart_custom_fragment( $cart_fragments ) {
- global $woocommerce;
- ob_start();
- ?>
- <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php _e('View cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
- <?php
- $cart_fragments['a.cart-contents'] = ob_get_clean();
- return $cart_fragments;
- }
- //Add WooCommerce Cart Icon to Menu with Cart Item Count
- // https://wpbeaches.com/add-woocommerce-cart-icon-to-menu-with-cart-item-count/
- add_shortcode ('woo_cart_but', 'woo_cart_but' );
- /**
- * Create Shortcode for WooCommerce Cart Menu Item
- */
- function woo_cart_but() {
- ob_start();
- $cart_count = WC()->cart->cart_contents_count; // Set variable for cart item count
- $cart_url = wc_get_cart_url(); // Set Cart URL
- ?>
- <li><a class="menu-item cart-contents" href="<?php echo $cart_url; ?>" title="My Basket">
- <?php
- if ( $cart_count > 0 ) {
- ?>
- <span class="cart-contents-count"><?php echo $cart_count; ?></span>
- <?php
- }
- ?>
- </a></li>
- <?php
- return ob_get_clean();
- }
- add_filter( 'woocommerce_add_to_cart_fragments', 'woo_cart_but_count' );
- /**
- * Add AJAX Shortcode when cart contents update
- */
- function woo_cart_but_count( $fragments ) {
- ob_start();
- $cart_count = WC()->cart->cart_contents_count;
- $cart_url = wc_get_cart_url();
- ?>
- <a class="cart-contents menu-item" href="<?php echo $cart_url; ?>" title="<?php _e( 'View your shopping cart' ); ?>">
- <?php
- if ( $cart_count > 0 ) {
- ?>
- <span class="cart-contents-count"><?php echo $cart_count; ?></span>
- <?php
- }
- ?></a>
- <?php
- $fragments['a.cart-contents'] = ob_get_clean();
- return $fragments;
- }
- add_filter( 'main-nav', 'woo_cart_but_icon', 10, 2 ); // Change menu to suit - example uses 'top-menu'
- /**
- * Add WooCommerce Cart Menu Item Shortcode to particular menu
- */
- function woo_cart_but_icon ( $items, $args ) {
- $items .= '[woo_cart_but]'; // Adding the created Icon via the shortcode already created
- return $items;
- }
Advertisement
Add Comment
Please, Sign In to add comment