Wordpress product gird loop with sale price, regular price, discount label

PHOTO EMBED

Tue Oct 18 2022 13:31:42 GMT+0000 (Coordinated Universal Time)

Saved by @hamzahanif192

function sale_badge_percentage() {
   global $product;
   if ( ! $product->is_on_sale() ) return;
   if ( $product->is_type( 'simple' ) ) {
      $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
   } elseif ( $product->is_type( 'variable' ) ) {
      $max_percentage = 0;
      foreach ( $product->get_children() as $child_id ) {
         $variation = wc_get_product( $child_id );
         $price = $variation->get_regular_price();
         $sale = $variation->get_sale_price();
         if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
         if ( $percentage > $max_percentage ) {
            $max_percentage = $percentage;
         }
      }
   }
   if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . round($max_percentage) . "%</span>"; // If you would like to show -40% off then add text after % sign
}

add_action( 'wp_enqueue_scripts', 'addcaraousel' );

function slick_cdn_enqueue_scripts()
{
	wp_enqueue_style('slick-style', '//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css');
	wp_enqueue_script('slick-script', '//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js', array(), null, true);
	  wp_enqueue_script( 'myowl', get_template_directory_uri() . '/assets/js/myowl.js', array( 'jquery' ), '', true );
}
add_action('wp_enqueue_scripts', 'slick_cdn_enqueue_scripts');


// Star here create product carousel

function latestproduct_loop()
{
	
	$arg = array(
		'post_type' => 'product',
		'posts_per_page' => 7,
		);
        $productPost = new WP_Query($arg);
	?>
	<div id="latestProduct">
	<?php if ($productPost->have_posts()) : ?>
			<?php while ($productPost->have_posts()) : ?>
			<?php $productPost->the_post(); 
			$product_id = get_the_ID();
			$content= get_the_excerpt();
		    // = get_post_meta( get_the_ID(), '_regular_price', true);
		    $productPost_id = wc_get_product( $product_id );
		   $price = $productPost_id->get_regular_price();
            $price_sale = $productPost_id->get_sale_price();
            $price_get =  $productPost_id->get_price();
			?>
			<div class="innerCard">
			    <div class="imageArea">
				
					<?php $pro = "product_id=".$product_id; ?>
				
			        <div><?php  echo do_shortcode('[yith_wcwl_add_to_wishlist {$pro} ]'); ?></div>
			        <?php the_post_thumbnail( 'full', array( 'class' => 'latestProduct-img' ) ); ?>
			        <div class="salebadge" >Sale <?php echo sale_badge_percentage(); ?></div>
			    </div>
			    <div class="cardDetails">
			        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p><?php echo $content; ?></p>
                   <?php
                   $product = wc_get_product( $product_id );
                    echo '<span class="product-rating__stars">'.wc_get_rating_html( $product->get_average_rating() ).'</span>';
                    ?>
                    
                    <?php
                    if ($price_sale){
                        echo '<div class="withsalePrice">';
                        echo '<h4>$'.$price .'</h4>';
                        echo '<h3>$'.$price_sale .'</h3>';
                        echo '</div>';
                    }else{
                         echo '<div class="onlyPrice">';
                        echo '<h3>$'.$price_get .'</h3>';
                         echo '</div>';
                    }
                    ?>
			    </div>
			</div>
			<?php endwhile; ?>
            <?php endif; ?>
</div>
<?php
	wp_reset_postdata();

}
add_shortcode('latestproducts', 'latestproduct_loop');
content_copyCOPY