start_controls_section( 'content_section', [ 'label' => __('Content', 'cannaiq-menus'), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'type', [ 'label' => __('Type', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'thc', 'options' => [ 'thc' => __('THC', 'cannaiq-menus'), 'cbd' => __('CBD', 'cannaiq-menus'), ], ] ); $this->add_control( 'source', [ 'label' => __('Value Source', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'auto', 'options' => [ 'auto' => __('Auto (from product)', 'cannaiq-menus'), 'custom' => __('Custom value', 'cannaiq-menus'), ], ] ); $this->add_control( 'custom_value', [ 'label' => __('Percentage', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::NUMBER, 'default' => 20, 'min' => 0, 'max' => 100, 'step' => 0.1, 'condition' => [ 'source' => 'custom', ], ] ); $this->add_control( 'display_format', [ 'label' => __('Display Format', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'meter', 'options' => [ 'meter' => __('Meter (progress bar)', 'cannaiq-menus'), 'badge' => __('Badge', 'cannaiq-menus'), 'pill' => __('Pill', 'cannaiq-menus'), 'text' => __('Text Only', 'cannaiq-menus'), ], ] ); $this->add_control( 'show_label', [ 'label' => __('Show Label', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'cannaiq-menus'), 'label_off' => __('No', 'cannaiq-menus'), 'return_value' => 'yes', 'default' => 'yes', ] ); $this->add_control( 'max_percentage', [ 'label' => __('Max Percentage (for meter)', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::NUMBER, 'default' => 35, 'min' => 10, 'max' => 100, 'description' => __('Used to calculate bar fill percentage', 'cannaiq-menus'), 'condition' => [ 'display_format' => 'meter', ], ] ); $this->end_controls_section(); // Style Section $this->start_controls_section( 'style_section', [ 'label' => __('Style', 'cannaiq-menus'), 'tab' => \Elementor\Controls_Manager::TAB_STYLE, ] ); $this->add_control( 'thc_color', [ 'label' => __('THC Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#22c55e', ] ); $this->add_control( 'cbd_color', [ 'label' => __('CBD Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#3b82f6', ] ); $this->add_control( 'bar_height', [ 'label' => __('Bar Height', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SLIDER, 'size_units' => ['px'], 'range' => [ 'px' => [ 'min' => 4, 'max' => 20, ], ], 'default' => [ 'size' => 6, ], 'selectors' => [ '{{WRAPPER}} .cannaiq-potency-meter__bar' => 'height: {{SIZE}}{{UNIT}};', ], 'condition' => [ 'display_format' => 'meter', ], ] ); $this->add_control( 'bar_background', [ 'label' => __('Bar Background', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#e5e7eb', 'selectors' => [ '{{WRAPPER}} .cannaiq-potency-meter__bar' => 'background-color: {{VALUE}};', ], 'condition' => [ 'display_format' => 'meter', ], ] ); $this->add_group_control( \Elementor\Group_Control_Typography::get_type(), [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .cannaiq-potency-meter, {{WRAPPER}} .cannaiq-potency-badge', ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $type = $settings['type']; // Get percentage value $percentage = 0; if ($settings['source'] === 'custom') { $percentage = floatval($settings['custom_value']); } else { global $cannaiq_current_product; if (isset($cannaiq_current_product)) { if ($type === 'thc') { $percentage = $cannaiq_current_product['THCContent']['range'][0] ?? $cannaiq_current_product['THC'] ?? $cannaiq_current_product['thc_percentage'] ?? 0; } else { $percentage = $cannaiq_current_product['CBDContent']['range'][0] ?? $cannaiq_current_product['CBD'] ?? $cannaiq_current_product['cbd_percentage'] ?? 0; } } } $percentage = floatval($percentage); if ($percentage <= 0) { return; } $label = strtoupper($type); $color = $type === 'thc' ? $settings['thc_color'] : $settings['cbd_color']; $formatted_value = number_format($percentage, 1) . '%'; switch ($settings['display_format']) { case 'meter': $fill_percent = min(100, ($percentage / floatval($settings['max_percentage'])) * 100); ?>