Code for adding quantity select to shop / shop archive pages - needs AJAX

Hey folks.

The below code works well for adding a quantity select box to product archives and shop pages in Bricks. The only issue is, after clicking ‘add to basket’ the page gets refreshed and a visitor is sent back to the top of the page.

Does anyone know how to convert this into using AJAX ?

Any help will be massively appreciated. Here’s the code:

<?php /** * How to show quantities next to the add to cart buttons */ add_filter( 'woocommerce_loop_add_to_cart_link', 'demo_add_quanitity_to_woo_archive', 10, 2 ); function demo_add_quanitity_to_woo_archive( $html, $product ) { if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) { $html = ''; $html .= woocommerce_quantity_input( array(), $product, false ); $html .= '' . esc_html( $product->add_to_cart_text() ) . ''; $html .= ''; } return $html; } ?>

Thanks @lanxalot . This got me on the right path but in the end I used the below code which worked better for me. I just had to add some extra CSS to style the button.

add_filter('woocommerce_loop_add_to_cart_link', 'add_quanitity_to_shop_archive', 10, 2);
function add_quanitity_to_shop_archive($html, $product) {
    if ($product && $product->is_type('simple') && $product->is_purchasable() && $product->is_in_stock() && !$product->is_sold_individually()) {
        $html = wc_get_template_html('single-product/add-to-cart/simple.php');
    }
    return $html;
}
1 Like

Awesome, thanks James, I will give this a try :+1: