<?php



use Elementor\Widget_Base;
use Elementor\Controls_Manager;

if (!defined('ABSPATH'))
  exit; // Exit if accessed directly


class Productscarouselseventhstkaveh extends Widget_Base
{

  public function get_name()
  {
    return 'productscarouselseventhstkaveh';
  }

  public function get_title()
  {
    return esc_html__('Products Carousel Style Seven', 'kavehTheme');
  }

  public function get_icon()
  {
    return 'eicon-archive-posts';
  }

  public function get_categories()
  {
    return ['kavehelements'];
  }

  protected function register_controls()
  {

    $this->start_controls_section(
      'contentproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Title Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );

    $this->add_control(
      'titleproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Title', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => esc_html__('Best Selling Products', 'kavehTheme'),

      ]
    );
    $this->add_control(
      'tagheadingproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Title Tag', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'H2',
        'options' => [
          'H1' => esc_html__('H1', 'kavehTheme'),
          'H2' => esc_html__('H2', 'kavehTheme'),
          'H3' => esc_html__('H3', 'kavehTheme'),
          'H4' => esc_html__('H4', 'kavehTheme'),
          'H5' => esc_html__('H5', 'kavehTheme'),
          'H6' => esc_html__('H6', 'kavehTheme'),
          'p' => esc_html__('p', 'kavehTheme'),
          'div' => esc_html__('div', 'kavehTheme'),
          'span' => esc_html__('span', 'kavehTheme'),
        ],
      ]
    );
    $this->add_control(
      'title2productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Subtitle', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::TEXT,
        'default' => esc_html__('Best Selling Products', 'kavehTheme'),

      ]
    );
    $this->add_control(
      'tag2headingproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Subtitle Tag', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'H4',
        'options' => [
          'H1' => esc_html__('H1', 'kavehTheme'),
          'H2' => esc_html__('H2', 'kavehTheme'),
          'H3' => esc_html__('H3', 'kavehTheme'),
          'H4' => esc_html__('H4', 'kavehTheme'),
          'H5' => esc_html__('H5', 'kavehTheme'),
          'H6' => esc_html__('H6', 'kavehTheme'),
          'p' => esc_html__('p', 'kavehTheme'),
          'div' => esc_html__('div', 'kavehTheme'),
          'span' => esc_html__('span', 'kavehTheme'),
        ],
      ]
    );
    $this->add_control(
      'backjproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Background Image', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::MEDIA,
        'selectors' => [
          '{{WRAPPER}} .offer-books-wrapper::before' => 'background: url({{URL}}) 0 0 no-repeat',
        ],
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'content2productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Product Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );
    $this->add_control(
      'stylecartproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Product Box Layout', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'one',
        'options' => [
          'one' => esc_html__('Layout One', 'kavehTheme'),
						'two' => esc_html__('Layout Two', 'kavehTheme'),
						'three' => esc_html__('Layout Three', 'kavehTheme'),
						'four' => esc_html__('Layout Four', 'kavehTheme'),
						'five' => esc_html__('Layout Five', 'kavehTheme'),
						'six' => esc_html__('Layout Six', 'kavehTheme'),
						'seven' => esc_html__('Layout Seven', 'kavehTheme'),
						'eight' => esc_html__('Layout Eight', 'kavehTheme'),
						'nine' => esc_html__('Layout Nine', 'kavehTheme'),
						'ten' => esc_html__('Layout Ten', 'kavehTheme'),
						'eleven' => esc_html__('Layout Eleven', 'kavehTheme'),
						'twelve' => esc_html__('Layout Twelve', 'kavehTheme'),
						'thirteen' => esc_html__('Layout Thirteen', 'kavehTheme'),
						'fourteen' => esc_html__('Layout Fourteen', 'kavehTheme'),
						'fifteen' => esc_html__('Layout Fifteen', 'kavehTheme'),
						'sixteen' => esc_html__('Layout Sixteen', 'kavehTheme'),
						'seventeen' => esc_html__('Layout Seventeen', 'kavehTheme'),
						'eighteen' => esc_html__('Layout Eighteen', 'kavehTheme'),
						'nineteen' => esc_html__('Layout Nineteen', 'kavehTheme'),
						'twenty' => esc_html__('Layout Twenty', 'kavehTheme'),
						'twenty-one' => esc_html__('Layout Twenty-One', 'kavehTheme'),
						'twenty-two' => esc_html__('Layout Twenty-Two', 'kavehTheme'),
						'twenty-three' => esc_html__('Layout Twenty-Three', 'kavehTheme'),
						'twenty-four' => esc_html__('Layout Twenty-Four', 'kavehTheme'),
						'twenty-five' => esc_html__('Layout Twenty-Five', 'kavehTheme'),
						'twenty-six' => esc_html__('Layout Twenty-Six', 'kavehTheme'),
						'twenty-seven' => esc_html__('Layout Twenty-Seven', 'kavehTheme'),
						'twenty-eight' => esc_html__('Layout Twenty-Eight', 'kavehTheme'),
        ],
      ]
    );
    $this->add_control(
      'kaveh_exclude_out_of_stock',
      [
        'label' => esc_html__('Hide Out of Stock Products', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'yes' => esc_html__('Yes', 'kavehTheme'),
				'no' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'yes',
      ]
    );
    $this->add_control(
      'pproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Show Products', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'all',
				'options' => [
					'all' => esc_html__('All', 'kavehTheme'),
					'cat' => esc_html__('Category', 'kavehTheme'),

        ],
      ]
    );
    $this->add_control(
      'psortproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Sort Products By', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'date',
				'options' => [
					'date' => esc_html__('Date', 'kavehTheme'),
					'_price' => esc_html__('Price', 'kavehTheme'),
					'_wc_average_rating' => esc_html__('Most Popular', 'kavehTheme'),
					'total_sales' => esc_html__('Sales Amount', 'kavehTheme'),

        ],
      ]
    );
    $this->add_control(
      'psortiproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Sort Type', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => 'desc',
				'options' => [
					'asc' => esc_html__('Ascending', 'kavehTheme'),
					'desc' => esc_html__('Descending', 'kavehTheme'),
        ],
      ]
    );
    $options2 = array();
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false;
    $cat_args = array(
      'orderby' => $orderby,
      'order' => $order,
      'hide_empty' => $hide_empty,
    );

    $product_categories = get_terms('product_cat', $cat_args);

    foreach ($product_categories as $key => $category) {
      $options2[$category->term_id] = $category->name;
    }
    $this->add_control(
      'catproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Select Category', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => $options2,
        'condition' => [
          'pproductscarouselseventhstkaveh' => 'cat',
        ],
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'content3productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Carousel Settings', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
      ]
    );

    // Navigation Controls
    $this->add_control(
      'showarrowproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Show Arrows', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'kavehTheme'),
				'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'yes',
      ]
    );

    $this->add_control(
      'showdotsproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Show Dots', 'kavehTheme'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'kavehTheme'),
				'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'no',
      ]
    );

    // Responsive Settings
    $this->add_control(
      'responsive_heading',
      [
        'label' => esc_html__('Responsive Settings', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
      ]
    );

    $this->add_control(
      'productnumberproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Number of Products Displayed', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 1,
        'max' => 50,
        'step' => 1,
        'default' => 5,
      ]
    );

    $this->add_control(
      'productcolumnproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Number of Product Columns on Desktop', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 1,
        'max' => 6,
        'step' => 1,
        'default' => 4,
      ]
    );

    $this->add_control(
      'productcolumn2productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Number of Product Columns on Tablet', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 1,
        'max' => 4,
        'step' => 1,
        'default' => 3,
      ]
    );

    $this->add_control(
      'productcolumn3productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Number of Product Columns on Mobile', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 1,
        'max' => 2,
        'step' => 1,
        'default' => 2,
      ]
    );

    // Animation Settings
    $this->add_control(
      'animation_heading',
      [
        'label' => esc_html__('Animation Settings', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::HEADING,
        'separator' => 'before',
      ]
    );

    $this->add_control(
      'autoplayproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Enable Autoplay', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => esc_html__('Yes', 'kavehTheme'),
        'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'no',
      ]
    );

    $this->add_control(
      'autoplaydelayproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Autoplay Delay (milliseconds)', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 1000,
        'max' => 10000,
        'step' => 500,
        'default' => 3000,
        'condition' => [
          'autoplayproductscarouselseventhstkaveh' => 'yes',
        ],
      ]
    );

    $this->add_control(
      'pauseonhoverproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Pause on Hover', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => esc_html__('Yes', 'kavehTheme'),
        'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'yes',
        'condition' => [
          'autoplayproductscarouselseventhstkaveh' => 'yes',
        ],
      ]
    );

    $this->add_control(
      'loopproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Loop', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => esc_html__('Yes', 'kavehTheme'),
        'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'yes',
      ]
    );

    $this->add_control(
      'speedproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Transition Speed (milliseconds)', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::NUMBER,
        'min' => 300,
        'max' => 3000,
        'step' => 100,
        'default' => 300,
      ]
    );

    $this->add_control(
      'centeredslidesproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Centered Slides', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => esc_html__('Yes', 'kavehTheme'),
        'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'no',
      ]
    );

    $this->add_control(
      'freemodeproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Free Mode', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SWITCHER,
        'label_on' => esc_html__('Yes', 'kavehTheme'),
        'label_off' => esc_html__('No', 'kavehTheme'),
        'return_value' => 'yes',
        'default' => 'no',
        'description' => esc_html__('Slides move freely', 'kavehTheme'),
      ]
    );

    $this->add_control(
      'effectproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Transition Effect', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::SELECT,
        'default' => 'slide',
        'options' => [
          'slide' => esc_html__('Slide', 'kavehTheme'),
          'fade' => esc_html__('Fade', 'kavehTheme'),
          'cube' => esc_html__('Cube', 'kavehTheme'),
          'coverflow' => esc_html__('Coverflow', 'kavehTheme'),
          'flip' => esc_html__('Flip', 'kavehTheme'),
        ],
      ]
    );

    $this->end_controls_section();
    $this->start_controls_section(
      'style_productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Title Styles', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'textcolor2productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Main Title Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .offer-books-heading .title' => 'color: {{VALUE}}',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'titleproductscarouselseventhstkaveh',
        'label' => esc_html__('Main Title Typography', 'kavehTheme'),
        'selector' => '{{WRAPPER}} .offer-books-heading .title',
      ]
    );
    $this->add_control(
      'textcolor22productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Subtitle Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .offer-books-heading .sub-title' => 'color: {{VALUE}}',
        ],
      ]
    );
    $this->add_group_control(
      \Elementor\Group_Control_Typography::get_type(),
      [
        'name' => 'title2productscarouselseventhstkaveh',
        'label' => esc_html__('Subtitle Typography', 'kavehTheme'),
        'selector' => '{{WRAPPER}} .offer-books-heading .sub-title',
      ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
      'style3_productscarouselseventhstkaveh',
      [
        'label' => esc_html__('Background Styles', 'kavehTheme'),
        'tab' => \Elementor\Controls_Manager::TAB_STYLE,
      ]
    );
    $this->add_control(
      'bgcolorproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Background Color', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::COLOR,
        'selectors' => [
          '{{WRAPPER}} .offer-books-wrapper::before' => 'background-color: {{VALUE}}',
        ],
      ]
    );
    $this->add_control(
      'boxradiusproductscarouselseventhstkaveh',
      [
        'label' => esc_html__('Box Border Radius', 'kavehTheme'),
        'type' => \Elementor\Controls_Manager::DIMENSIONS,
        'size_units' => ['px'],
        'selectors' => [
          '{{WRAPPER}} .offer-books-wrapper::before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
        ],
      ]
    );
    $this->end_controls_section();
  }


  protected function render()
  {
    $settings = $this->get_settings_for_display();
    $widget_id = $this->get_id();

    // Build swiper settings array
    $swiper_settings = array(
      'watchSlidesProgress' => true,
      'breakpoints' => array(
        300 => array(
          'slidesPerView' => (int) $settings['productcolumn3productscarouselseventhstkaveh'],
          'spaceBetween' => 12
        ),
        768 => array(
          'slidesPerView' => (int) $settings['productcolumn2productscarouselseventhstkaveh'],
          'spaceBetween' => 12
        ),
        992 => array(
          'slidesPerView' => (int) $settings['productcolumnproductscarouselseventhstkaveh'],
          'spaceBetween' => 16
        ),
        1200 => array(
          'slidesPerView' => (int) $settings['productcolumnproductscarouselseventhstkaveh'],
          'spaceBetween' => 24
        )
      )
    );

    // Add navigation if arrows are enabled
    if ($settings['showarrowproductscarouselseventhstkaveh'] === 'yes') {
      $swiper_settings['navigation'] = array(
        'nextEl' => '.swiper-button-next',
        'prevEl' => '.swiper-button-prev'
      );
    }

    // Add pagination if dots are enabled
    if ($settings['showdotsproductscarouselseventhstkaveh'] === 'yes') {
      $swiper_settings['pagination'] = array(
        'el' => '.swiper-pagination',
        'type' => 'bullets',
        'clickable' => true
      );
    }

    // Add loop if enabled
    if ($settings['loopproductscarouselseventhstkaveh'] === 'yes') {
      $swiper_settings['loop'] = true;
    }

    // Add speed if set
    if (!empty($settings['speedproductscarouselseventhstkaveh'])) {
      $swiper_settings['speed'] = (int) $settings['speedproductscarouselseventhstkaveh'];
    }

    // Add centered slides if enabled
    if ($settings['centeredslidesproductscarouselseventhstkaveh'] === 'yes') {
      $swiper_settings['centeredSlides'] = true;
    }

    // Add free mode if enabled
    if ($settings['freemodeproductscarouselseventhstkaveh'] === 'yes') {
      $swiper_settings['freeMode'] = true;
    }

    // Add effect if not default
    if ($settings['effectproductscarouselseventhstkaveh'] !== 'slide') {
      $swiper_settings['effect'] = $settings['effectproductscarouselseventhstkaveh'];
    }

    // Add autoplay if enabled
    if ($settings['autoplayproductscarouselseventhstkaveh'] === 'yes') {
      $autoplay_settings = array(
        'delay' => (int) $settings['autoplaydelayproductscarouselseventhstkaveh'],
        'disableOnInteraction' => false
      );

      if ($settings['pauseonhoverproductscarouselseventhstkaveh'] === 'yes') {
        $autoplay_settings['pauseOnMouseEnter'] = true;
      }

      $swiper_settings['autoplay'] = $autoplay_settings;
    }

    // Properly encode and escape JSON
    $swiper_json = wp_json_encode($swiper_settings, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    ?>

    <section class="offer-books">
      <div class="container">
        <div class="offer-books-wrapper position-relative">
          <!-- Start Heading -->
          <div class="offer-books-heading text-center position-relative">
            <<?php echo esc_attr($settings['tagheadingproductscarouselseventhstkaveh']); ?>
              class="title"><?php echo esc_html($settings['titleproductscarouselseventhstkaveh']); ?></<?php echo esc_attr($settings['tagheadingproductscarouselseventhstkaveh']); ?>>
            <<?php echo esc_attr($settings['tag2headingproductscarouselseventhstkaveh']); ?> class="sub-title
              mb-0"><?php echo esc_html($settings['title2productscarouselseventhstkaveh']); ?></<?php echo esc_attr($settings['tag2headingproductscarouselseventhstkaveh']); ?>>
          </div>
          <!-- End Heading -->
          <!-- Start Carousel -->
          <div class="swiper swiper-products-two swiper-visible" data-swiper='<?php echo esc_attr($swiper_json); ?>'>
            <div class="swiper-wrapper">
              <?php if ('cat' === $settings['pproductscarouselseventhstkaveh']) { ?>
                <?php
                // Define your custom query arguments
                $query_args = array(
                  'post_type' => array('product'),
                  'post_status' => array('publish'),
                  'posts_per_page' => $settings['productnumberproductscarouselseventhstkaveh'],
                  'orderby' => $settings['psortproductscarouselseventhstkaveh'] === 'date' ? 'date' : 'meta_value_num',
                  'order' => $settings['psortiproductscarouselseventhstkaveh'],
                  'tax_query' => array(
                    'relation' => 'AND',
                    array(
                      'taxonomy' => 'product_cat',
                      'field' => 'term_id',
                      'terms' => $settings['catproductscarouselseventhstkaveh'],
                    ),
                  ),
                );

                // Handle specific orderby values
                switch ($settings['psortproductscarouselseventhstkaveh']) {
                  case '_price':
                    $query_args['meta_key'] = '_price';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                  case '_wc_average_rating':
                    $query_args['meta_key'] = '_wc_average_rating';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                  case 'total_sales':
                    $query_args['meta_key'] = 'total_sales';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                }

                if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                  $query_args['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field' => 'name',
                    'terms' => array('outofstock'),
                    'operator' => 'NOT IN'
                  );
                }

                // Get the cached query results
                $cached_query = KavehCustomQuery::get_cached_query($query_args);
                if ($cached_query->have_posts()):
                  while ($cached_query->have_posts()):
                    $cached_query->the_post();
                    ?>
                    <div class="swiper-slide h-auto">
                      <?php $styleproduct2 = $settings['stylecartproductscarouselseventhstkaveh']; ?>
                      <?php get_template_part("template-part/product-cart-$styleproduct2"); ?>
                    </div>
                    <?php
                  endwhile;
                  wp_reset_postdata();
                else:
                  echo 'No posts found.';
                endif;
                ?>
              <?php } elseif ('all' === $settings['pproductscarouselseventhstkaveh']) { ?>
                <?php
                // Define your custom query arguments for all products
                $query_args = array(
                  'post_type' => array('product'),
                  'post_status' => array('publish'),
                  'posts_per_page' => $settings['productnumberproductscarouselseventhstkaveh'],
                  'orderby' => $settings['psortproductscarouselseventhstkaveh'] === 'date' ? 'date' : 'meta_value_num',
                  'order' => $settings['psortiproductscarouselseventhstkaveh'],
                  'tax_query' => array('relation' => 'AND'),
                );

                // Handle specific orderby values
                switch ($settings['psortproductscarouselseventhstkaveh']) {
                  case '_price':
                    $query_args['meta_key'] = '_price';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                  case '_wc_average_rating':
                    $query_args['meta_key'] = '_wc_average_rating';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                  case 'total_sales':
                    $query_args['meta_key'] = 'total_sales';
                    $query_args['orderby'] = 'meta_value_num';
                    break;
                }

                if ("yes" === $settings['kaveh_exclude_out_of_stock']) {
                  $query_args['tax_query'][] = array(
                    'taxonomy' => 'product_visibility',
                    'field' => 'name',
                    'terms' => array('outofstock'),
                    'operator' => 'NOT IN'
                  );
                }

                // Get the cached query results
                $cached_query = KavehCustomQuery::get_cached_query($query_args);
                if ($cached_query->have_posts()):
                  while ($cached_query->have_posts()):
                    $cached_query->the_post();
                    ?>
                    <div class="swiper-slide h-auto">
                      <?php $styleproduct2 = $settings['stylecartproductscarouselseventhstkaveh']; ?>
                      <?php get_template_part("template-part/product-cart-$styleproduct2"); ?>
                    </div>
                    <?php
                  endwhile;
                  wp_reset_postdata();
                else:
                  echo 'No posts found.';
                endif;
                ?>
              <?php } ?>
            </div>

            <?php if (isset($settings['showdotsproductscarouselseventhstkaveh']) && $settings['showdotsproductscarouselseventhstkaveh'] === 'yes') { ?>
              <div class="swiper-pagination"></div>
            <?php } ?>

            <?php
            if (isset($settings['showarrowproductscarouselseventhstkaveh']) && $settings['showarrowproductscarouselseventhstkaveh'] === 'yes') {
              ?>
              <div class="swiper-button-next"></div>
              <div class="swiper-button-prev"></div>
            <?php } ?>
          </div>
          <!-- End Carousel -->
        </div>
      </div>
    </section>

    <?php
  }


}
