Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyperking/9513180 to your computer and use it in GitHub Desktop.
Save hyperking/9513180 to your computer and use it in GitHub Desktop.
MiniCart JS Cart Widget and Item Counter
<html>
<head>
<meta charset="UTF-8">
<title>MiniCart JS</title>
</head>
<body>
<div id="cart">
<button class="showCart">Show Cart</button>
<div class="itemCount"></div>
<!-- paypal minicart will be added here after dom load -->
</div>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript" src="/assets/js/minicart.min.js"></script>
<script>
jQuery('document').ready(function($) {
// render minicart
paypal.minicart.render();
// initialize item counter
cartCount();
// initialize cart toggle button and positioning
initCart();
function cartCount(){
var count = paypal.minicart.cart.items().length;
var total = paypal.minicart.cart.total()
$(".itemCount").text(function(){
if (count < 1){
return "Cart Empty";
}else{
if(count == 1){
return count + "Item in Cart";
}else{
return count +"Items in Cart";
}
}
});
paypal.minicart.cart.on('add',cartCount);
paypal.minicart.cart.on('remove',cartCount);
paypal.minicart.cart.on('checkout', function(){
$(".itemCount").text("..Processing Order..");
});
}
function showCart(){
// toggle cart function
paypal.minicart.view.toggle();
}
function initCart(){
//render cart where we want it. #cart can be whatever you wish
$("#PPMiniCart").appendTo("#cart");
//initialize the toggle button
$('.showCart').on("click",showCart);
}
});// end doc ready
</script>
</body>
</html>
@patrickyue
Copy link

Hi~ thanks for this code. but there is a critical situation~ this code will have something affects cart.model.destroy and then got stuck...please give an help or some idea on this,thanks

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