Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active July 9, 2021 09:53
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save woogists/c5e5d73bb4d7d44f5642313b8094937c to your computer and use it in GitHub Desktop.
Save woogists/c5e5d73bb4d7d44f5642313b8094937c to your computer and use it in GitHub Desktop.
[Frontend Snippets] Override loop template and show quantities next to add to cart buttons.
/**
* Override loop template and show quantities next to add to cart buttons
*/
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;
}
@Spreeuw
Copy link

Spreeuw commented Jun 15, 2021

@lgdelai the add_to_cart shortcode is rendered by WooCommerce with this function:
includes/class-wc-shortcodes.php#L305-L368 and the woocommerce_loop_add_to_cart_link hook is called in the loop/add-to-cart.php template called by that very function.
This means that if this doesn't work in the list generated by the ConventViews plugin, they are loading the shortcode before your filter is applied. You could try putting the filter in an mu-plugin to see if that resolves the issue.

@lgdelai
Copy link

lgdelai commented Jun 15, 2021

@Spreeuw thanks for your explanations.

Could you explain better what the mu-plugin you mentioned is about?

I didn't quite understand what I need to try to do.

@Spreeuw
Copy link

Spreeuw commented Jun 16, 2021

it's an alternative method of adding custom code to your sites (compared to the 'traditional' method of using functions.php in your theme): mu-plugins load before regular plugins. This was the first google result:
https://wordpress.org/support/article/must-use-plugins/
but there are many other resources and tutorials.

Alternatively you could try the Code Snippets plugin, which also loads filters before functions.php and most plugins.

It's still possible the root cause lies somewhere else (for example, when the woocommerce_loop_add_to_cart_link is missing from the loop/add-to-cart.php template of your theme), or something else sepecific to ContentViews, but the developers of that plugin should be able to help with this. I'm not with WooCommerce and not the author of the original gist, just provided an alternative solution.

@lgdelai
Copy link

lgdelai commented Jun 16, 2021

@Spreeuw
I find a plugin who works fine in all situations and is ajax compatible.

https://br.wordpress.org/plugins/quantity-field-on-shop-page-for-woocommerce/

He is capable of show quantity field a side of add to cart button.

Thanks all for help.

@scienceandnature
Copy link

I am using
/** * Override loop template and show quantities next to add to cart buttons */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;}

The number picker works fine but it overrides the ajax add to cart to throw to the top of the page instead of adding: View cart
Can anyone suggest a change to code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment