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.

  • 1
    The taxonomy for products is product_cat not category.. as first edit.. and what is $options? – Andrea Somovigo Aug 2 '18 at 19:17
  • I think $options was left over from something else I tried. – Lz430 Aug 2 '18 at 19:24
  • Curious as to why you can't use one of the built-in shortcodes? – Steve Aug 2 '18 at 19:50
  • As it stands, your code is a mess. You're getting WC products into a variable called $products that you're doing nothing with. Your loop has $options for it's args, but $options is not defined. Inside your loop you're calling a variable called $loop which is not defined. I suggest you take a step back and have a good long hard look at the whole thing! Finally, like Steve said, what's wrong with the built-in shortcode of [products category="pxair"]? – Peter HvD Aug 2 '18 at 20:34
  • Truth be told, I didn't know about the woocommerce shortcode. I'm trying to show a single product. I'm using [products skus="11400000" limit="1"], but no results. Is that right? – Lz430 Aug 3 '18 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.