<?php

namespace Elementor;


include get_template_directory() . '/elementor/' . 'contracts.php';


if (!defined('ABSPATH')) exit;


class ProductsSwiper3 extends Widget_Base
{

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

    public function get_title()
    {
        return __('Products Carousel 3', TEXT_DOMAIN);
    }

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

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

    public function get_keywords()
    {
        return ['card', 'box', 'list', 'carousel', 'slider', 'product','swiper'];
    }


    protected function _register_controls()
    {
        // content tab
        $this->register_content_tab_products_controls();
        $this->register_content_tab_query();
        $this->register_content_tab_carousel_controls();

        // style tab
        $this->register_style_tab_card_controls();
        $this->register_style_tab_image_controls();
        $this->register_style_tab_title_controls();
        $this->register_style_tab_price_controls();
        $this->register_style_tab_rating_controls();
        $this->register_style_tab_cart_button_controls();
        $this->register_style_tab_view_button_controls();
        $this->register_style_tab_carousel_controls();

    }


    // products content
    protected function register_content_tab_products_controls(){
        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__( 'Content', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        // rating
        $this->add_control(
            'show_title',
            [
                'label'   => esc_html__( 'Show title', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'title_tag',
            [
                'type' => Controls_Manager::SELECT,
                'label' => esc_html__( 'Title html tag', TEXT_DOMAIN ),
                'options' => HTML_TAGS,
                'default' => 'h3',
                'condition' => [
                    'show_title' => 'yes'
                ]
            ]
        );


        $this->add_control(
            'image_size',
            [
                'type' => Controls_Manager::SELECT,
                'label' => esc_html__( 'image size', TEXT_DOMAIN ),
                'options' => THUMB_SIZES,
                'default' => 'medium',
            ]
        );

        $this->add_control(
            'content_align',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__( 'Alignment', TEXT_DOMAIN ),
                'options' => [
                    'card-content-left' => [
                        'title' => esc_html__( 'left', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'card-content-center' => [
                        'title' => esc_html__( 'center', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'card-content-right' => [
                        'title' => esc_html__( 'right', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'card-content-right',
            ]
        );

        // quick look
        $this->add_control(
            'show_quick_view',
            [
                'label'   => esc_html__( 'Show quick view button', TEXT_DOMAIN),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
                'separator' => 'before',
            ]
        );

        // rating
        $this->add_control(
            'show_rating',
            [
                'label'   => esc_html__( 'show rating', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'no',
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'free_text',
            [
                'label' => esc_html__('Edit Product Free Label', TEXT_DOMAIN),
                'type' => Controls_Manager::TEXT,
                'placeholder' => __('Free', TEXT_DOMAIN),
                'default' => __('Free', TEXT_DOMAIN),
                'label_block' => true,
                'separator' => 'before'
            ]
        );
        
        $this->end_controls_section();
    }

    // query
    protected function register_content_tab_query(){
        $this->start_controls_section(
            'section_query',
            [
                'label' => esc_html__( 'Query', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'source',
            [
                'label'   => esc_html__( 'products source', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SELECT,
                'options' => [
                    ''        => esc_html__( 'current query', TEXT_DOMAIN ),
                    'by_name' => esc_html__( 'manual selection', TEXT_DOMAIN ),
                    'related' => esc_html__( 'related products', TEXT_DOMAIN ),
                ],
                'label_block' => true,
                'default' => 'by_name'
            ]
        );

        $product_categories = get_terms( 'product_cat' );

        $options = [];
        foreach ( $product_categories as $category ) {
            $options[ $category->slug ] = $category->name;
        }

        $this->add_control(
            'product_categories',
            [
                'label'       => esc_html__( 'categories', TEXT_DOMAIN ),
                'type'        => Controls_Manager::SELECT2,
                'options'     => $options,
                'default'     => [],
                'label_block' => true,
                'multiple'    => true,
                'condition'   => [
                    'source'    => 'by_name',
                ],
            ]
        );
        $this->add_control(
            'exclude_products',
            [
                'label'       => esc_html__( 'exclude products', TEXT_DOMAIN ),
                'type'        => Controls_Manager::TEXT,
                'placeholder'     => 'product_id',
                'label_block' => true,
                'description' => __( 'Write the product ID here, if you want to remove multiple products, use a comma as separator. like 1, 2', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'posts_per_page',
            [
                'label'   => esc_html__( 'number of products', TEXT_DOMAIN ),
                'type'    => Controls_Manager::NUMBER,
                'default' => 12,
                'condition'   => [
                    'source'    => ['by_name','related'],
                ],
            ]
        );
        $this->add_control(
            'show_product_type',
            [
                'label'   => esc_html__( 'products type', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'all',
                'options' => [
                    'all'      => esc_html__( 'any', TEXT_DOMAIN ),
                    'onsale'   => esc_html__( 'onsale', TEXT_DOMAIN ),
                    'featured' => esc_html__( 'featured', TEXT_DOMAIN ),
                ],
                'condition' => [
                    'source' => ['by_name','']
                ],
            ]
        );
        $this->add_control(
            'hide_free',
            [
                'label'   => esc_html__( 'hide free', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
            ]
        );
        $this->add_control(
            'hide_out_stock',
            [
                'label'   => esc_html__( 'hide out of stock', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
            ]
        );
        $this->add_control(
            'orderby',
            [
                'label'   => esc_html__( 'sort by', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'date',
                'options' => [
                    'date'  => esc_html__( 'date', TEXT_DOMAIN ),
                    'price' => esc_html__( 'price', TEXT_DOMAIN ),
                    'sales' => esc_html__( 'sales', TEXT_DOMAIN ),
                    'rand'  => esc_html__( 'random', TEXT_DOMAIN ),
                    'popularity'  => esc_html__( 'popularity', TEXT_DOMAIN ),
                    'rating'  => esc_html__( 'rating', TEXT_DOMAIN ),
                ],
            ]
        );
        $this->add_control(
            'order',
            [
                'label'   => esc_html__( 'order', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SELECT,
                'default' => 'DESC',
                'options' => [
                    'DESC' => esc_html__( 'Descending', TEXT_DOMAIN ),
                    'ASC'  => esc_html__( 'Ascending', TEXT_DOMAIN ),
                ],
            ]
        );

        $taxonomies = get_object_taxonomies('product', 'objects');
        unset($taxonomies['post_translations'],$taxonomies['post_format']);
        foreach ( $taxonomies as $object ) {
            if ($object !== 'post_translations' && $object !== 'post_format'){
                $taxonomy = is_string($object) ? $object : $object->name;
                $categories = get_terms( array(
                    'taxonomy'    => $taxonomy,
                    'hide_empty' => 0,
                ) );
                $cat_array  = array();
                foreach( $categories as $cat_id => $cat_name ) {
                    $cat_array[ $cat_name->term_id ] = $cat_name->name;
                }
                @$this->add_control(
                    $taxonomy,
                    [
                        'label' => $taxonomy,
                        'type' => Controls_Manager::SELECT2,
                        'label_block' => true,
                        'multiple' => true,
                        'options' => $cat_array,
                    ]
                );
            }
        }


        $this->end_controls_section();
    }

    // carousel
    protected function register_content_tab_carousel_controls()
    {
        $this->start_controls_section(
            'section_swiper',
            [
                'label' => esc_html__('Carousel', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        // items
        $this->add_control(
            'carousel_items_heading',
            [
                'label' => esc_html__('Slides per view', TEXT_DOMAIN),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',

            ]
        );
        $this->add_control(
            'carousel_items',
            [
                'label' => esc_html__('desktop', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => '4.5',
                'placeholder' => esc_html__('enter the number', TEXT_DOMAIN),

            ]
        );
        $this->add_control(
            'carousel_items_tablet',
            [
                'label' => esc_html__('tablet', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => '3.5',
                'placeholder' => esc_html__('enter the number', TEXT_DOMAIN),

            ]
        );
        $this->add_control(
            'carousel_items_mobile',
            [
                'label' => esc_html__('mobile', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => '1.5',
                'placeholder' => esc_html__('enter the number', TEXT_DOMAIN),

            ]
        );

        $this->add_control(
            'carousel_autoplay',
            [
                'label' => esc_html__('autoplay', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__('active', TEXT_DOMAIN),
                'label_off' => esc_html__('off', TEXT_DOMAIN),
                'return_value' => 'yes',
                'default' => 'no',
                'separator' => 'before',

            ]
        );

        $this->add_control(
            'carousel_autoplay_delay',
            [
                'label' => __('animation delay', TEXT_DOMAIN),
                'type' => Controls_Manager::NUMBER,
                'default' => 4000,
                'condition' => [
                    'carousel_autoplay' => 'yes'
                ]

            ]
        );

        $this->add_control(
            'carousel_smart_speed',
            [
                'label' => __('animation speed', TEXT_DOMAIN),
                'type' => Controls_Manager::NUMBER,
                'default' => 400,

            ]
        );

        $this->add_control(
            'carousel_center',
            [
                'label' => esc_html__('center mode', TEXT_DOMAIN),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('active', TEXT_DOMAIN),
                'label_off' => esc_html__('off', TEXT_DOMAIN),
                'return_value' => 'yes',
                'default' => 'no',

            ]
        );

        $this->add_control(
            'carousel_loop',
            [
                'label' => esc_html__('infinite loop', TEXT_DOMAIN),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('active', TEXT_DOMAIN),
                'label_off' => esc_html__('off', TEXT_DOMAIN),
                'return_value' => 'yes',
                'default' => 'no',
            ]
        );

        $this->add_control(
            'carousel_margin',
            [
                'label' => esc_html__('items margin', TEXT_DOMAIN),
                'type' => Controls_Manager::NUMBER,
                'default' => 12,
                'placeholder' => esc_html__('enter the value', TEXT_DOMAIN),

            ]
        );

        $this->add_control(
            'carousel_items_per_group',
            [
                'label' => esc_html__('Item to change', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::NUMBER,
                'default' => 1,
                'placeholder' => esc_html__('enter the number', TEXT_DOMAIN),

            ]
        );

        $this->end_controls_section();
    }



    // style box
    protected function register_style_tab_card_controls(){
        $this->start_controls_section(
            'section_box_style',
            [
                'label' => esc_html__( 'Box', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );



        $this->add_responsive_control(
            'box_padding',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->start_controls_tabs(
            'box_style_tabs',[
                'separator' => 'before'
            ]
        );

        // normal tab
        $this->start_controls_tab(
            'box_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );

        $this->add_responsive_control(
            'box_radius',
            [
                'label'      => __( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', 'rem' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_control(
            'box_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'box_border_type',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card',
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'box_shadow',
                'selector' => '{{WRAPPER}} .product-card',
            ]
        );
        $this->end_controls_tab();

        // hover tab
        $this->start_controls_tab(
            'box_style_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );
        $this->add_responsive_control(
            'box_radius_hover',
            [
                'label'      => __( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', 'rem' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'box_color_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'box_border_type_hover',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card:hover',
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'box_shadow_hover',
                'selector' => '{{WRAPPER}} .product-card:hover',
            ]
        );
        $this->end_controls_tab();

        $this->end_controls_tabs();
        $this->end_controls_section();
    }

    // style image
    protected function register_style_tab_image_controls(){
        $this->start_controls_section(
            'section_image_style',
            [
                'label' => esc_html__( 'Image', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );


        $this->add_responsive_control(
            'image_padding',
            [
                'label'      => esc_html__( 'margin', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', 'em', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-image' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'image_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-image img' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_responsive_control(
            'image_margin',
            [
                'label'      => __( 'margin', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-image img' => 'margin-bottom: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->end_controls_section();
    }

    // style title
    protected function register_style_tab_title_controls(){
        $this->start_controls_section(
            'section_title_style',
            [
                'label' => esc_html__( 'Title', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'title_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card .title',
            ]
        );
        $this->add_responsive_control(
            'title_margin',
            [
                'label'      => __( 'margin', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card .title' => 'margin-bottom: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_control(
            'title_transition',
            [
                'label'      => __( 'transition', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 's', 'ms'],
                'range'      => [
                    's' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                    'ms' => [
                        'min' => 1,
                        'max' => 1000,
                    ],
                ],
                'default' =>[
                    'unit' => 'ms',
                    'size' => 200
                ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card .title' => 'transition: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->start_controls_tabs(
            'title_style_tabs',[
                'separator' => 'before'
            ]
        );

        // normal tab
        $this->start_controls_tab(
            'title_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'title_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card .title' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Text_Shadow::get_type(),
            [
                'name'     => 'title_text_shadow',
                'selector' => '{{WRAPPER}} .product-card .title',
            ]
        );
        $this->end_controls_tab();

        // hover tab
        $this->start_controls_tab(
            'title_style_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'title_color_hover',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card .title:hover' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Text_Shadow::get_type(),
            [
                'name'     => 'title_text_shadow_hover',
                'selector' => '{{WRAPPER}} .product-card .title:hover',
            ]
        );
        $this->end_controls_tab();

        $this->end_controls_tabs();
        $this->end_controls_section();
    }

    // style price
    protected function register_style_tab_price_controls(){
        $this->start_controls_section(
            'section_price_style',
            [
                'label' => esc_html__( 'Price', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        // price
        $this->add_control(
            'price_heading',
            [
                'label' => esc_html__( 'price', TEXT_DOMAIN ),
                'type' => Controls_Manager::HEADING,
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'price_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card .current-price',
            ]
        );
        $this->add_control(
            'price_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card .current-price' => 'color: {{VALUE}}!important;',
                ],
            ]
        );


        // main price
        $this->add_control(
            'old_price_heading',
            [
                'label' => esc_html__( 'main price', TEXT_DOMAIN ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'old_price_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card .old-price',
            ]
        );
        $this->add_control(
            'old_price_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card .old-price' => 'color: {{VALUE}}!important;',
                ],
            ]
        );

        // symbol
        $this->add_control(
            'tooman_heading',
            [
                'label' => esc_html__( 'price symbol', TEXT_DOMAIN ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'price_tooman_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card .tooman',
            ]
        );
        $this->add_control(
            'price_tooman_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card .tooman' => 'color: {{VALUE}}!important;',
                ],
            ]
        );

        // percent
        $this->add_control(
            'percent_heading',
            [
                'label' => esc_html__( 'discount percent', TEXT_DOMAIN ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );
        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'percent_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .price-discount-percent',
            ]
        );
        $this->add_responsive_control(
            'percent_size',
            [
                'label' => __('size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .price-discount-percent' => 'width: {{SIZE}}{{UNIT}}!important;height: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_control(
            'percent_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .price-discount-percent' => 'color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'percent_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .price-discount-percent' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'percent_border_type',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .price-discount-percent',
            ]
        );


        $this->add_responsive_control(
            'percent_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .price-discount-percent' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );



        $this->end_controls_section();
    }

    // style rating
    protected function register_style_tab_rating_controls(){
        $this->start_controls_section(
            'section_rating_style',
            [
                'label' => esc_html__( 'Ratings', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_rating' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'rating_color',
            [
                'label'     => esc_html__( 'Color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#e7e7e7',
                'selectors' => [
                    '{{WRAPPER}} .product-card .star-rating:before' => 'color: {{VALUE}};',
                ]
            ]
        );
        $this->add_control(
            'active_rating_color',
            [
                'label'     => esc_html__( 'Active Color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'default'   => '#FFCC00',
                'selectors' => [
                    '{{WRAPPER}} .product-card .star-rating span::before' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'rating_size',
            [
                'label'      => __( 'size', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em' ],
                'range'      => [
                    'px' => [
                        'min' => 1,
                        'max' => 100,
                    ],
                    'em' => [
                        'min' => 1,
                        'max' => 10,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card .star-rating' => 'font-size: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_responsive_control(
            'rating_margin',
            [
                'label'      => __( 'margin', TEXT_DOMAIN ),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => [ 'px', 'em', 'rem' ],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 200,
                    ],
                ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card .star-rating' => 'margin-bottom: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->end_controls_section();
    }

    // style cart button
    protected function register_style_tab_cart_button_controls(){
        $this->start_controls_section(
            'section_cart_button_style',
            [
                'label' => esc_html__( 'Cart Button', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );


        $this->add_responsive_control(
            'cart_button_icon_size',
            [
                'label' => __('Icon size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-cart' => 'font-size: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );



        $this->start_controls_tabs(
            'cart_button_style_tabs',[
                'separator' => 'before'
            ]
        );

        // normal tab
        $this->start_controls_tab(
            'cart_button_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'cart_button_icon_color',
            [
                'label'     => esc_html__( 'Icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-cart' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'cart_button_bg',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-cart' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'cart_button_border_type',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card-3-cart',
            ]
        );
        $this->add_responsive_control(
            'cart_button_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-3-cart' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'cart_button_shadow',
                'selector' => '{{WRAPPER}} .product-card-3-cart',
            ]
        );
        $this->end_controls_tab();

        // hover tab
        $this->start_controls_tab(
            'cart_button_style_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'cart_button_icon_color_hover',
            [
                'label'     => esc_html__( 'Icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-cart:hover' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'cart_button_bg_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-cart:hover' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'cart_button_border_type_hover',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card-3-cart:hover',
            ]
        );
        $this->add_responsive_control(
            'ocart_button_border_radius_hover',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-3-cart:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'cart_button_shadow_hover',
                'selector' => '{{WRAPPER}} .product-card-3-cart:hover',
            ]
        );
        $this->end_controls_tab();
        $this->end_controls_tabs();


        $this->end_controls_section();
    }

    // style quick view button
    protected function register_style_tab_view_button_controls(){
        $this->start_controls_section(
            'section_view_button_style',
            [
                'label' => esc_html__( 'Quick View Button', TEXT_DOMAIN ),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'show_quick_view' => 'yes'
                ]
            ]
        );


        $this->add_responsive_control(
            'view_button_icon_size',
            [
                'label' => __('Icon size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view' => 'font-size: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'view_button_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card-3-view .card-3-btn-title',
            ]
        );


        $this->start_controls_tabs(
            'view_button_style_tabs',[
                'separator' => 'before'
            ]
        );

        // normal tab
        $this->start_controls_tab(
            'view_button_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'view_button_icon_color',
            [
                'label'     => esc_html__( 'Icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'view_button_text_color',
            [
                'label'     => esc_html__( 'Text color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view .card-3-btn-title' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'view_button_bg',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'view_button_border_type',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card-3-view',
            ]
        );
        $this->add_responsive_control(
            'view_button_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-3-view' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'view_button_shadow',
                'selector' => '{{WRAPPER}} .product-card-3-view',
            ]
        );
        $this->end_controls_tab();

        // hover tab
        $this->start_controls_tab(
            'view_button_style_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'view_button_icon_color_hover',
            [
                'label'     => esc_html__( 'Icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view:hover' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'view_button_text_color_hover',
            [
                'label'     => esc_html__( 'Text color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view:hover .card-3-btn-title' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'view_button_bg_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .product-card-3-view:hover' => 'background-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name'     => 'view_button_border_type_hover',
                'label'    => esc_html__( 'border', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .product-card-3-view:hover',
            ]
        );
        $this->add_responsive_control(
            'view_button_border_radius_hover',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .product-card-3-view:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'view_button_shadow_hover',
                'selector' => '{{WRAPPER}} .product-card-3-view:hover',
            ]
        );
        $this->end_controls_tab();
        $this->end_controls_tabs();


        $this->end_controls_section();
    }
    
    // style carousel
    protected function register_style_tab_carousel_controls()
    {
        $this->start_controls_section(
            'section_carousel_style',
            [
                'label' => esc_html__('Carousel', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_STYLE,

            ]
        );

        // buttons backdrop
        $this->add_control(
            'carousel_arrows_backdrop_style_heading',
            [
                'label' => esc_html__('Arrows backdrop', TEXT_DOMAIN),
                'type' => Controls_Manager::HEADING,
            ]
        );

        $this->add_control(
            'carousel_arrows_backdrop_color',
            [
                'label' => esc_html__('color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .swiper-backdrop-button.prev' => 'background: linear-gradient(to left, transparent,{{VALUE}});',
                    '{{WRAPPER}} .swiper-backdrop-button.next' => 'background: linear-gradient(to right, transparent,{{VALUE}});',
                ],
            ]
        );




        // nav
        $this->add_control(
            'carousel_nav_style_heading',
            [
                'label' => esc_html__('arrows', TEXT_DOMAIN),
                'type' => Controls_Manager::HEADING,
            ]
        );
        $this->add_responsive_control(
            'carousel_nav_width',
            [
                'label' => __('width', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev,{{WRAPPER}} .custom-swiper-button-next' => 'width: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_responsive_control(
            'carousel_nav_height',
            [
                'label' => __('height', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev,{{WRAPPER}} .custom-swiper-button-next' => 'height: {{SIZE}}{{UNIT}}!important;line-height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'carousel_nav_offset',
            [
                'label' => __('margin', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => -100,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-next' => 'left: {{SIZE}}{{UNIT}}!important;',
                    '{{WRAPPER}} .custom-swiper-button-prev' => 'right: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_responsive_control(
            'carousel_arrow_size',
            [
                'label' => __('icon size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-next,{{WRAPPER}} .custom-swiper-button-prev' => 'font-size: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );



        $this->start_controls_tabs(
            'carousel_nav_style_tabs', [
                'separator' => 'before'
            ]
        );

        // nav normal tab
        $this->start_controls_tab(
            'carousel_nav_style_normal_tab',
            [
                'label' => esc_html__('normal', TEXT_DOMAIN),
            ]
        );
        $this->add_control(
            'carousel_nav_icon_color',
            [
                'label' => esc_html__('Icon color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-next,{{WRAPPER}} .custom-swiper-button-prev' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'carousel_nav_bg_color',
            [
                'label' => esc_html__('background color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev,{{WRAPPER}} .custom-swiper-button-next' => 'background-color: {{VALUE}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'carousel_nav_border_type',
                'label' => esc_html__('border', TEXT_DOMAIN),
                'selector' => '{{WRAPPER}} .custom-swiper-button-prev.swiper-button-primary,{{WRAPPER}} .custom-swiper-button-next.swiper-button-primary',
            ]
        );
        $this->add_responsive_control(
            'carousel_nav_border_radius',
            [
                'label' => esc_html__('border radius', TEXT_DOMAIN),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev,{{WRAPPER}} .custom-swiper-button-next' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'carousel_nav_shadow',
                'selector' => '{{WRAPPER}} .custom-swiper-button-prev,{{WRAPPER}} .custom-swiper-button-next',
            ]
        );
        $this->end_controls_tab();

        // nav hover tab
        $this->start_controls_tab(
            'carousel_nav_style_hover_tab',
            [
                'label' => esc_html__('hover', TEXT_DOMAIN),
            ]
        );
        $this->add_control(
            'carousel_nav_icon_color_hover',
            [
                'label' => esc_html__('Icon color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev:hover,{{WRAPPER}} .custom-swiper-button-next:hover' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'carousel_nav_bg_color_hover',
            [
                'label' => esc_html__('background color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev:hover,{{WRAPPER}} .custom-swiper-button-next:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'carousel_nav_border_type_hover',
                'label' => esc_html__('border', TEXT_DOMAIN),
                'selector' => '{{WRAPPER}} .custom-swiper-button-prev.swiper-button-primary:hover,{{WRAPPER}} .custom-swiper-button-next.swiper-button-primary:hover',
            ]
        );
        $this->add_responsive_control(
            'carousel_nav_border_radius_hover',
            [
                'label' => esc_html__('border radius', TEXT_DOMAIN),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .custom-swiper-button-prev:hover,{{WRAPPER}} .custom-swiper-button-next:hover' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'carousel_nav_shadow_hover',
                'selector' => '{{WRAPPER}} .custom-swiper-button-prev:hover,{{WRAPPER}} .custom-swiper-button-next:hover',
            ]
        );
        $this->end_controls_tab();
        $this->end_controls_tabs();


        $this->end_controls_tabs();
        $this->end_controls_section();
    }
    



    protected function renderQuery($settings): array
    {
        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
        elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
        else { $paged = 1; }

        $exclude_products = ($settings['exclude_products']) ? explode(',', $settings['exclude_products']) : [];


        $query_args = array(
            'post_type'           => 'product',
            'post_status'         => 'publish',
            'meta_query'          => [],
            'tax_query'           => [ 'relation' => 'AND' ],
            'paged'               => $paged,
            'order'               => $settings['order'],
            'post__not_in'        => $exclude_products
        );

        $taxonomies = get_object_taxonomies('product', 'objects');
        foreach ( $taxonomies as $object ) {
            if (! empty($settings[$object->name])) {
                $query_args['tax_query'] = array(
                    array(
                        'taxonomy' => $object->name,
                        'terms' => $settings[$object->name],
                    )
                );
            }
        }

        if ($settings['source'] == 'by_name' || $settings['source'] == 'related'){
            $query_args['posts_per_page'] = $settings['posts_per_page'];
        }

        $product_visibility_term_ids = wc_get_product_visibility_term_ids();

        // order
        switch ( $settings['orderby'] ) {
            case 'price':
                $query_args['meta_key'] = '_price'; // WPCS: slow query ok.
                $query_args['orderby']  = 'meta_value_num';
                break;
            case 'rand':
                $query_args['orderby'] = 'rand';
                break;
            case 'sales':
                $query_args['meta_key'] = 'total_sales'; // WPCS: slow query ok.
                $query_args['orderby']  = 'meta_value_num';
                break;
            default:
                $query_args['orderby'] = 'date';
        }

        if ($settings['source'] == 'related'){
            // get related products
            global $product;
            $productID = $product->id;
            $items = wc_get_related_products($productID, $settings['posts_per_page'], array() );
            $query_args['post__in'] = $items;
        }else{

            switch ( $settings['show_product_type'] ) {
                case 'featured':
                    $query_args['tax_query'][] = array(
                        'taxonomy' => 'product_visibility',
                        'field'    => 'term_taxonomy_id',
                        'terms'    => $product_visibility_term_ids['featured'],
                    );
                    break;
                case 'onsale':
                    $product_ids_on_sale    = wc_get_product_ids_on_sale();
                    $product_ids_on_sale[]  = 0;
                    $query_args['post__in'] = $product_ids_on_sale;
                    break;
            }

        }

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

        if ( 'yes' == $settings['hide_free'] ) {
            $query_args['meta_query'][] = array(
                'key'     => '_price',
                'value'   => 0,
                'compare' => '>',
                'type'    => 'DECIMAL',
            );
        }

        if ( 'by_name' === $settings['source'] and !empty($settings['product_categories']) ) {
            $query_args['tax_query'][] = array(
                'taxonomy'           => 'product_cat',
                'field'              => 'slug',
                'terms'              => $settings['product_categories'],
                'post__not_in'       => $exclude_products,
            );
        }



        if ($settings['source'] == ''){
            global $wp_query;
            $query_args = array_merge( $wp_query->query_vars, $query_args );
        }

        return $query_args;
    }

    public function calculatePricePercentage($product){
        if( $product->is_type('variable')){
            $percentages = array();

            // Get all variation prices
            $prices = $product->get_variation_prices();

            // Loop through variation prices
            foreach( $prices['price'] as $key => $price ){
                // Only on sale variations
                if( $prices['regular_price'][$key] !== $prices ){
                    // Calculate and set in the array the percentage for each variation on sale
                    $percentages[] = round( 100 - ( floatval($prices['sale_price'][$key]) / floatval($prices['regular_price'][$key]) * 100 ) );
                }
            }
            // We keep the highest value
            if (count($percentages) < 1) {
                $percentage = '0%';
            }else{
                $percentage = max($percentages) . '%';
            }

        } elseif( $product->is_type('grouped') ){
            $percentages = array();

            // Get all variation prices
            $children_ids = $product->get_children();

            // Loop through variation prices
            foreach( $children_ids as $child_id ){
                $child_product = wc_get_product($child_id);

                $regular_price = (float) $child_product->get_regular_price();
                $sale_price    = (float) $child_product->get_sale_price();

                if ( $sale_price != 0 || ! empty($sale_price) ) {
                    // Calculate and set in the array the percentage for each child on sale
                    $percentages[] = round(100 - ($sale_price / $regular_price * 100));
                }
            }
            // We keep the highest value
            if (count($percentages) < 1) {
                $percentage = '0%';
            }else{
                $percentage = max($percentages) . '%';
            }

        } else {
            $regular_price = (float) $product->get_regular_price();
            $sale_price    = (float) $product->get_sale_price();

            if ( $sale_price != 0 || ! empty($sale_price) ) {
                $percentage  = round(100 - ($sale_price / $regular_price * 100)) . '%';
            } else {
                $percentage = 0;
            }
        }

        return $percentage;
    }

    public function bcp_wc_get_rating_html( $rating, $count = 0 ) {
        $html = '';
        $label = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
        $html  = '<div class="woocommerce"><div class="star-rating" role="img" aria-label="' . esc_attr( $label ) . '">' . wc_get_star_rating_html( $rating, $count ) . '</div></div>';
        return apply_filters( 'woocommerce_product_get_rating_html', $html, $rating, $count );
    }

    protected function is_product_in_stock($product)
    {
        if (!$product) {
            return false;
        }

        $product_type = $product->get_type();

        switch ($product_type) {
            case 'simple':
            case 'external':
                return $product->is_in_stock();

            case 'variable':
                $variations = $product->get_available_variations();
                if (empty($variations)) {
                    return false;
                }
                foreach ($variations as $variation_data) {
                    $variation = wc_get_product($variation_data['variation_id']);
                    if ($variation && $variation->is_in_stock()) {
                        return true;
                    }
                }
                return false;

            case 'grouped':
                $children = $product->get_children();
                if (empty($children)) {
                    return false;
                }
                foreach ($children as $child_id) {
                    $child_product = wc_get_product($child_id);

                    if ($child_product && is_product_in_stock($child_product)) {
                        return true;
                    }
                }
                return false;

            default:
                // برای سایر انواع محصولات
                return $product->is_in_stock();
        }
    }


    public function render_price($product,$freePriceText){
        if ($this->is_product_in_stock($product)){
            if( $product->is_type('variable')){
                $prices = $product->get_variation_prices();
                if (!empty($prices['regular_price'])) {
                    $regular_price = (float) min($prices['regular_price']);
                } else {
                    $regular_price = 0;
                }

                if (!empty($prices['sale_price'])) {
                    $sale_price = (float) min($prices['sale_price']);
                } else {
                    $sale_price = 0;
                }

            }elseif( $product->is_type('grouped') ){
                $children_ids = $product->get_children();
                $child_product = wc_get_product($children_ids[0]);
                $regular_price = (float) $child_product->get_regular_price();
                $sale_price    = (float) $child_product->get_sale_price();

            }else{
                $regular_price = (float) $product->get_regular_price();
                $sale_price    = (float) $product->get_sale_price();
            }

            echo "<div class='product-price-container'>";

            $percentage = $this->calculatePricePercentage($product);
            if (intval(str_replace('%','',$percentage)) > 0 && !$product->is_type('variable')):
                echo "<div class='price-discount-percent'>".$percentage."</div>";
            endif;

            echo "<div class='product-price'>";
            if ($regular_price == 0){
                if (!empty($freePriceText)) {
                    echo '<span class="current-price">' . $freePriceText . '</span>';
                }
            }else{
                if ($regular_price == $sale_price){
                    echo "<div class='d-flex flex-column'><span class='current-price'>".number_format($regular_price)."</span><span class='tooman'>".get_woocommerce_currency_symbol()."</span></div>";
                }else{
                    if($sale_price == 0 ||  empty($sale_price) ){
                        echo "<div class='d-flex flex-column'><span class='current-price'>".number_format($regular_price)."</span><span class='tooman'>".get_woocommerce_currency_symbol()."</span></div>";

                    }else{
                        echo '<div class="current-price">'.number_format($sale_price).'</div>';
                        echo "<div class='d-flex'><span class='old-price'>".number_format($regular_price)."</span><span class='tooman ms-2'>".get_woocommerce_currency_symbol()."</span></div>";

                    }
                }
            }
            echo "</div></div>";
        }else{
            echo "<div class='product-price-container'><div class='out-of-stock-text'>".__('out of stock',TEXT_DOMAIN)."</div></div>";
        }
    }
    

    public function get_attribute_json($product){

        $attributes = $product->get_attributes();

        if ( ! $attributes ) {
            return;
        }

        $display_result = '';

        $attrCount = count($attributes);

        foreach ( $attributes as $attribute ) {

            if ( $attribute->get_variation() ) {
                continue;
            }

            $name = $attribute->get_name();

            if ( $attribute->is_taxonomy() ) {

                $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );

                $tax = $terms[0]->taxonomy;

                $object_taxonomy = get_taxonomy($tax);

                if ( isset ($object_taxonomy->labels->singular_name) ) {

                    $tax_label = $object_taxonomy->labels->singular_name;

                } elseif ( isset( $object_taxonomy->label ) ) {

                    $tax_label = $object_taxonomy->label;

                    if ( 0 === strpos( $tax_label, 'Product ' ) ) {

                        $tax_label = substr( $tax_label, 8 );

                    }

                }

                $display_result .= '<li>' . $tax_label . ': ';

                $tax_terms = array();

                foreach ( $terms as $term ) {

                    $single_term = esc_html( $term->name );

                    array_push( $tax_terms, $single_term );

                }

                $display_result .=  implode(', ', $tax_terms) .  '</li>';


            } else {

                $display_result .= '<li>' .$name . ': ';

                $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) .  '</li>';

            }

        }

        $result = '<ul class="bcp-attributes-list">'. $display_result .'</ul>';

        if ($attrCount > 3){
            $result .= '<span class="bcp-see-more-attr"><i class="bcp-icon-plus"></i><span>'. _e('See More',TEXT_DOMAIN).'</span></span>';
        }

        echo json_encode($result);
    }

    protected function render()
    {

        $settings = $this->get_settings_for_display();
        $args = $this->renderQuery($settings);

        // swiper
        $itemsPerGroup = $settings['carousel_items_per_group'];
        $items = $settings['carousel_items'];
        $itemsTablet = $settings['carousel_items_tablet'];
        $itemsMobile = $settings['carousel_items_mobile'];
        $autoplay = $settings['carousel_autoplay'];
        $center = $settings['carousel_center'];
        $loop = $settings['carousel_loop'];
        $margin = $settings['carousel_margin'];
        $smartSpeed = $settings['carousel_smart_speed'];
        $autoplayDelay = $settings['carousel_autoplay_delay'];

        // products
        $titleTag = $settings['title_tag'];
        $contentAlign = $settings['content_align'];
        $showQuickViewBtn = $settings['show_quick_view'] == 'yes';
        $showRating = $settings['show_rating'] == 'yes';
        $showTitle = $settings['show_title'] == 'yes';
        $freePriceText = $settings['free_text'];


        echo "<div class='swiper swiper-before-init products-swiper elementor-swiper buttons-backdrop-desktop' swiper-items-per-group='".$itemsPerGroup."' swiper-items='" . $items . "' swiper-items-tablet='" . $itemsTablet . "' swiper-items-mobile='" . $itemsMobile . "' swiper-autoplay='" . $autoplay . "' swiper-speed='" . $smartSpeed . "' swiper-autoplay-delay='" . $autoplayDelay . "' swiper-center='" . $center . "' swiper-loop='" . $loop . "' swiper-margin='" . $margin . "'>";


        // main swiper wrapper
        echo "<div class='swiper-wrapper'>";
        $query = new \WP_Query(apply_filters( 'woocommerce_products_widget_query_args', $args ));
        while ($query->have_posts()): $query->the_post();
            $product = wc_get_product($query->post->ID);?>
            <div class="swiper-slide">
                <div class="product-card product-card-3 <?php echo ($this->is_product_in_stock($product)) ? '' : 'product-card-unavailable'?>">
                    <div class="product-card-image">
                        <!-- thumbnail -->
                        <a href="<?php the_permalink() ?>">
                            <?php if (has_post_thumbnail()){
                                the_post_thumbnail($settings['image_size']);
                            }else{?>
                                <img src="<?php echo esc_url(get_template_directory_uri() . '/images/default.jpg'); ?>" alt="<?php the_title();?>">
                            <?php }?>
                        </a>
                    </div>
                    <div class="product-card-content <?php echo $contentAlign?>">
                        <!-- title -->
                        <?php if($showTitle):?>
                            <a href="<?php the_permalink() ?>">
                                <?php echo "<$titleTag class='title'>";?>
                                <?php the_title(); ?>
                                <?php echo "</$titleTag>";?>
                            </a>
                        <?php endif;?>

                        <!-- rating -->
                        <?php if($showRating): echo $this->bcp_wc_get_rating_html($product->get_average_rating()); endif;?>

                        <!-- price -->
                        <?php $this->render_price($product,$freePriceText);?>

                        <div class="product-card-3-buttons">
                            <!-- add to cart -->
                            <?php if($this->is_product_in_stock($product) && floatval($product->get_price()) != 0):?>
                                <?php if($product->is_type('variable')):?>
                                    <a href="<?php the_permalink() ?>" class="product-card-3-cart">
                                        <span class="product-card-3-cart-icon"><i class="iconly-Buy"></i></span>
                                        <span class="card-3-btn-title"><?php _e('Add to shopping',TEXT_DOMAIN);?></span>
                                    </a>
                                <?php else:?>
                                    <span class="product-card-3-cart ajax-add-to-cart" data-product-id="<?php echo esc_attr($product->get_id()); ?>">
                                    <span class="product-card-3-cart-icon"><i class="iconly-Buy"></i></span>
                                    <span class="card-3-btn-title"><?php _e('Add to shopping',TEXT_DOMAIN);?></span>
                                </span>
                                <?php endif;?>
                            <?php endif;?>

                            <!-- quick view -->
                            <?php if($showQuickViewBtn):
                                $excerpt = mb_substr(get_the_excerpt(),0,200);
                                $images = [];
                                $featuredImg = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'single-post-thumbnail' );
                                if ($featuredImg){
                                    array_push($images,$featuredImg[0]);
                                }
                                $attachment_ids = $product->get_gallery_image_ids();
                                foreach( $attachment_ids as $attachment_id ) {
                                    $image_link = wp_get_attachment_url( $attachment_id );
                                    array_push($images,$image_link);
                                }
                                ?>
                                <span class="product-card-3-view show-bcp-modal quick-look-button"
                                      data-bcp-id='<?php echo 'modal-' . $product->get_id(); ?>'
                                      data-bcp-link='<?php the_permalink() ?>' data-bcp-title='<?php the_title(); ?>' data-bcp-description='<?php echo esc_attr( wp_strip_all_tags( $excerpt ) ); ?>'
                                      data-bcp-images='<?php echo json_encode($images);?>'
                                      data-bcp-price='<?php echo $this->is_product_in_stock($product) ? json_encode($product->get_price_html()) : '';?>'
                                      data-bcp-attributes='<?php $this->get_attribute_json($product)?>'
                                      data-bcp-rating='<?php woocommerce_template_loop_rating();?>'
                                      data-bcp-stock='<?php echo $product->get_stock_status()?>'>
                                    <i class="iconly-Show"></i><span class="card-3-btn-title"><?php _e('Quick view',TEXT_DOMAIN);?></span>
                                </span>
                            <?php endif;?>

                        </div>

                    </div>
                </div>
            </div>
        <?php endwhile; ?>
        <?php wp_reset_postdata();
        echo "</div>";

        // button navigations
        echo "<div class='swiper-backdrop-button prev'></div>";
        echo "<div class='swiper-backdrop-button next'></div>";
        echo "<div class='custom-swiper-button-prev swiper-button-primary'><i class='iconly-Arrow-Right'></i></div>";
        echo "<div class='custom-swiper-button-next swiper-button-primary'><i class='iconly-Arrow-Left'></i></div>";

        echo "<div class='swiper-spinner'></div>";
        echo "</div>";
    }


}

Plugin::instance()->widgets_manager->register_widget_type(new ProductsSwiper3());