Skip to content

Instantly share code, notes, and snippets.

@maxsum-corin
Last active January 4, 2017 00:08
Show Gist options
  • Save maxsum-corin/4526990 to your computer and use it in GitHub Desktop.
Save maxsum-corin/4526990 to your computer and use it in GitHub Desktop.
Modified to use is_on_sale, which includes variation products

This plugin adds a sale items shortcode to WooCommerce. Usage:

[sale_products]

Options & defaults:

per_page=12
columns=4
orderby=title
order=asc

This code was taken directly from the shortcode-init.php file in WooCommerce 1.6.4 and modified to output sale items instead of featured items. It's been submitted to the Woocommerce git repo, so hopefully it will be added soon and you won't need a separate plugin.

<?php
/*
Plugin Name: Woocommerce Sale Products Shortcode
Version: 0.1
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* List all products on sale
*
* @access public
* @param array $atts
* @return string
*/
function woocommerce_sale_products( $atts ){
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'title',
'order' => 'asc'
), $atts ) );
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $orderby,
'order' => $order,
'posts_per_page' => $per_page,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<ul class="products">
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php $product = new WC_Product( $post->ID ) ?>
<?php if ($product->is_on_sale()) : ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php endif;
wp_reset_query();
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('sale_products', 'woocommerce_sale_products');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment