Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX add to cart button not working on custom query loop product woocommerce

I'm building a custom e-commerce website using woocommerce and I'm having some trouble fixing the "add to cart button". Whenever I add the multiple amounts in the input box/quantity box it only increments or adds one item to the cart. This only happens when I create a custom loop.

On the shop and single-product page, it works fine. If I add 10 items and press the add to cart button. It exactly adds 10 items to cart.

Here is the template I have been working.

<?php

/*
* Template Name: Home
*/

get_header(); ?>

<section class="full-width home-template">

    <div class="full-width shop-section">

        <div class="container">

            <?php
            $args = array(
                    'post_type' => 'product',
                    'meta_query' => array(
                        array(
                            'key' => '_stock_status',
                            'value' => 'instock'
                        )
                    )
            );

            $crate_products = new WP_Query ( $args );
            if ( $crate_products->have_posts() ) : while ( $crate_products->have_posts() ) :
              $crate_products->the_post();

            ?>

            <div id="post-<?php the_ID() ?>" class="three columns product-post">

                <?php // wc_get_template_part('content', 'product'); ?>


                <figure class="featured-image">
                    <?php
                    //Display Product Thumbnail
                    $product_thumbnail =  woocommerce_get_product_thumbnail();

                    ?>

                    <a href="<?php  the_permalink()?>" ><?php echo $product_thumbnail ?></a>
                </figure>


                <h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template( 'single-product/price.php' ); ?></a></h2>
                <span class="product-name"><?php the_title(); ?></span>

                <?php // woocommerce_quantity_input(); ?>


                <div class="add-to-cart-btn">
                <?php woocommerce_template_loop_add_to_cart( $crate_products->post, $product ); ?>
                <?php // do_action( 'woocommerce_after_shop_loop_item' ); ?>
                </div>


            </div>

            <?php wp_reset_postdata(); ?>

            <?php endwhile; else: ?>

            <?php endif; ?>

            <?php wp_reset_query(); ?>


        </div>

    </div>


</section>


<?php get_footer(); ?>

What's confusing also is that the AJAX functionality works on the upsells template(up-sells.php) which is a template of woocommerce and it works fine.

<?php
/**
 * Single Product Up-Sells
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/up-sells.php.
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

global $product, $woocommerce_loop;

$upsells = $product->get_upsells();

if ( sizeof( $upsells ) === 0 ) {
    return;
}

$meta_query = WC()->query->get_meta_query();

$args = array(
    'post_type'           => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows'       => 1,
    'posts_per_page'      => $posts_per_page,
    'orderby'             => $orderby,
    'post__in'            => $upsells,
    'post__not_in'        => array( $product->id ),
    'meta_query'          => $meta_query
);

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $columns;

if ( $products->have_posts() ) : ?>

    <div class="upsells products">

        <div class="twelve columns">
            <h2><?php // _e( 'You may also like&hellip;', 'woocommerce' ) ?></h2>
      </div>

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <div id="post-<?php the_ID() ?>" class="three columns product-post">

                    <?php  wc_get_template_part('content', 'product'); ?>





                </div>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>

    </div>

<?php endif;

wp_reset_postdata();

I have already tried applying the solutions from this developer

  • https://gist.github.com/claudiosmweb/5114131

and also this one

  • https://gist.github.com/webaware/6260468

But it still produces the same output. I really don't know why it only increments one item to the cart. I have checked the browser console for any errors and also have commented out some parts of the code to ensure or let you know that I have tried different methods or options in making the functionality work

like image 729
clestcruz Avatar asked Apr 26 '16 06:04

clestcruz


2 Answers

Follow these steps

  1. Uncomment woocommerce_quantity_input();
  2. Check in Browser Console, if there are any errors in console or not. If yes, then please share your errors here.
  3. If there are no errors then replace

woocommerce_template_loop_add_to_cart( $crate_products->post, $product );
with
print_r(woocommerce_template_loop_add_to_cart( $crate_products->post, $product ));

and check whether it returns any data or not.

Also try uncommenting do_action( 'woocommerce_after_shop_loop_item' );

like image 181
Optimum Creative Avatar answered Oct 07 '22 15:10

Optimum Creative


Here is an updated version.

<?php
/*
* Template Name: Home
*/
get_header(); ?>

<section class="full-width home-template">
    <div class="full-width shop-section">
        <div class="container">

            <?php

            global $product;

            $args = array(
                'post_type' => 'product',
                'meta_query' => array(
                    array(
                        'key' => '_stock_status',
                        'value' => 'instock'
                    )
                )
            );            

            $posts = get_posts( $args );            
            foreach( $posts as $post ) :
                setup_postdata( $post );
                wc_setup_product_data( $post );
                $product = wc_get_product( $post->ID ); ?>

                <div id="post-<?php the_ID() ?>" class="three columns product-post">

                    <figure class="featured-image">
                        <a href="<?php  the_permalink()?>" ><?php echo woocommerce_get_product_thumbnail(); ?></a>
                    </figure>

                    <h2 class="product-price"><a href="<?php the_permalink(); ?>"><?php wc_get_template( 'single-product/price.php' ); ?></a></h2>
                    <span class="product-name"><?php the_title(); ?></span>

                    <?php woocommerce_quantity_input(); ?>

                    <div class="add-to-cart-btn">
                        <?php woocommerce_template_loop_add_to_cart(); ?>                
                    </div>

                </div>

            <?php endforeach; ?>

            <script type="text/javascript">

                (function($){
                    $(document).ready(function(){
                        $(document).on( "keyup", "input.qty", function(){                       
                            $(this).parent().next().find("a").attr( "data-quantity", $(this).val() );
                        });
                    });
                })(jQuery);

            </script>

        </div>
    </div>
</section>

<?php get_footer(); ?>
like image 20
Sark Avatar answered Oct 07 '22 16:10

Sark