menardmam
menardmam

Reputation: 9986

WooCommerce Custom Loop to get all product from one specific category

I try to find the code (short code) in the woo comm plugin that made the list of all the product from one category, to be able to modify it... no luck after 1 hours, still no find.

So i start coding it myself (reinventing the wheel) and here what i try to get

get me all the product from category ID="151" and be able to output the name, the permalink etc etc...

this is the code now, that return everything.... way too much ! and i don't know how to filter it

{
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 99
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
    //echo get_title()."<br/>";
    var_dump($loop);
endwhile;
} 

Upvotes: 3

Views: 31322

Answers (3)

Shina
Shina

Reputation: 2064

I had similar problem as I wanted to get "Fabrics" product for a "Custom Page", here is the code I used.

<ul class="products">
<?php
    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 12,
        'product_cat' => 'fabrics'
        );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
            woocommerce_get_template_part( 'content', 'product' );
        endwhile;
    } else {
        echo __( 'No products found' );
    }
    wp_reset_postdata();
?>
</ul><!--/.products-->

Using the above code means you get the default inline styles, classes and other necessary tags.

Upvotes: 4

benz001
benz001

Reputation: 2168

The [product_category] shortcode defined in /woocommerce/includes/class-wc-shortcodes.php is a great starting place for this, particularly as Woocommerce is constantly evolving!

The guts of it is just a standard WP_Query with some extra code added on to do pagination, setting sort orders from the Woocommerce settings and some checking to see if the products are marked as visible or not.

So if you strip out the shortcode related code and wanted a function that just got visible products from a category with a specific slug, it would look like this:

function getCategoryProducts($category_slug) {
    // Default Woocommerce ordering args
    $ordering_args = WC()->query->get_catalog_ordering_args();

    $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'ignore_sticky_posts'   => 1,
            'orderby'               => $ordering_args['orderby'],
            'order'                 => $ordering_args['order'],
            'posts_per_page'        => '12',
            'meta_query'            => array(
                array(
                    'key'           => '_visibility',
                    'value'         => array('catalog', 'visible'),
                    'compare'       => 'IN'
                )
            ),
            'tax_query'             => array(
                array(
                    'taxonomy'      => 'product_cat',
                    'terms'         => array( esc_attr( $category_slug ) ),
                    'field'         => 'slug',
                    'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
                )
            )
        );

    if ( isset( $ordering_args['meta_key'] ) ) {
            $args['meta_key'] = $ordering_args['meta_key'];
    }
    $products = new WP_Query($args);

    woocommerce_reset_loop();
    wp_reset_postdata();

    return $products;
}

Pass in the slug and you'll get back a standard wordpress posts collection using the same ordering that you've configured in your Woocommerce settings.

Upvotes: 4

menardmam
menardmam

Reputation: 9986

here is the code i have found, and modify to my needs

function get_me_list_of($atts, $content = null)
{   
    $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );

    $loop = new WP_Query( $args );

    echo '<h1 class="upp">Style '.$atts[0].'</h1>';
    echo "<ul class='mylisting'>";
    while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product; 

    echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';

    endwhile; 

    echo "</ul>";

    wp_reset_query(); 

}

?>

Upvotes: 8

Related Questions