Archived topic
WooCommerce ajax quantity input in shop page
7 replies · Started by Daniel on February 22, 2022
Hi!
I am using this filter from here in stack overflow to display an ajax quantity input field on the WooCommerce shop page.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
// Get the necessary classes
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );
// Embedding the quantity field to Ajax add to cart button
$html = sprintf( '%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
woocommerce_quantity_input( array(), $product, false ),
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() )
);
}
return $html;
}
It works really well, it displays an input box for the quantity but it doesn't show the + and - buttons from Generatepress.
Would there be a possibility to display those + and - buttons next to the input?
Thanks in advance!
Hi there,
Unfortunately, we can't assist with tweaking a custom solution from Stackoverflow.
I had a quick look and it doesn't look like the implementation would be very simple either since we are using our own script for the quantity buttons.
Sorry about that!
Thanks Leo!
Yes, that's completely understandable. Maybe I was lucky and the implementation was simple, that's why I asked.
Anyway, if I manage to get it done another way I'll post the solution here in case someone else needs it in the future.
Sounds good thank you for understanding :)
By the way, could you tell me where in GP premium files I can find the script that generates the quantity buttons?
Hi there,
the script is in this file in the plugin:
/woocommerce/functions/js/woocommerce.js
Hello again
As I said before, if I found a solution to display the Generatepess ajax quantity buttons on the shop page I would share it here.
The solution is to use this plugin: https://wordpress.org/plugins/quantity-field-on-shop-page-for-woocommerce/
As of today it gives an error when activating it which is easily solved by editing the plugin code in the file wc-quantity-field-shop-page.php and in line 75 you have to change this:
$args['max_value'] = $product->stock_quantity;
to this:
$args['max_value'] = $product->get_stock_quantity();
This way we get the Generatepress quantity buttons on the shop page with ajax functionality and even with quantity limited to the stock available. Great!
Thanks for sharing!