Home > Backend Development > PHP Tutorial > How to Dynamically Update WooCommerce Header Cart Items Count Using AJAX?

How to Dynamically Update WooCommerce Header Cart Items Count Using AJAX?

Linda Hamilton
Release: 2024-10-29 06:19:02
Original
1022 people have browsed it

How to Dynamically Update WooCommerce Header Cart Items Count Using AJAX?

Ajaxify Header Cart Items Count in WooCommerce

Identifying the Need

In WooCommerce integrated WordPress themes, updating the header cart items count without reloading the page is a common challenge. jQuery offers a solution, but questions arise when items can be added in multiple quantities.

Utilizing AJAX to Retrieve Total Count

To dynamically retrieve the total cart count from PHP sessions using jQuery, a reloadCart.php file is created to echo the value:

<code class="php"><?php
require('../../../wp-blog-header.php');
global $woocommerce;
echo $woocommerce->cart->get_cart_contents_count();
?></code>
Copy after login

AJAX Implementation

However, attempts to make AJAX calls to this file using jQuery's get(), post(), or ajax() functions have been unsuccessful.

Improved Approach

Instead of relying on a reload, WooCommerce offers a dedicated woocommerce_add_to_cart_fragments action hook that supports Ajax.

HTML Integration

Embed the cart count in the header with a unique ID or class:

<code class="php">$items_count = WC()->cart->get_cart_contents_count(); 
?>
    <div id="mini-cart-count"><?php echo $items_count ? $items_count : '&amp;nbsp;'; ?></div>
<?php
Copy after login

PHP Code

Implement the following code in the theme's function.php file or a plugin:

<code class="php">add_filter( 'woocommerce_add_to_cart_fragments', 'wc_refresh_mini_cart_count');
function wc_refresh_mini_cart_count($fragments){
    ob_start();
    $items_count = WC()->cart->get_cart_contents_count();
    ?>
    <div id="mini-cart-count"><?php echo $items_count ? $items_count : '&amp;nbsp;'; ?></div>
    <?php
        $fragments['#mini-cart-count'] = ob_get_clean();
    return $fragments;
}</code>
Copy after login

Replace #mini-cart-count with .mini-cart-count if using a class.

jQuery Refresh

If additional jQuery refresh is required:

<code class="php">$(document.body).trigger('wc_fragment_refresh');</code>
Copy after login

or

<code class="php">$(document.body).trigger('wc_fragments_refreshed');</code>
Copy after login

The above is the detailed content of How to Dynamically Update WooCommerce Header Cart Items Count Using AJAX?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template