Abu Nooh
Abu Nooh

Reputation: 856

Get Products by Product category in a shortcode with WooCommerce

I have a shortcode, I would like to get all the products from a specific Woocommerce category.

add_shortcode( 'list-products', 'prod_listing_params' );
function prod_listing_params( $atts ) {
ob_start();

extract( shortcode_atts( array (
    'type' => 'product',
    'order' => 'date',
    'orderby' => 'title',
    'posts' => -1,
    'category' => '',
), $atts ) );

$options = array(
    'post_type' => $type,
    'order' => $order,
    'orderby' => $orderby,
    'posts_per_page' => $posts,
    'product_cat' => $product_cat,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
    <div class="#">
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <p class="#">
        <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </span></p>
        <?php endwhile;
        wp_reset_postdata(); ?>
    </div>
<?php
    $myvar = ob_get_clean();
    return $myvar;
}
}

So I use the shortcode:

[list-products category="shoes"]

But it returns all the products from all categories despite providing the category in the shortcode.

How can I amend this to get by category?

Thanks

Upvotes: 1

Views: 4831

Answers (2)

MakeWebBetter
MakeWebBetter

Reputation: 473

you can also use shortcodes provided by WooCommerce itself

[product_category category="appliances"]

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 253868

Instead of 'product_cat' => $product_cat, you should use a tax_query this way:

'tax_query' => array( array(
     'taxonomy' => 'product_cat',
     'field' => 'slug',
      'terms' => $atts['cat'],
) ),

So your code should be something like (I have revisited a bit your code):

// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
    function prod_listing_params( $atts ) {
        ob_start();

         $atts = shortcode_atts( array (
            'type' => 'product',
            'order' => 'date',
            'orderby' => 'title',
            'posts' => -1,
            'category' => '', // category slug
        ), $atts, 'list_products' );

        $query = new WP_Query( array(
            'post_type' => $atts['type'],
            'order' => $atts['order'],
            'orderby' => $atts['orderby'],
            'posts_per_page' => $atts['posts'],
            'tax_query' => array( array(
                 'taxonomy' => 'product_cat',
                 'field' => 'slug',
                  'terms' => $atts['category'],
            ) ),
        ) );

        if ( $query->have_posts() ) { 
            ?>
                <div class="#">
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <p class="#">
                    <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </span></p>
                    <?php endwhile;
                    wp_reset_postdata(); ?>
                </div>
            <?php
            $myvar = ob_get_clean();
            return $myvar;
        }
    }
    add_shortcode( 'list_products', 'prod_listing_params' );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Example USAGE:

[list_products category="shoes"]

Related answers:

Upvotes: 2

Related Questions