<?php
// Get setting to determine whether to display the section
$expert_dental_specialist_aboutus = get_theme_mod('expert_dental_specialist_our_products_show_hide_section', false);

if ($expert_dental_specialist_aboutus == '1') : ?>
<section id="product-section" class="my-5 mx-md-0 mx-3">
  <div class="container">
    <div class="product-main-head mx-auto text-center">
     <?php
      // Small heading for the section
      $expert_dental_specialist_our_products_small_heading_section = get_theme_mod('expert_dental_specialist_our_products_small_heading_section');
      if (!empty($expert_dental_specialist_our_products_small_heading_section)) : ?>
        <p class="small-product-heading">
          <?php echo esc_html($expert_dental_specialist_our_products_small_heading_section); ?>
        </p>
      <?php endif; ?>

      <?php
      // Main heading for the section
      $expert_dental_specialist_our_products_heading_section = get_theme_mod('expert_dental_specialist_our_products_heading_section');
      if (!empty($expert_dental_specialist_our_products_heading_section)) : ?>
        <h2 class="product-heading mb-3">
          <?php echo esc_html($expert_dental_specialist_our_products_heading_section); ?>
        </h2>
      <?php endif; ?>
    </div>
    <?php if (class_exists('WooCommerce')) : ?>
      <div class="owl-carousel expert-products-carousel">
        <?php
        // Fetch and display products from selected category
        $expert_dental_specialist_selected_category = get_theme_mod('expert_dental_specialist_our_product_product_category','product_cat1');
        
        if (!empty($expert_dental_specialist_selected_category)) {
          $expert_dental_specialist_args = array(
            'post_type'      => 'product',
            'posts_per_page' => 50, 
            'product_cat'    => $expert_dental_specialist_selected_category,
            'order'          => 'ASC'
          );

          $expert_dental_specialist_loop = new WP_Query($expert_dental_specialist_args);
          while ($expert_dental_specialist_loop->have_posts()) : $expert_dental_specialist_loop->the_post();
            global $product;
        ?>
          <div class="product-box px-2 wow fadeInUp" data-wow-delay="0.5s">
            <div class="product-image position-relative">
              <?php
              // Display sale badge
              if ($product->is_on_sale()) {
                echo '<span class="product-badge">Sale</span>';
              }

              // Wishlist icon if YITH is active
              if (class_exists('YITH_WCWL')) {
                echo '<a href="' . esc_url(YITH_WCWL()->get_wishlist_url()) . '" class="wishlist-icon" aria-label="Add to Wishlist">
                        <i class="far fa-heart"></i>
                      </a>';
              }
              ?>
              <?php echo woocommerce_get_product_thumbnail(); ?>
            </div>
            <div class="product-content text-start px-1 pt-1 pb-3">
              <h3 class="my-1">
                <a href="<?php the_permalink(); ?>"><?php echo esc_html(get_the_title()); ?></a>
              </h3>
              <p class="mb-2 product-price">
                <?php echo $product->get_price_html(); ?>
              </p>
              <div class="cart-button mt-1">
  <?php if( $product->is_type( 'simple' ) ){ woocommerce_template_loop_add_to_cart( $expert_dental_specialist_loop->post, $product ); } ?>
</div>

            </div>
          </div>
        <?php
          endwhile;
          wp_reset_postdata();
        }
        ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php endif; ?>