Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Amourspirit/7601b62932a54aa4f71302c4dc1aa693 to your computer and use it in GitHub Desktop.
Save Amourspirit/7601b62932a54aa4f71302c4dc1aa693 to your computer and use it in GitHub Desktop.
<?php
/**
* Add WooCommerce cart to Main Menu for Divi
*
* Add WooCommerce cart icon to right of main menu
*/
// Author Paul Moss
// Created September 13 2019
// Append cart item (and cart count) to end of main menu.
if ( is_page_template( 'page-template-blank.php' ) ) {
return;
}
add_action( 'wp_head', function () { ?>
<style>
.custom-cart-info ::before {
text-shadow: 0 0;
font-family: ETmodules !important;
font-weight: 400;
font-style: normal;
font-variant: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
line-height: 1;
text-transform: none;
speak: none;
}
.custom-cart-info span::before {
margin-right: 10px;
content: "\e07a";
}
</style>
<?php } );
// Append cart item (and cart count) to end of main menu.
add_filter( 'wp_nav_menu_main-menu_items', 'am_append_cart_icon', 10, 2 );
function am_append_cart_icon( $items, $args ) {
$cart_item_count = WC()->cart->get_cart_contents_count();
$cart_count_span = '';
if ( empty( $cart_item_count )) {
$cart_item_count = 0;
}
$cart_count_span = '<span>'.$cart_item_count.' Items</span>';
$cart_link = '<li class="cart menu-item menu-item-type-post_type menu-item-object-page"><a href="' . get_permalink( wc_get_page_id( 'cart' ) ) . '" class="custom-cart-info">'.$cart_count_span.'</a></li>';
// Add the cart link to the end of the menu.
$items = $items . $cart_link;
return $items;
}
@Amourspirit
Copy link
Author

Adds Cart Icon with number of items to main menu for Divi main menu

Can be added to functions.php or as a custom php snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment