<?php
/**
 *
 * This is the section that displays the section on the homepage.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package GrShop
 */

$lproduct_section_label = get_theme_mod( 'lproduct_section_label','Top Picks' );
$lproduct_section_title = get_theme_mod( 'lproduct_section_title','Latest' );
$lproduct_section_focus = get_theme_mod( 'lproduct_section_focus','Products' );
?>

<?php if (class_exists('WooCommerce')) : ?>
<!-- Products Area -->
<section class="products-area gr-section-padding">
    <div class="container">
        <div class="row mb-30">
            <div class="col-12">
                <div class="section-title  text-center m-btm-30">
                    <p class="small-title font-stylish m-btm-10 pr-color"><?php echo $lproduct_section_label;?></p>
                    <h2 class="s-content-title"><?php echo $lproduct_section_title;?> <span class="pr-color"><?php echo $lproduct_section_focus;?></span></h2>
                </div>
            </div>
        </div>
        <div class="row">

            <?php
            // WooCommerce Loop
            if (class_exists('WooCommerce')) {
                $product_number = get_theme_mod( 'show_product', 4 );
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => $product_number // Adjust this value to display desired number of products
                );
                $products = new WP_Query($args);

                if ($products->have_posts()) :
                    while ($products->have_posts()) :
                        $products->the_post();
                        global $product;
                    ?>
                        <div class="col-lg-3 col-md-6 col-12">
                            <!-- Single Product -->
                            <div class="oa-single-product">
                                <!-- Product Head -->
                                <div class="oa-single-product__head">
                                    <?php if ($product->is_on_sale()) : ?>
                                        <div class="p-badge"><?php esc_html_e('Sale','grshop-grocery');?></div>
                                    <?php endif; ?>
                                    <div class="oa-single-product__head">
                                        <a href="<?php the_permalink(); ?>"><?php echo woocommerce_get_product_thumbnail(); ?></a>
                                    </div>
                                    <!-- Button Head -->
                                    <div class="oa-single-product__boverlay">
                                        <!-- Action  Button -->
                                        <div class="oa-single-product__bgroup">
                                            <?php if (class_exists('YITH_WCQV_Frontend')) : ?>
                                                <a data-bs-toggle="modal" data-bs-target="#exampleModal" href="<?php the_permalink(); ?>"><i class="icofont-eye-alt"></i><span><?php esc_html_e('Quick Shop','grshop-grocery');?></span></a>
                                            <?php endif; ?>
                                            <?php if (class_exists('YITH_WCWL')) : ?>
                                                <a title="Wishlist" href="<?php echo esc_url(YITH_WCWL()->get_wishlist_url()); ?>"><i class="icofont-heart"></i><span><?php esc_html_e('Add to Wishlist','grshop-grocery');?></span></a>
                                            <?php endif; ?>
                                            <a title="Compare" href="<?php echo esc_url(add_query_arg('add-to-cart', $product->get_id())); ?>"><i class="icofont-random"></i><span><?php esc_html_e('Add to Compare','grshop-grocery');?></span></a>
                                        </div>
                                        <!-- Action to Cart Button -->
                                        <div class="oa-single-product__bgroup-2">
                                            <a title="Add to cart" href="<?php the_permalink(); ?>"><?php esc_html_e('View Product','grshop-grocery');?></a>
                                        </div>
                                    </div>
                                </div>
                                <!-- Product Content -->
                                <div class="oa-single-product__content">
                                    <div class="oa-single-product__cats"><?php echo wc_get_product_category_list($product->get_id()); ?></div>
                                    <!-- Product Title -->
                                    <h3 class="oa-single-product__title">
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <!-- Product Price -->
                                    <div class="oa-single-product__price">
                                        <b><span><?php echo $product->get_price_html(); ?></b>
                                    </div>
                                    <!-- Rating -->
                                    <div class="oa-single-product__rating">
                                        <?php echo wc_get_rating_html($product->get_average_rating()); ?>
                                        <a href="<?php echo esc_url($product->get_permalink()); ?>" class="total-review"><?php esc_html_e('Revew:','grshop-grocery');?>   (<?php echo $product->get_review_count(); ?>)</a>
                                    </div>
                                    <!-- Product Button -->
                                    <div class="product-button">
                                    <?php woocommerce_template_loop_add_to_cart() ?>
                                    </div>
                                </div>
                            </div>
                            <!-- End Single Product -->
                        </div>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                endif;
            } ?>
        </div>
    </div>
</section>
<!-- End Products Area -->
<?php endif;?>