{"id":871,"date":"2017-12-12T19:04:01","date_gmt":"2017-12-12T11:04:01","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=871"},"modified":"2018-09-05T10:56:27","modified_gmt":"2018-09-05T02:56:27","slug":"how-to-reflect-cart-count-at-the-cart-icon-as-and-when-products-are-added-or-removed-from-the-cart-in-woocommerce","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-reflect-cart-count-at-the-cart-icon-as-and-when-products-are-added-or-removed-from-the-cart-in-woocommerce\/","title":{"rendered":"How to reflect cart count at the cart icon as and when products are added or removed from the cart in WooCommerce"},"content":{"rendered":"<p>Although there is a hassle free option of using <a href=\"https:\/\/woocommerce.com\/?aff=1878\" rel=\"noopener\" target=\"_blank\">WooCommerce.com<\/a> as your e-commerce store, you may prefer to host your own.  <\/p>\n<p>If you are familiar with <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> and wish to host your e-commerce website, <a href=\"https:\/\/wordpress.org\/plugins\/woocommerce\/\" rel=\"noopener\" target=\"_blank\">WooCommerce is also available as a WordPress plugin<\/a> that augments WordPress with e-commerce website features. <\/p>\n<p>Although more work needs to be done for self-hosted WordPress sites, we have the option of creating our own themes without having to pay additional charges on top of the web hosting fees. <\/p>\n<p>When it comes to creating our own WooCommerce themes, one feature that we may want to implement would be to show the number of products that had been added to our WooCommerce cart near the shopping cart icon as and when products are added or removed from the cart.<\/p>\n<p>This post documents how we can reflect cart count at the cart icon as and when products are added or removed from the WooCommerce shopping cart.<\/p>\n<h2>Hooking into WooCommerce to update the cart count HTML portion when products are added to or removed from the cart<\/h2>\n<p>The first step is to hook into WooCommerce to update cart count when products are added to or removed from the cart.<\/p>\n<p>WooCommerce included a filter that enables theme developers to customize the HTML output for the cart count whenever there is any changes made to the shopping cart. <\/p>\n<p>Hooking into the <code>woocommerce_add_to_cart_fragments<\/code> filter allows us to change the HTML portion that reflects the cart count each time a product is added to or removed from the cart. With that, we could include the following code fragments in <code>functions.php<\/code> to reflect changes to the cart count:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\nfunction woocommerce_header_add_to_cart_fragment( $fragments = array()) {\r\n\tob_start();\r\n\t$cart_count = WC()-&gt;cart-&gt;get_cart_contents_count();\r\n?&gt;\r\n\r\n\t&lt;div id=&quot;cart-count&quot;&gt;&lt;?php if ($cart_count &gt; 0) { ?&gt;&lt;span&gt;&lt;?php echo $cart_count;?&gt;&lt;\/span&gt;&lt;?php } ?&gt;&lt;\/div&gt;\r\n\r\n&lt;?php\r\n\r\n\t$fragments&#x5B;'#cart-count'] = ob_get_clean();\r\n\r\n\treturn $fragments;\r\n\r\n} \/\/ end function woocommerce_add_to_cart_fragments\r\nadd_filter('woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');\r\n<\/pre>\n<p>In the above code fragment, we first define a function, <code>woocommerce_header_add_to_cart_fragment<\/code>, that expects an array as an input parameter. When nothing is supplied to the function, a new array will be implicitly created.<\/p>\n<p>Inside the function body, we use the <a href=\"http:\/\/php.net\/manual\/en\/function.ob-start.php\" rel=\"noopener\" target=\"_blank\">ob_start()<\/a> and <a href=\"http:\/\/php.net\/manual\/en\/function.ob-get-clean.php\" rel=\"noopener\" target=\"_blank\">ob_get_clean()<\/a> functions to capture some PHP code execution that will produce the HTML portion that reflects the cart count.<\/p>\n<p>In between the <a href=\"http:\/\/php.net\/manual\/en\/function.ob-start.php\" rel=\"noopener\" target=\"_blank\">ob_start()<\/a> and <a href=\"http:\/\/php.net\/manual\/en\/function.ob-get-clean.php\" rel=\"noopener\" target=\"_blank\">ob_get_clean()<\/a> function calls, we then make a call to <code>WC()->cart->get_cart_contents_count()<\/code> to get the number of products that are in our WooCommerce shopping cart and assigned it to the <code>$cart_count<\/code> variable. We then use <code>$cart_count<\/code> to generate the HTML code to reflect the cart count.<\/p>\n<p>We then create an array entry, with:<\/p>\n<ul>\n<li>the jQuery selector for seeking the HTML element to replace as the key and<\/li>\n<li>the HTML code that reflects the cart count as the value. <\/li>\n<\/ul>\n<p>Finally, we return the <code>$fragment<\/code> array to the function caller.<\/p>\n<p><strong>Note that the HTML portion includes the HTML element, with id=\u201ccart-count\u201d. This HTML element will be replaced by WooCommerce whenever there are changes to the shopping cart.<\/strong><\/p>\n<h2>Calling the function earlier in header<\/h2>\n<p>Since the function that we had added to the filter already did the work of generating the HTML portion for the cart count, we simply make a call to that function to display cart count at our shopping cart link:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;a href=&quot;\/cart&quot; title=&quot;See what is in your shopping cart&quot;&gt;\r\n\t&lt;?php echo woocommerce_header_add_to_cart_fragment()&#x5B;'#cart-count'];?&gt;\r\n&lt;\/a&gt;\r\n<\/pre>\n<p>With that, whenever products are added to or removed from the WooCommerce shopping cart, the cart count will be updated and reflected at the cart icon.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-e3\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-e3&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-e3&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F871&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Although there is a hassle free option of using <a href=\"https:\/\/woocommerce.com\/?aff=1878\" rel=\"noopener\" target=\"_blank\">WooCommerce.com<\/a> as your e-commerce store, you may prefer to host your own.  <\/p>\n<p>If you are familiar with <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> and wish to host your e-commerce website, <a href=\"https:\/\/wordpress.org\/plugins\/woocommerce\/\" rel=\"noopener\" target=\"_blank\">WooCommerce is also available as a WordPress plugin<\/a> that augments WordPress with e-commerce website features. <\/p>\n<p>Although more work needs to be done for self-hosted WordPress sites, we have the option of creating our own themes without having to pay additional charges on top of the web hosting fees. <\/p>\n<p>When it comes to creating our own WooCommerce themes, one feature that we may want to implement would be to show the number of products that had been added to our WooCommerce cart near the shopping cart icon as and when products are added or removed from the cart.<\/p>\n<p>This post documents how we can reflect cart count at the cart icon as and when products are added or removed from the WooCommerce shopping cart.<\/p>\n","protected":false},"author":1,"featured_media":1276,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[457,459,16,461,460,13,456,458,5],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/WooCommerce-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-e3","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/871"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=871"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/871\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1276"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}