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.

  • Have you considered asking this in the WooCommerce support forums?
    – EAMann
    Jun 19 '12 at 21:16
  • Yes, but my experience is that StackExchange is always faster. Jun 19 '12 at 21:17

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:

<?php do_action('woocommerce_before_shop_loop_item'); ?>
<a href="<?php the_permalink(); ?>">
    <?php do_action('woocommerce_before_shop_loop_item_title', $post, $_product); ?>
    <h3><?php the_title(); ?></h3>
    <?php do_action('woocommerce_after_shop_loop_item_title', $post, $_product); ?>

<?php do_action('woocommerce_after_shop_loop_item', $post, $_product); ?>

The part we care about is the last action: woocommerce_after_shop_loop_item. It's wired in /woocommerce_template_actions.php to the function woocommerce_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:

global $post; // Assuming it's already set up
$_product = &new woocommerce_product( $post->ID );

woocommerce_template_loop_add_to_cart( $post, $_product );

The function itself calls its own echos, so just call it directly.

  • Oh, awesome! I'll give it a try. Jun 20 '12 at 18:54
  • 1
    This outputs exactly what I need, except it looks like the button must be linked to some JS, because now, when I click it, it loads the product without adding it to the cart even though the link looks like this: {url}/?add-to-cart=9686 Jun 20 '12 at 19:06
  • That just means that the WooCommerce script files aren't being enqueued. Take a look at /woocommerce.php` and you'll see how woocommerce_frontend_scripts() is being tied to the template_redirect hook. You'll either need to hook this into your own code or load the specific front-end scripts you need manually.
    – EAMann
    Jun 20 '12 at 19:11
  • 1
    Without seeing your site, there's only so much advice I can give. The click handler is wrapped in another check: if (woocommerce_params.option_ajax_add_to_cart=='yes') { is the code getting past this?
    – EAMann
    Jun 20 '12 at 20:56
  • 1
    Hi, checked that, and I got further, now it seems the $.post() doesn't get a response at all. It seems admin-ajax.php doesn't return anything. Jun 22 '12 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.