Woocommerce query specific product from specific category

I’m trying to create a shortcode that will show a woocommerce product via just a shortcode. I want the shortcode to take 1 parameter that is either the ID or slug of the actual product. Preferably slug. The category needs to be in there since there maybe 2 products matching the slug. One matching a category and one not matching.

I’m not sure how to get the correct product to display. Here’s my code:

add_shortcode( ‘pxair-products’, ‘pxair_shortcode’ );
function pxair_shortcode( $atts ) {

// define attributes and their defaults
extract( shortcode_atts( array (
‘post_type’ => ‘product’,
‘posts_per_page’ => 1,
), $atts ) );

// define query parameters based on attributes
$args = array(
‘category’ => array( ‘pxair’ ),
$products = wc_get_products( $args );

$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) { ?>
<ul class=”clothes-listing “>
<li class=”product”>
<a href=”<?php echo get_permalink( $loop->post->ID ) ?>” title=”<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>”>
<?php //woocommerce_show_product_sale_flash( $post, $product ); ?>
<?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=”300px” height=”300px” />’; ?>
<h3><?php the_title(); ?></h3>
<?php echo $product->description; ?>
<span class=”price”><?php echo $product->get_price_html(); ?></span>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
$myvariable = ob_get_clean();
return $myvariable;

My query is returning nothing right now.

Read more here:: Woocommerce query specific product from specific category

Leave a Reply

Your email address will not be published. Required fields are marked *