Question

I'm attempting to display one or more specific product(s) on my home page on Woocommerce, in a very simple fashion :

  • name of the product;
  • short description of the product;
  • price of the product;
  • quantity selector;
  • add to cart button;

Now, i've created a custom loop for doing so:

<?php
    $args = array(
        'post_type' => 'product',
        'sku' => 'lundivegetarien',
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            echo the_title();
            echo woocommerce_template_single_excerpt();
            echo woocommerce_template_single_price();
            echo woocommerce_template_single_add_to_cart();
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
?>

Main problem here is that this loop displays all my products, irrespectively of the sku i'm trying to call. I would like to be more specific, and be able to choose to display one or several products i'd call by their specific sku.

What am i doing wrong?

Any pointers?

Help appreciated!

Was it helpful?

Solution

Sorted out the problem myself eventually, so dumb i was! Just turned to using categories and it works pretty fine!

Here's the updated code in cas anybody needs it!

<?php
    $args = array( 'post_type' => 'product', 'product_cat' => 'name_of_the_category', 'posts_per_page' => 1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); global $product; 
?>

<p>
    <?php 
    the_title(); 
    ?>
</p>

<?php 
    echo woocommerce_template_single_excerpt();
    echo $product->get_price_html();
?>

<div class="order_form close">
    <p>
        <?php 
        woocommerce_template_loop_add_to_cart( $loop->post, $product ); 
        ?>
    </p>
</div>               
<br>
<?php 
endwhile; 
?>

Hope this will help someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top