Snippet

Automatically Update WooCommerce Cart on Quantity Change

How to auto update cart when quantity changes in woocommerceWoocommerce automatically update cart on quantity changePlugin to update cart automatically when quantity changesAutomatic cart update on quantity change woocommerceUpdate woocommerce cart automatically when changing quantityAuto update cart quantity woocommerceWoocommerce cart update without button clickEnable automatic cart update on quantity changeWoocommerce ajax update cart on quantity changeHow to make woocommerce cart update automatically

Explanation

Here's how you can make your WooCommerce cart update automatically when you change the quantity of a product, without needing to click an update button.

What This Does:

  • When you're on the cart page and change the quantity of a product, the cart will automatically update.
  • This is done using a bit of JavaScript magic (AJAX) that talks to your server behind the scenes.

How It Works:

  • A custom script is loaded on the cart page. This script listens for any changes in the quantity input fields.
  • When a change is detected, it sends the new quantity to the server using AJAX.
  • The server updates the cart with the new quantity and recalculates the totals.
  • Finally, the updated cart details are sent back to the page, refreshing the cart display without needing a full page reload.

Key Parts:

  • PHP Code: This part sets up the server-side handling. It listens for AJAX requests and updates the cart accordingly.
  • JavaScript Code: This part runs in your browser. It detects changes in the quantity fields and sends the necessary data to the server.

With this setup, your customers will enjoy a smoother shopping experience, as they won't need to manually update the cart every time they adjust quantities.

Code

1<?php 2/** 3 * Automatically update WooCommerce cart when quantity changes. 4 * 5 * This snippet uses AJAX to update the cart totals without requiring a button click. 6 * It hooks into the WooCommerce scripts and adds custom JavaScript to handle the AJAX request. 7 */ 8 9// Enqueue custom script to handle AJAX cart update 10function wp_dudecom_enqueue_ajax_cart_update_script() { 11 if (is_cart()) { 12 wp_enqueue_script('wp-dudecom-ajax-cart-update', get_template_directory_uri() . '/js/wp-dudecom-ajax-cart-update.js', array('jquery'), null, true); 13 wp_localize_script('wp-dudecom-ajax-cart-update', 'wp_dudecom_ajax_cart', array( 14 'ajax_url' => admin_url('admin-ajax.php'), 15 )); 16 } 17} 18add_action('wp_enqueue_scripts', 'wp_dudecom_enqueue_ajax_cart_update_script'); 19 20// Handle AJAX request to update cart 21function wp_dudecom_ajax_update_cart() { 22 WC()->cart->set_quantity($_POST['cart_item_key'], $_POST['quantity']); 23 WC()->cart->calculate_totals(); 24 WC()->cart->maybe_set_cart_cookies(); 25 26 // Send updated cart fragments back to the front-end 27 WC_AJAX::get_refreshed_fragments(); 28 wp_die(); 29} 30add_action('wp_ajax_wp_dudecom_update_cart', 'wp_dudecom_ajax_update_cart'); 31add_action('wp_ajax_nopriv_wp_dudecom_update_cart', 'wp_dudecom_ajax_update_cart'); 32 33?> 34 35// JavaScript file: wp-dudecom-ajax-cart-update.js 36jQuery(function($) { 37 $('body').on('change', '.woocommerce-cart-form input.qty', function() { 38 var $this = $(this); 39 var cart_item_key = $this.closest('tr').attr('data-cart_item_key'); 40 var quantity = $this.val(); 41 42 $.ajax({ 43 type: 'POST', 44 url: wp_dudecom_ajax_cart.ajax_url, 45 data: { 46 action: 'wp_dudecom_update_cart', 47 cart_item_key: cart_item_key, 48 quantity: quantity 49 }, 50 success: function(response) { 51 // Update cart fragments 52 if (response && response.fragments) { 53 $.each(response.fragments, function(key, value) { 54 $(key).replaceWith(value); 55 }); 56 } 57 } 58 }); 59 }); 60}); 61

Instructions

File Location: Add the PHP code to your theme's functions.php file or a custom plugin file. The JavaScript code should be placed in a new file named wp-dudecom-ajax-cart-update.js within a js directory in your theme.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.

Implementation Steps:

  1. Add PHP Code: Open your theme's functions.php file or a custom plugin file and paste the provided PHP code. This code enqueues the JavaScript file and handles the AJAX request to update the cart.
  2. Create JavaScript File: In your theme directory, create a folder named js if it doesn't exist. Inside this folder, create a new file named wp-dudecom-ajax-cart-update.js and paste the provided JavaScript code into this file.
  3. Verify File Paths: Ensure the path in the wp_enqueue_script function matches the location of your JavaScript file. It should be get_template_directory_uri() . '/js/wp-dudecom-ajax-cart-update.js'.
  4. Test the Functionality: Navigate to your WooCommerce cart page and change the quantity of a product. The cart should automatically update without needing to click an update button.

For further assistance or to explore more advanced functionalities, consider reaching out to wp-dude.com for professional WordPress support.