lorro

WooCommerce - Show free products shortcode

Nov 8th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2.   // show free products in the shop
  3.   // Use: [free_products columns="3" orderby="title" order="asc"]
  4.   // code goes in functions.php for your child theme
  5.   add_shortcode('free_products', 'free_products');
  6.   function free_products( $atts ) {
  7.     $atts = shortcode_atts( array(
  8.       'columns' => '4',
  9.       'orderby' => 'title',
  10.       'order'   => 'asc',
  11.     ), $atts );
  12.  
  13.     $query_args = array(
  14.       'post_type'           => 'product',
  15.       'post_status'         => 'publish',
  16.       'ignore_sticky_posts' => 1,
  17.       'orderby'             => $atts['orderby'],
  18.       'order'               => $atts['order'],
  19.       'posts_per_page'      => 12,
  20.       'meta_query' => array(
  21.         array(
  22.         'key' => '_price',
  23.         'value' => 0,
  24.         'compare' => '=',
  25.         'type' => 'NUMERIC'
  26.         )
  27.       )
  28.     );
  29.  
  30.     $loop = new WP_Query($query_args);
  31.     print '<div class="clear"></div>';
  32.     print '<ul class="products-list">';
  33.     if ( $loop->have_posts() ) {
  34.       while ( $loop->have_posts() ) : $loop->the_post();
  35.         wc_get_template_part( 'content', 'product' );
  36.       endwhile;
  37.     } else {
  38.       echo __( 'No products found' );
  39.     }
  40.     print '</ul>';
  41.     print '<div class="clear"></div>';
  42.     wp_reset_postdata();
  43.     return;
  44.   }
Add Comment
Please, Sign In to add comment