Get Shopify Item Count on WordPress

This example presumes you have a Shopify shop already set up. Using AJAX, it fetching the cart data object (including ‘item_count‘) to allow customization of styles and placement on a WordPress site.

Currently only works on Firefox due to Chromium-based browsers having different security settings.

$.ajax( {
	type: 'GET',
	url: 'https://SHOP-URL-HERE/cart.json',
	dataType: 'jsonp',
	success: function(data) { 
		var item_count = data['item_count'];
	
		//If there are items in cart.
		if( item_count > 0 ) {
			$( '.cart' ).append( '<span class="cart_count" style="background-color: #000; display: inline-block; font-size: 13px; font-weight: 600; color: #FFF; border-radius: 50%; width: 20px; height: 20px; text-align: center; position: absolute; top: -10px; right: -50%;">'+ item_count +'</span>' );
		}
	}
} );

Leave a Reply

Your email address will not be published.