Last active
June 19, 2023 03:10
-
-
Save mikejolley/2793710 to your computer and use it in GitHub Desktop.
WooCommerce - Show quantity inputs for simple products within loops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Code should be placed in your theme functions.php file. | |
*/ | |
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 ); | |
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) { | |
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) { | |
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">'; | |
$html .= woocommerce_quantity_input( array(), $product, false ); | |
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>'; | |
$html .= '</form>'; | |
} | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be great if someone could assist me in disabling the redirection to the product page after clicking the quantity field buttons (+ or -) next to add to cart button on the shop page . The shop page reloads and redirects to the product page if I do not click the add to cart button quick enough right after entering a number or increasing/decreasing the quantity. I want to stop this behavior.
Expected behaviour: User should stay on the shop page. No reload, no redirect to the product page but actual cart contents need to be updated upon clicking + or - buttons.
Note: I enabled Redirect to the basket page after successful addition in wooCommerce > Settings > Products > Add to basket behaviour but this doesn’t do anything.
Here is the code:
Appreciate any suggestions.
Thanks in advance