PHP 8.0.30
Preview: product-carousel.php Size: 16.13 KB
/home/certprox/test.certproxywizard.com/wp-content/plugins/lvmr-addons/widgets/product-carousel.php

<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class LVMR_Product_Carousel_Widget extends \Elementor\Widget_Base {

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

	public function get_title() {
		return esc_html__( 'Product Carousel', 'lvmr-addons' );
	}

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

	public function get_categories() {
		return [ 'lvmr-addons' ];
	}

	public function get_script_depends() {
		return [];
	}

    public function get_style_depends() {
		return [ 'lvmr-product-carousel' ];
	}

	protected function register_controls() {

		$this->start_controls_section(
			'content_section',
			[
				'label' => esc_html__( 'Content', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'title',
			[
				'label' => esc_html__( 'Primary Title', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'default' => esc_html__( 'Featured', 'lvmr-addons' ),
				'placeholder' => esc_html__( 'First part of title', 'lvmr-addons' ),
			]
		);

		$this->add_control(
			'title_secondary',
			[
				'label' => esc_html__( 'Secondary Title', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::TEXT,
				'default' => esc_html__( 'products', 'lvmr-addons' ),
				'placeholder' => esc_html__( 'Second colored part', 'lvmr-addons' ),
				'separator' => 'after',
			]
		);

        // Get WooCommerce Categories
        $categories = get_terms( [
            'taxonomy'   => 'product_cat',
            'hide_empty' => true,
        ] );

        $cat_options = [ '' => esc_html__( 'All Categories', 'lvmr-addons' ) ];
        if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) {
            foreach ( $categories as $cat ) {
                $cat_options[ $cat->term_id ] = $cat->name;
            }
        }

		$this->add_control(
			'category',
			[
				'label' => esc_html__( 'Category', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'options' => $cat_options,
                'default' => '',
			]
		);

        $this->add_control(
			'posts_per_page',
			[
				'label' => esc_html__( 'Number of Products', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::NUMBER,
				'min' => 1,
				'max' => 20,
				'step' => 1,
				'default' => 6,
			]
		);



		$this->add_responsive_control(
			'slides_per_view',
			[
				'label' => esc_html__( 'Columns', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => '5',
				'tablet_default' => '3',
				'mobile_default' => '2',
				'options' => [
					'' => esc_html__( 'Default', 'lvmr-addons' ),
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
					'5' => '5',
					'6' => '6',
				],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-container' => '--lvmr-pc-cols: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		// STYLE TAB

		// 0. Section Title Style
		$this->start_controls_section(
			'section_style_widget_title',
			[
				'label' => esc_html__( 'Widget Title', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'widget_title_typography',
				'selector' => '{{WRAPPER}} .lvmr-pc-title',
			]
		);
		$this->add_control(
			'widget_title_color_primary',
			[
				'label' => esc_html__( 'Primary Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-title-primary' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'widget_title_color_secondary',
			[
				'label' => esc_html__( 'Secondary Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-title-secondary' => 'color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();

		// 0.1 Card Style
		$this->start_controls_section(
			'section_style_card',
			[
				'label' => esc_html__( 'Card', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		
		$this->add_responsive_control(
			'card_padding',
			[
				'label' => esc_html__( 'Padding', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		
		$this->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			[
				'name' => 'card_border',
				'selector' => '{{WRAPPER}} .lvmr-pc-item',
			]
		);

		$this->add_responsive_control(
			'card_border_radius',
			[
				'label' => esc_html__( 'Border Radius', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		
		$this->add_control(
			'card_bg_color',
			[
				'label' => esc_html__( 'Background Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_image',
			[
				'label' => esc_html__( 'Image', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_responsive_control(
			'image_width',
			[
				'label' => esc_html__( 'Image Width', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range' => [
					'px' => [ 'min' => 50, 'max' => 1500 ],
				],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-image img' => 'width: {{SIZE}}{{UNIT}};',
				],
			]
		);
		$this->add_responsive_control(
			'image_height',
			[
				'label' => esc_html__( 'Image Height', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'vh' ],
				'range' => [
					'px' => [ 'min' => 50, 'max' => 1500 ],
				],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-image' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();

		// 2. Title Style
		$this->start_controls_section(
			'section_style_title',
			[
				'label' => esc_html__( 'Title', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'title_color',
			[
				'label' => esc_html__( 'Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-title a' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'title_hover_color',
			[
				'label' => esc_html__( 'Hover Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-title a:hover' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'title_typography',
				'selector' => '{{WRAPPER}} .lvmr-pc-item-title a',
			]
		);
		$this->end_controls_section();

		// 3. Price Style
		$this->start_controls_section(
			'section_style_price',
			[
				'label' => esc_html__( 'Price', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'price_color',
			[
				'label' => esc_html__( 'Price Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-price' => 'color: {{VALUE}};',
					'{{WRAPPER}} .lvmr-pc-item-price ins' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'price_typography',
				'selector' => '{{WRAPPER}} .lvmr-pc-item-price, {{WRAPPER}} .lvmr-pc-item-price ins',
			]
		);
		$this->end_controls_section();

		// 4. Button Style
		$this->start_controls_section(
			'section_style_button',
			[
				'label' => esc_html__( 'Add to Cart Button', 'lvmr-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'button_icon',
			[
				'label' => esc_html__( 'Button Icon', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::ICONS,
			]
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'button_typography',
				'selector' => '{{WRAPPER}} .lvmr-pc-item-addcart .button',
			]
		);
		$this->start_controls_tabs( 'tabs_button_style' );
		$this->start_controls_tab(
			'tab_button_normal',
			[ 'label' => esc_html__( 'Normal', 'lvmr-addons' ) ]
		);
		$this->add_control(
			'button_text_color',
			[
				'label' => esc_html__( 'Text Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'button_bg_color',
			[
				'label' => esc_html__( 'Background Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_tab();
		$this->start_controls_tab(
			'tab_button_hover',
			[ 'label' => esc_html__( 'Hover', 'lvmr-addons' ) ]
		);
		$this->add_control(
			'button_hover_color',
			[
				'label' => esc_html__( 'Text Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button:hover' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'button_hover_bg_color',
			[
				'label' => esc_html__( 'Background Color', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button:hover' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_responsive_control(
			'button_border_radius',
			[
				'label' => esc_html__( 'Border Radius', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'separator' => 'before',
			]
		);
		$this->add_responsive_control(
			'button_padding',
			[
				'label' => esc_html__( 'Padding', 'lvmr-addons' ),
				'type' => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .lvmr-pc-item-addcart .button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();
	}

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

        $args = [
            'post_type'      => 'product',
            'posts_per_page' => $settings['posts_per_page'] ?? 6,
            'post_status'    => 'publish',
            'tax_query'      => [],
        ];

        // Filter by category if selected
        if ( ! empty( $settings['category'] ) ) {
            $args['tax_query'][] = [
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => $settings['category'],
            ];
        }

        // Only show visible products (this replaces the loop visibility check for better performance and layout stability)
        $args['tax_query'][] = [
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'exclude-from-catalog',
            'operator' => 'NOT IN',
        ];

        $query = new \WP_Query( $args );

        if ( ! $query->have_posts() ) {
            if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
                echo '<div class="elementor-alert elementor-alert-warning">' . esc_html__( 'No products found matching your criteria.', 'lvmr-addons' ) . '</div>';
            }
            return;
        }

        // Generating a unique ID for the swiper instance
        $carousel_id = 'lvmr-pc-' . $this->get_id();
        $total_posts = $query->post_count;

		?>
		<div class="lvmr-product-carousel-wrapper">
            <div class="lvmr-pc-header">
                <?php if ( ! empty( $settings['title'] ) || ! empty( $settings['title_secondary'] ) ) : ?>
                    <h3 class="lvmr-pc-title">
                        <?php if ( ! empty( $settings['title'] ) ) : ?>
                            <span class="lvmr-pc-title-primary"><?php echo esc_html( $settings['title'] ); ?></span>
                        <?php endif; ?>
                        <?php if ( ! empty( $settings['title_secondary'] ) ) : ?>
                            <span class="lvmr-pc-title-secondary"><?php echo esc_html( $settings['title_secondary'] ); ?></span>
                        <?php endif; ?>
                    </h3>
                <?php endif; ?>
                
            </div>

            <div class="lvmr-pc-container" id="<?php echo esc_attr($carousel_id); ?>">
                    <?php while ( $query->have_posts() ) : $query->the_post(); 
                        $product = wc_get_product( get_the_ID() );
                        if ( ! $product ) continue;
                        
                        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'full' );
                        $image_url = $image ? $image[0] : wc_placeholder_img_src();
                    ?>
                        <div class="lvmr-pc-grid-item">
                            <div class="lvmr-pc-item">
                                <a href="<?php echo esc_url( $product->get_permalink() ); ?>" class="lvmr-pc-item-image">
                                    <img src="<?php echo esc_url( $image_url ); ?>" alt="<?php echo esc_attr( $product->get_name() ); ?>">
                                </a>

                                <div class="lvmr-pc-item-content">
                                    <h4 class="lvmr-pc-item-title">
                                        <a href="<?php echo esc_url( $product->get_permalink() ); ?>">
                                            <?php echo esc_html( $product->get_name() ); ?>
                                        </a>
                                    </h4>
                                    
                                    <div class="lvmr-pc-item-price">
                                        <?php echo $product->get_price_html(); ?>
                                    </div>
                                </div>

                                <div class="lvmr-pc-item-addcart">
                                    <?php
                                    add_filter( 'woocommerce_loop_add_to_cart_link', function($html, $product) {
                                        return sprintf(
                                            '<a href="%s" data-quantity="1" class="%s">%s</a>',
                                            esc_url( $product->add_to_cart_url() ),
                                            'button btn-shop-now',
                                            esc_html__( 'Shop Now', 'woocommerce' )
                                        );
                                    }, 10, 2 );

                                    woocommerce_template_loop_add_to_cart();
                                    remove_all_filters( 'woocommerce_loop_add_to_cart_link' );
                                    ?>
                                </div>
                            </div>
                        </div>
                    <?php endwhile; wp_reset_postdata(); ?>
            </div>
        </div>
		<?php
	}
}

Directory Contents

Dirs: 0 × Files: 2

Name Size Perms Modified Actions
16.13 KB lrw-r--r-- 2026-03-11 09:38:37
Edit Download
11.48 KB lrw-r--r-- 2026-03-11 09:38:37
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).