WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display woocommerce recent products on home page (12 posts)

  1. amarstyle
    Member
    Posted 1 year ago #

    How can i display woocommerce recent products on my home Page?

    someone please help.
    I know where to add but just dont know the exact code. i want to show 4 recently added products with link to to view more.

    my website is : http://amarstyle.com

    please help.

    thanks in advance.

  2. amarstyle
    Member
    Posted 1 year ago #

    figured it out myself.

  3. dharmendra1989
    Member
    Posted 1 year ago #

    Hello amarstyle,

    I also want to do same thing on my website, so please could you tell me how to solved you on your website??

    I hope for your immediate response!!

  4. amarstyle
    Member
    Posted 1 year ago #

    add this in your index page

    <ul class="productshome">
        <?php
            $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 2, 'product_cat' => 'Salwar-Kameez', 'orderby' =>'rand','order' => 'DESC' );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    
                    <li class="producthome">    
    
                        <a>post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
    
                            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
    
                            <h3><?php the_title(); ?></h3>
    
                        	   <span class="price"><?php echo $product->get_price_html(); ?></span>
    
                        </a>
    
                        <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
    
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>
    
    </div>
  5. Jon Reader
    Member
    Posted 11 months ago #

    Amarstyle's code was slightly broken, so I've updated it.

    You can drop this straight into your code and it should work - it returns four products, arranges the product via date and is bootstrap ready (if you use it).

    <section id="recent">
    
        <h1>Recently Added</h1>
    
        <ul class="row-fluid">
    
            <?php
                $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    
                        <li class="span3">    
    
                            <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    
                                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; ?>
    
                                <h3><?php the_title(); ?></h3>
    
                            	   <span class="price"><?php echo $product->get_price_html(); ?></span>
    
                            </a>
    
                            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                        </li><!-- /span3 -->
            <?php endwhile; ?>
            <?php wp_reset_query(); ?>
    
        </ul><!-- /row-fluid -->
    </section><!-- /recent -->
  6. icevincent
    Member
    Posted 11 months ago #

    Jon,
    Your code is great! It's working!

    But one trouble, they couldn's display the button "add to cart", the place is just words with link, not a image of button.

    Can you please tell me the reason? Thank you very much!

  7. MsFunk
    Member
    Posted 11 months ago #

    Ice,
    The Add to Cart link has the classes "add_to_cart_button" and "button" that can be used to make it look like a button with CSS.

  8. jorgeanr
    Member
    Posted 11 months ago #

    Guys, where I need to put this code? Tks

  9. icevincent
    Member
    Posted 11 months ago #

    Funk,
    Tks for your reply, right, it's a problem of html:)
    $
    Jorgeanr,
    just add the code anywhere you want to display your products list on homepage (index.php or other page setted as homepage).

  10. pradip24
    Member
    Posted 8 months ago #

    Hey Jon Reader, works great can you tell me how to add rating in this code.

  11. hosamu
    Member
    Posted 7 months ago #

    Jon

    Your code is great! It's working!

  12. Permanence
    Member
    Posted 5 months ago #

    This code is great, can anyone help me modify this so instead of latest products it shows featured products?

Topic Closed

This topic has been closed to new replies.

About this Topic