Product with shortout with category

PHOTO EMBED

Tue Apr 04 2023 11:47:43 GMT+0000 (Coordinated Universal Time)

Saved by @hamzahanif192

function product_loop($atts)
{
	ob_start();
	wp_reset_postdata();
?>
	<?php
   extract(shortcode_atts(array(
		'category' => ''
	), $atts));


	$arg = array(
		'post_type' => 'product',
		'posts_per_page' => 12,
		'tax_query' => array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => $atts['category']
            )
        )
	);

	$sliderPost = new WP_Query($arg);

	?>
	<div class="row">
		<?php if ($sliderPost->have_posts()) : ?>
			<?php while ($sliderPost->have_posts()) : ?>
				<?php $sliderPost->the_post(); ?>
				<?php $url = wp_get_attachment_url(get_post_thumbnail_id($sliderPost->ID));
                    global $product;
				?>
                <div class="col-lg-3 col-md-4 col-sm-6">
                    <div class="productWrap">
                        <div class="productImg">
                                <?php 
                                    if($product->get_stock_status() == 'outofstock'){ ?>
                                        <div class="outofstock">Out of Stock</div>
                                       <?php } else{
                                            if(!$product->get_sale_price()){?>
                                        
                                                <div class="regular_price">New</div>
                                            
                                            <?php } elseif($product->get_sale_price()) {?>
                                     
                                            <div class="sale_price">Sale</div>
                                         
                                    <?php 
                                         }
                                    }
                                ?>
                            <a href="<?php echo get_permalink( $product->get_id() ); ?>">
                                <?php echo $product->get_image('full'); ?>
                            </a>
                            <div class="cartBtn">
                                <a href="<?php echo get_site_url(); ?>/cart/?add-to-cart=<?php echo $product->get_ID(); ?>">
                                    <span>Add To Cart</span>
                                </a>
                            </div>
                        </div>
                        <div class="product_content">
                            <div class="priceProduct">
                                <a href="<?php echo get_permalink( $product->get_id() ); ?>">
                                    <h4><?php echo $product->get_title(); ?></h4>
                                </a>
                                <?php  if ($product->is_type( 'simple' )) { ?>
                                <?php echo $product->get_price_html(); ?>
                                <?php } ?>
                                <?php 
                                  if($product->get_type()=='variable') {
                                      $available_variations = $product->get_available_variations();
                                      $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
                                      $variable_product1= new WC_Product_Variation( $variation_id );
                                      $regular_price = $variable_product1 ->regular_price;
                                      $sales_price = $variable_product1 -> sale_price;
                                      if (empty($sales_price)) {
                                      $sales_price = 0;
                                      }
                                      echo "$" . ($regular_price  + $sales_price);
                                      }
                                  ?>
                            </div>
                            <?php if($product->get_average_rating() != 0){ ?>
                                <div class="ratingStar producRating-<?php echo round($product->get_average_rating()) ?>"></div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
            <?php endwhile; ?>
        <?php endif; ?>
</div>

<?php
	wp_reset_postdata();
	return '' . ob_get_clean();
}
add_shortcode('product_code', 'product_loop');
content_copyCOPY