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 ) {
   ob_start();

   // 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="https://wordpress.stackexchange.com/questions/310365/<?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>
                  </a>
                    <p>
                      <?php echo $product->description; ?>
                    </p>
                    <span class="price"><?php echo $product->get_price_html(); ?></span>                    
                </a>
                <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
            </li>
      </ul>
    <?php
        $myvariable = ob_get_clean();
        return $myvariable;
    }
}

My query is returning nothing right now.

0

Leave a Comment