Anyone here ever had the need to add the AJAX add to cart button (the one located on product archives) on a custom query post output?
Do you mind sharing the hook? I can’t seem to find the correct one.
Anyone here ever had the need to add the AJAX add to cart button (the one located on product archives) on a custom query post output?
Do you mind sharing the hook? I can’t seem to find the correct one.
You must be logged in to post a comment.
Digging through the code, it’s pretty straight-forward to find.
First, look at the template used for product archives –
/templates/archive-product.php
. Among other things, it sets up the regular queries and begins building the markup of the page.But when it actually starts looping through each product, it delegates the work to
/templates/loop-shop.php
. This template is loaded for every product, and builds a listing using the following code:The part we care about is the last action:
woocommerce_after_shop_loop_item
. It’s wired in/woocommerce_template_actions.php
to the functionwoocommerce_template_loop_add_to_cart()
.Digging even deeper, and it looks like this function is the one you need.
How to Add It
The easiest way to use this function is to call it directly. Just know that it requires to parameters,
$post
and$_product
. The following code would call it just fine:The function itself calls its own
echo
s, so just call it directly.