Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Anriëtte Myburgh Jun 19 '12 at 21:17
add comment

1 Answer

up vote 1 down vote accepted

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); ?>

</a>

<?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.

share|improve this answer
    
Oh, awesome! I'll give it a try. –  Anriëtte Myburgh Jun 20 '12 at 18:54
    
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 –  Anriëtte Myburgh 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
    
Thanks. Really appreciate your help. –  Anriëtte Myburgh Jun 20 '12 at 19:18
    
I can see it's loading the default woocommerce.min.js and related files, but the button's not caught by the live('click') handler. Any advice? –  Anriëtte Myburgh Jun 20 '12 at 20:48
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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