Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Last active December 14, 2020 20:04
Show Gist options
  • Save KustomDeveloper/8e13e48f7b2241515fc55bab82a10a64 to your computer and use it in GitHub Desktop.
Save KustomDeveloper/8e13e48f7b2241515fc55bab82a10a64 to your computer and use it in GitHub Desktop.
//Add to functions.php
<?php
function add_number_of_cart_items() {
global $woocommerce;
$cart_count = $woocommerce->cart->cart_contents_count;
?>
<script>
var cartCount = <?php echo $cart_count; ?>;
var node = document.createElement("span");
node.classList.add('cart-count');
var textnode = document.createTextNode("<?php echo $cart_count; ?>");
node.appendChild(textnode);
const menu = document.querySelector("#primary-menu li:nth-last-of-type(1)").appendChild(node);
</script>
<style>.cart-count{width:20px;height:20px;text-align: center;position:absolute; background: red; border-radius: 50%; color:#fff; top:0; right:-15px;}</style>
<?php
}
add_action( 'wp_footer', 'add_number_of_cart_items' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment