start_controls_section( 'content_section', [ 'label' => __('Content', 'cannaiq-menus'), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'source', [ 'label' => __('Discount 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_discount', [ 'label' => __('Discount Percentage', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::NUMBER, 'default' => 25, 'min' => 1, 'max' => 99, 'condition' => [ 'source' => 'custom', ], ] ); $this->add_control( 'format', [ 'label' => __('Display Format', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'ribbon', 'options' => [ 'ribbon' => __('Ribbon', 'cannaiq-menus'), 'pill' => __('Pill', 'cannaiq-menus'), 'text' => __('Text Only', 'cannaiq-menus'), ], ] ); $this->add_control( 'text_template', [ 'label' => __('Text Template', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => '{percent}% OFF', 'description' => __('Use {percent} as placeholder', 'cannaiq-menus'), ] ); $this->add_control( 'hide_if_no_discount', [ 'label' => __('Hide if No Discount', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'cannaiq-menus'), 'label_off' => __('No', 'cannaiq-menus'), 'return_value' => 'yes', 'default' => 'yes', ] ); $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( 'size', [ 'label' => __('Size', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'medium', 'options' => [ 'small' => __('Small', 'cannaiq-menus'), 'medium' => __('Medium', 'cannaiq-menus'), 'large' => __('Large', 'cannaiq-menus'), ], ] ); $this->add_control( 'background_color', [ 'label' => __('Background Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#ef4444', 'selectors' => [ '{{WRAPPER}} .cannaiq-discount-ribbon' => 'background-color: {{VALUE}};', ], 'condition' => [ 'format!' => 'text', ], ] ); $this->add_control( 'text_color', [ 'label' => __('Text Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#ffffff', 'selectors' => [ '{{WRAPPER}} .cannaiq-discount-ribbon' => 'color: {{VALUE}};', ], ] ); $this->add_group_control( \Elementor\Group_Control_Typography::get_type(), [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .cannaiq-discount-ribbon', ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); // Get discount percentage $discount = 0; if ($settings['source'] === 'custom') { $discount = intval($settings['custom_discount']); } else { // Get from product context global $cannaiq_current_product; if (isset($cannaiq_current_product)) { $original = $cannaiq_current_product['Prices'][0] ?? $cannaiq_current_product['regular_price'] ?? null; $sale = $cannaiq_current_product['specialPrice'] ?? $cannaiq_current_product['sale_price'] ?? null; if ($original && $sale && $original > $sale) { $discount = round((($original - $sale) / $original) * 100); } } } // Hide if no discount and setting enabled if ($discount <= 0 && $settings['hide_if_no_discount'] === 'yes') { return; } // Build display text $text = str_replace('{percent}', $discount, $settings['text_template']); // Build classes $classes = [ 'cannaiq-discount-ribbon', 'cannaiq-discount-ribbon--' . $settings['format'], ]; if ($settings['size'] !== 'medium') { $classes[] = 'cannaiq-discount-ribbon--' . $settings['size']; } printf( '%s', esc_attr(implode(' ', $classes)), esc_html($text) ); } }