<section id="featuredproduct-product-section" class="ht-section">
    <div class="container">
        <div class="featuredproduct-posts-box">
            <div class="section-title">
                <h4><?php echo esc_html(get_theme_mod('featuredproducts_subheading', 'POPULAR DISHES')); ?></h4>
                <h2><?php echo esc_html(get_theme_mod('featuredproducts_heading', 'Best Selling Dishes')); ?></h2>
            </div>
           
          
            <div class="row m-0">  
                <?php
                if ( function_exists('woocommerce_template_loop_add_to_cart') && function_exists('WC') ) {
                    $meta_query   = WC()->query->get_meta_query();
                    $tax_query    = WC()->query->get_tax_query();
                    $tax_query[]  = array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'name',
                        'terms'    => 'featured',
                        'operator' => 'IN',
                    );

                    $args = array(
                        'post_type'      => 'product',
                        'posts_per_page' => 8,
                        'orderby'        => 'date',
                        'order'          => 'DESC',
                        'meta_query'     => $meta_query,
                        'tax_query'      => $tax_query,
                    );

                    $loop = new WP_Query($args);

                    if ($loop->have_posts()) {
                        while ($loop->have_posts()) : $loop->the_post(); 
                            global $product;

                            if ( empty($product) || ! is_a($product, 'WC_Product') ) {
                                $product = wc_get_product(get_the_ID());
                            }
                        ?>
                        <div class="col-lg-4 col-md-6 col-sm-6 product-bx">  
                            <div class="product-grid">
                                <div class="product-image">
                                    <a id="id-<?php the_ID(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                        <?php 
                                        if (has_post_thumbnail()) {
                                            echo get_the_post_thumbnail(get_the_ID(), 'shop_catalog');
                                        } else {
                                            echo '<img class="pic-1" src="' . get_template_directory_uri() . '/assets/images/default.png" alt="Placeholder" />';
                                        }
                                        ?>
                                    </a>
                                    <a class="add-to-cart" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                        <i class="fa-regular fa-heart"></i>
                                    </a>
                                </div>
                                <div class="tag"></div>
                                <div class="productcontent-wrap">
                                    <div class="pcontent">
                                        <a class="add-to-cart" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">    
                                            <h3><?php the_title(); ?></h3>
                                        </a>

                                        <div class="description">
                                            <?php echo wp_trim_words( $product->get_short_description(), 20 ); ?>
                                        </div>

                                        <span class="price">
                                            <?php
                                            $sale_price    = $product->get_sale_price();
                                            $regular_price = $product->get_regular_price();

                                            if ($sale_price) {
                                                echo '<span class="regular-price">' . wc_price($regular_price) . '</span>';
                                                echo '<span class="sale-price">' . wc_price($sale_price) . '</span>';
                                            } else {
                                                echo '<span class="sale-price">' . wc_price($regular_price) . '</span>';
                                            }
                                            ?>
                                        </span>
                                    </div>
                                    <div class="clearfix"></div>
                                </div>
                            </div>
                        </div> 
                    <?php
                        endwhile;
                        wp_reset_postdata();
                    } else {
                        ?>
                        <div class="alert alert-warning text-center">
                            <strong><?php esc_html_e('Sorry, no new products to show.', 'fresh-food-restaurant'); ?></strong>
                        </div>
                    <?php
                    }
                }
                ?>
            </div> 
            <div class="btn">
                <a href="<?php echo esc_url(get_permalink(wc_get_page_id('shop'))); ?>">
                    <?php esc_html_e('VIEW ALL', 'fresh-food-restaurant'); ?>
                </a>
            </div> 
        </div>
        <div class="clearfix"></div>
    </div>
</section>
