Product Loop
Thu Sep 28 2023 16:49:06 GMT+0000 (Coordinated Universal Time)
Saved by @hamzakhan123
function product_loop() { ob_start(); $arg = array( 'post_type' => 'product', 'posts_per_page' => -1, ); $themeslider = new WP_Query($arg); ?> <div class="row productSlider"> <?php if ($themeslider->have_posts()) : ?> <?php while ($themeslider->have_posts()) : ?> <?php $themeslider->the_post();?> <?php global $product; ?> <div class="col-md-4"> <div class="productWrap"> <div class="productImg"> <a href="<?php echo get_permalink( $product->get_id() ); ?>"> <?php the_post_thumbnail('full'); ?> </a> </div> <div class="productContent"> <h4 class="text-center"><?php echo $product->get_title(); ?></h4> <div class="detailWrap"> <div class="detailCat"> <span class="list-icon fas fa-cog"></span> <?php echo $product->get_categories(); ?> </div> <div class="detailPrice"> <?php if ($product->is_type('simple')) { echo str_replace('.00', '', $product->get_price_html()); } if ($product->get_type() == 'variable') { $available_variations = $product->get_available_variations(); $variation_id = $available_variations[0]['variation_id']; $variable_product = new WC_Product_Variation($variation_id); $regular_price = $variable_product->get_regular_price(); $sales_price = $variable_product->get_sale_price(); if (empty($sales_price)) { $sales_price = 0; } $total_price = $regular_price + $sales_price; echo '$' . number_format($total_price, 0, '', ''); } ?> </div> </div> <div class="ratingWrap"> <div class="ratings"> <?php if($product->get_average_rating() != 0){ ?> <div class="ratingStar producRating-<?php echo round($product->get_average_rating()) ?>"></div> <?php } ?> </div> <div class="cartBtn"> <a href="<?php echo get_permalink( $product->get_id() ); ?>">book now</a> </div> </div> </div> </div> </div> <?php endwhile; ?> <?php endif; ?> </div> <?php wp_reset_postdata(); return '' . ob_get_clean(); } add_shortcode('product_code', 'product_loop');
Comments