I need to add custom query and add to cart button dynamic code in woocommerce?

I was make custom template for my WordPress site. So I need to show my product in my custom template dynamically. How can i do that?

This is my html code–>

Read More
             <div class="all_content_shop">
                <div class="col-md-3 single_all_c_s"> 
                    <div class="shop_product_inner"> 
                        <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
                            <img src="http://jetsetbabies.com/wp-content/uploads/2006/07/100Juice_Organic_Apple_MAIN_v2.jpg" alt="Prduct Image" />
                            <div class="shop_product_inner_caption"> 
                                <h2>Product Title</h2>
                                <p>$200</p>
                                <a href="">Add to Cart</a>
                            </div>
                        </a>
                    </div>
                </div>
            </div>

================================================

Also i was make simple query but i did not found add to cart button dynamic code???

This is my query code—->

                <div class="all_content_shop">

                    <?php $new_posts = new WP_Query(array(
                    'post_type' => 'product', //post of page of my post type
                    'cat' => 0, // category id, 0 for all categories. 
                    'posts_per_page' => 12, 
                    'offset' => 0, //how many posts you want to eliminate from the query
                    'orderby' => '', // order by title or date ?
                    'order' => 'DESC') // order as ASC or DESC
                    ); ?>
                    <?php if ($new_posts->have_posts()) :
                        while ($new_posts->have_posts()) :
                                $new_posts->the_post(); ?>

                        <div class="col-md-3 single_all_c_s"> 
                            <div class="shop_product_inner"> 
                                <a href="<?php echo get_permalink( $loop->post->ID ) ?>">
                                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_single'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
                                    <div class="shop_product_inner_caption"> 
                                        <h2><?php the_title(); ?></h2>
                                        <p><?php if ( ! defined( 'ABSPATH' ) ) exit; global $post, $product; ?> <?php echo $product->get_price_html(); ?></p>
                                        <a href="">Add to Cart</a>
                                    </div>
                                </a>
                            </div>
                        </div>

                    <?php endwhile;//Possibility to add else statement ?>
                        <?php wp_reset_postdata(); ?>
                        <?php else:?>
                        <p class="not_found">Sorry, The post you are looking is unavailable!</p>
                    <?php endif; wp_reset_query(); ?>
            </div>

So my question is—>

  1. My query is right or wrong?? if my query is wrong, please give me right query… Also if my query is almost right so please give me just “Add to cart” (http://prntscr.com/439lxj) button dynamic Ajax code…

Please help me…
Please help me…

Thanks !!!

Related posts

Leave a Reply

1 comment

  1. I have made the necessary changes for the code to work. Make sure woocommerce add-to-cart.js or add-to-cart.min.js is enqueued in the page.

    <div class="all_content_shop">
    <?php wc_print_notices(); ?>
    <?php $new_posts = new WP_Query( array(
        'post_type'      => 'product', //post of page of my post type
        'cat'            => 0, // category id, 0 for all categories.
        'posts_per_page' => 12,
        'offset'         => 0, //how many posts you want to eliminate from the query
        'orderby'        => '', // order by title or date ?
        'order'          => 'DESC'
    ) // order as ASC or DESC
    ); ?>
    <?php if ( $new_posts->have_posts() ) :
        while ( $new_posts->have_posts() ) :
            $new_posts->the_post();
            global $product;
            $product = get_product( get_the_ID() ); //set the global product object?>
    
            <div class="col-md-3 single_all_c_s">
                <div class="shop_product_inner">
                    <a href="<?php the_permalink() ?>">
                        <?php if ( has_post_thumbnail( get_the_ID() ) ) {
                            echo get_the_post_thumbnail( get_the_ID(), 'shop_single' );
                        } else {
                            echo '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" />';
                        } ?>
                        <div class="shop_product_inner_caption">
                            <h2><?php the_title(); ?></h2>
    
                            <p><?php echo $product->get_price_html(); ?></p>
                            <?php woocommerce_template_loop_add_to_cart(); //ouptput the woocommerce loop add to cart button ?>
                        </div>
                    </a>
                </div>
            </div>
    
        <?php endwhile;//Possibility to add else statement ?>
        <?php wp_reset_postdata(); ?>
    <?php else: ?>
        <p class="not_found">Sorry, The post you are looking is unavailable!</p>
    <?php endif;
    wp_reset_query(); ?>