Add +(Plus) & -(Minus) Buttons To Quantity Inputs

To add plus (+) and minus (-) buttons to the quantity input field in WooCommerce using the `woocommerce_before_add_to_cart_quantity` and `woocommerce_after_add_to_cart_quantity` hooks, you can use the following code:

1. Open the theme’s functions.php file in your WordPress theme directory.
2. Add the following code to the file:

 

// Add plus and minus buttons to quantity input field
function add_plus_minus_buttons_to_quantity() {
    echo '<div class="quantity-buttons">';
    echo '<span class="minus">-</span>';
    echo '<input type="number" step="1" min="1" max="" name="quantity" value="1" class="input-text qty text" size="4" />';
    echo '<span class="plus">+</span>';
    echo '</div>';
}
add_action('woocommerce_before_add_to_cart_quantity', 'add_plus_minus_buttons_to_quantity', 10);

// Enqueue custom JavaScript
function enqueue_custom_scripts() {
    wp_enqueue_script('custom-scripts', get_stylesheet_directory_uri() . '/js/custom-scripts.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_custom_scripts');

 

3. Create a new directory in your theme called “js” (if it doesn’t exist already) and create a new file called “custom-scripts.js” inside that directory.
4. Open the “custom-scripts.js” file and add the following code:

jQuery(document).ready(function($) {
    // Handle quantity increment and decrement
    $(document).on('click', '.plus, .minus', function() {
        var $quantityInput = $(this).siblings('input[type="number"]');
        var currentValue = parseFloat($quantityInput.val());
        var min = parseFloat($quantityInput.attr('min'));
        var max = parseFloat($quantityInput.attr('max'));
        var step = parseFloat($quantityInput.attr('step'));
        
        if ($(this).hasClass('plus')) {
            if (currentValue < max) {
                $quantityInput.val(currentValue + step);
            }
        } else {
            if (currentValue > min) {
                $quantityInput.val(currentValue - step);
            }
        }
        
        $quantityInput.change();
    });
});

5. Save the changes and upload the modified functions.php and custom-scripts.js files back to your server.

This code adds the plus and minus buttons to the quantity input field in WooCommerce by hooking into the `woocommerce_before_add_to_cart_quantity` and `woocommerce_after_add_to_cart_quantity` actions. The plus and minus buttons are wrapped in a `<div>` with the class “quantity-buttons”. The custom JavaScript handles the quantity increment and decrement functionality when the buttons are clicked.

After making these changes, the plus and minus buttons will be displayed alongside the quantity input field in the WooCommerce product pages, allowing users to easily increase or decrease the quantity.

Please note that you may need to adjust the file paths accordingly, depending on your theme structure.

Related Blogs

Adding Sidebar In WordPress Theme

Adding a sidebar to a WordPress theme involves modifying the theme files and registering a new sidebar area using WordPress functions. Below are the general steps to add a sidebar in WordPress themes:

Ajax Add to Cart Quantity on Shop WooCommerce

To enable AJAX add to cart with quantity selectors on the WooCommerce shop page, you’ll need to use JavaScript to handle the AJAX request and update the cart quantity dynamically. Below are the steps to achieve this:

How to add ACF options pages and options sub page

To add Advanced Custom Fields (ACF) options pages and options sub-pages, you’ll need to have ACF Pro installed and activated on your WordPress site. ACF Pro allows you to create custom options pages where you can add settings, fields, and other configuration data.

Ajax login WordPress

Sometime we need to login in site without reloading the page.so we simply use ajax for it. first we add

Request A Quote

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.