Skip to content

Instantly share code, notes, and snippets.

@seoindex
Last active January 10, 2017 09:52
Show Gist options
  • Save seoindex/25d2c0c8a81bec9126bfdcb71b518f8c to your computer and use it in GitHub Desktop.
Save seoindex/25d2c0c8a81bec9126bfdcb71b518f8c to your computer and use it in GitHub Desktop.
Aggiungere carrello WooCommerce nel menu di wordpress
/**
* Aggiungere l'icona del carrello, numero degli elementi ed il costo totale nel menu
*/
add_filter('wp_nav_menu_items','sk_wcmenucart', 10, 2);
function sk_wcmenucart($menu, $args) {
if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location )
return $menu;
ob_start();
global $woocommerce;
$viewing_cart = __('View your shopping cart', 'your-theme-slug');
$start_shopping = __('Start shopping', 'your-theme-slug');
$cart_url = $woocommerce->cart->get_cart_url();
$shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
$cart_contents_count = $woocommerce->cart->cart_contents_count;
$cart_contents = sprintf(_n('%d item', '%d', $cart_contents_count, 'your-theme-slug'), $cart_contents_count);
$cart_total = $woocommerce->cart->get_cart_total();
// Togli commento alle righe sottostanti per nascondere il carrello quando non ci sono elementi
// if ( $cart_contents_count > 0 ) {
if ($cart_contents_count == 0) {
$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
} else {
$menu_item = '<li class="right"><a class="wcmenucart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
}
$menu_item .= '<i class="fa fa-shopping-cart"></i> ';
$menu_item .= $cart_contents.' - '. $cart_total;
$menu_item .= '</a></li>';
// }
echo $menu_item;
$social = ob_get_clean();
return $menu . $social;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment