<?php
/**
 * Template part for displaying the services section
 *
 * @package Optical Glasses
 * @subpackage optical_glasses
 */

// Get setting to determine whether to display the section
$optical_glasses_aboutus = get_theme_mod('optical_glasses_our_products_show_hide_section', true);

if ($optical_glasses_aboutus == '1') : ?>
<section id="product-section" class="my-5 mx-md-0 mx-3">
  <div class="container">
    <div class="product-main-head">
      <?php
      $optical_glasses_our_products_heading_section = get_theme_mod('optical_glasses_our_products_heading_section');
      if (!empty($optical_glasses_our_products_heading_section)) : ?>
          <h2 class="product-heading">
              <?php echo esc_html($optical_glasses_our_products_heading_section); ?>
          </h2>
      <?php endif; ?>
     <?php
      $optical_glasses_our_products_small_title_section = get_theme_mod('optical_glasses_our_products_small_title_section');
      if (!empty($optical_glasses_our_products_small_title_section)) : ?>
          <p class="product-small-heading mb-4">
              <?php echo esc_html($optical_glasses_our_products_small_title_section); ?>
          </p>
      <?php endif; ?>
    </div>
    <?php if (class_exists('WooCommerce')) : ?>
      <div class="owl-carousel">
        <?php
        // Fetch and display products from selected category
        $optical_glasses_selected_category = get_theme_mod('optical_glasses_our_product_product_category');

        if (!empty($optical_glasses_selected_category)) {
          $optical_glasses_args = array(
            'post_type'      => 'product',
            'posts_per_page' => 50,
            'product_cat'    => $optical_glasses_selected_category,
            'order'          => 'ASC',
          );

          $optical_glasses_loop = new WP_Query($optical_glasses_args);

          while ($optical_glasses_loop->have_posts()) : $optical_glasses_loop->the_post();
            global $product;
            ?>
            <div class="product-box position-relative">
              <div class="product-box-img product-image position-relative">
                <?php
                if ($product->is_on_sale()) {
                    echo '<span class="onsale">' . __('Best Seller', 'optical-glasses') . '</span>';
                }
                ?>
                  <?php if ( has_post_thumbnail( $optical_glasses_loop->post->ID ) ) {
                    echo get_the_post_thumbnail( $optical_glasses_loop->post->ID, 'shop_catalog' );
                  } else {
                    echo '<img src="'.esc_url(woocommerce_placeholder_img_src()).'" />';
                  } ?>
                  <div class="product-icon position-absolute product-actions-overlay">
                    <?php if (defined('YITH_WCWL')) { ?>
                      <?php echo do_shortcode('[yith_wcwl_add_to_wishlist]'); ?>
                    <?php } ?>
                    <?php if ( class_exists( 'YITH_WCQV_Frontend' ) ) {
                      echo do_shortcode('[yith_quick_view]');
                    } ?>
                  </div>
                </div>
              <div class="product-content text-start">
              <?php
                $optical_glasses_categories = get_the_terms(get_the_ID(), 'product_cat');

                if ($optical_glasses_categories && !is_wp_error($optical_glasses_categories)) {
                    $category = $optical_glasses_categories[0];
                    $category_link = get_term_link($category);

                    echo '<span class="product-category">';
                    echo '<a href="' . esc_url($category_link) . '">';
                    echo esc_html($category->name);
                    echo '</a>';
                    echo '</span>';
                }
                ?>
              <h3 class="my-0">
                  <a class="text-capitalize" href="<?php the_permalink(); ?>">
                      <?php echo esc_html(get_the_title()); ?>
                  </a>
              </h3>
              <p class="mb-0 product-price">
                  <span class="price-reverse"><?php echo $product->get_price_html(); ?></span>
              </p>
          </div>
            </div>
          <?php
          endwhile;
          wp_reset_postdata();
        }
        ?>
      </div>
    <?php endif; ?>
  </div>
</section>
<?php endif; ?>