Skip to content

Instantly share code, notes, and snippets.

@gizalink
Last active September 22, 2015 13:06
Show Gist options
  • Save gizalink/1706c7b20a90dd7e0696 to your computer and use it in GitHub Desktop.
Save gizalink/1706c7b20a90dd7e0696 to your computer and use it in GitHub Desktop.
Check Product is new (recent product)
<?php
// Function check product in woocommerce is new //
// Label "New" added after title product . Product on archive//
// Author: Nguyen Phuc Gia Linh //
// Contact: nguyenphucgialinh@gmail.com //
function check_id_last()
{
$args = array( 'numberposts' => '1' ,
'post_type' => 'product',
'orderby' => 'ID',
'order' => 'DESC',
);
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
return $recent["ID"];
}
}
function action_woocommerce_after_shop_loop_item_title( )
{
global $post, $product;
if ( $product->is_on_sale() ) :
echo '<img src="'.get_template_directory_uri().'/icon_km.gif" class="img-km"/>';
endif;
$maxid = (int) check_id_last();
$proid= (int) get_the_id();
$hieu = $maxid - $proid;
if($hieu<=5):
echo '<img src="'.get_template_directory_uri().'/icon_new.gif" class="img-km"/>';
endif;
};
// add the action
add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_after_shop_loop_item_title', 10, 0 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment