We’ve already covered a similar customization on Business Bloomer: how to display Ajax quantity selectors on the WooCommerce Shop page. The problem there was that that only works for simple products, and only when Ajax add to cart is activated via the WooCommerce settings.
Today I’d like to experiment a little, and see what happens when we include the “add to cart template” (i.e. the one you see on the WooCommerce Single Product page) under each product on the WooCommerce Shop page.
This *should* work with all product types – especially variable products – because you will see the attribute dropdown selectors in such a case, as if you were on the single product page.
I haven’t tested this with grouped, bundle, and special product types, so feel free to share your findings in the comments below. Enjoy!
PHP Snippet: Display Add to Cart Form For Each Product @ WooCommerce Shop Page
/**
* @snippet Add Cart Form @ WooCommerce Shop
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community https://businessbloomer.com/club/
*/
add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_display_whole_add_cart_form', 1 );
function bbloomer_display_whole_add_cart_form() {
global $product;
if ( ! $product->is_purchasable() ) return;
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_single_add_to_cart', 11 );
}
Hi there!
Thanks for the code! It works fine!
After I added it, if my cart is empty, I’m adding a product with variation, it doesn’t show me some info and I have a message that the “mail adresse seems to be wrong”. That not the case if I have some items in the basket.
Do you have an idea how to fix that?
Thanks
Fab, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
i got it! it’s because i have a Gift card on the same page, and it seems to be a priority on this product.
is there a filter we can add on your snippet to exclude some Categories?
I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂
Hi Sir, the above code works like a charm, but I want to change the variation image when the user selects the variation attributes. Would you help me with that?
Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!