WooCommerce: Displaying Only Discounted Products

Hi, several hours ago my friend Andrea from Italy asked me about how to displaying only discounted products while using WooCommerce. The unique case is, she is not using a ColorLabs themes. So, i have to create a code that can used for any themes out there.

At this post, i will tell you how to made it. This code can be placed at any place you want:

<?php
	$i = 1;
  $args = array(
		'post_type' => 'product',
    'stock' => 1,
    'showposts' => 4,
    'orderby' =>'date',
    'order' => 'DESC' ,
    'meta_query'  => array(
			array(
				'key' => '_sale_price',
        'value' => 0,
        'compare' => '>',
        'type' => 'numeric'
             )
     )
  );
                        
	$loop = new WP_Query( $args );
  while ( $loop->have_posts() ) : $loop->the_post(); global $product;
  if ( $i == 1 ) {
?>

	<div class="big-sale-wrap">
		<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"></a>
		<?php
		if ( has_post_thumbnail($loop->post->ID) ) {
			get_the_post_thumbnail( $post_id, 'large' );   
		}else {}
		?>
															
		<div class="inside-big">
			<h3><?php the_title(); ?></h3>
			<span class="price"><?php echo $product->get_price_html(); ?></span>
			<?php
				$test = get_post_meta($post->ID, '_sale_price', true);
				$test2 = get_post_meta($post->ID, '_regular_price', true);
				$hasil = round((($test2 - $test) / $test2)*100);
				echo '<div class="disc-value">You Save ' . $hasil . '% </div>';
		 
				$sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
				$sales_price_date_to = date("j F y", $sales_price_to);
				echo '<div class="end-date">Get this item before <span class="txt">' . $sales_price_date_to . '!</span></div>';
			?>
		</div>                
	</div>

<?php } else {?>  
 
	<div class="small-sale-wrap clearfloat">
		<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"></a>
		
		<?php
			if ( has_post_thumbnail($loop->post->ID) ) {
				get_the_post_thumbnail( $post_id, 'medium' );   
			}else {}
		?>

		<div class="inside-small">
			<h3><?php the_title(); ?></h3>
			<span class="price"><?php echo $product->get_price_html(); ?></span>
																	
		<?php
			$test = get_post_meta($post->ID, '_sale_price', true);
			$test2 = get_post_meta($post->ID, '_regular_price', true);
			$hasil = round((($test2 - $test) / $test2)*100);
			echo '<div class="disc-value">You Save ' . $hasil . '% </div>';
		?>
		</div>
													
	</div>    
 
<?php }?>
 
<?php $i++; endwhile; ?>
<?php wp_reset_query(); ?>

And that’s it!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s