start_controls_section( 'content_section', [ 'label' => __('Content', 'cannaiq-menus'), 'tab' => \Elementor\Controls_Manager::TAB_CONTENT, ] ); $this->add_control( 'source', [ 'label' => __('Stock 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_in_stock', [ 'label' => __('In Stock', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'cannaiq-menus'), 'label_off' => __('No', 'cannaiq-menus'), 'return_value' => 'yes', 'default' => 'yes', 'condition' => [ 'source' => 'custom', ], ] ); $this->add_control( 'format', [ 'label' => __('Display Format', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'badge', 'options' => [ 'badge' => __('Badge', 'cannaiq-menus'), 'text' => __('Text', 'cannaiq-menus'), 'dot' => __('Dot + Text', 'cannaiq-menus'), ], ] ); $this->add_control( 'in_stock_text', [ 'label' => __('In Stock Text', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'In Stock', ] ); $this->add_control( 'out_of_stock_text', [ 'label' => __('Out of Stock Text', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'Out of Stock', ] ); $this->add_control( 'show_quantity', [ 'label' => __('Show Quantity', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'cannaiq-menus'), 'label_off' => __('No', 'cannaiq-menus'), 'return_value' => 'yes', 'default' => '', 'description' => __('Show quantity if available', 'cannaiq-menus'), ] ); $this->add_control( 'hide_if_in_stock', [ 'label' => __('Hide if In Stock', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('Yes', 'cannaiq-menus'), 'label_off' => __('No', 'cannaiq-menus'), 'return_value' => 'yes', 'default' => '', 'description' => __('Only show when out of stock', 'cannaiq-menus'), ] ); $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( 'in_stock_color', [ 'label' => __('In Stock Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#16a34a', ] ); $this->add_control( 'out_of_stock_color', [ 'label' => __('Out of Stock Color', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#9ca3af', ] ); $this->add_control( 'in_stock_bg', [ 'label' => __('In Stock Background', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#dcfce7', 'condition' => [ 'format' => 'badge', ], ] ); $this->add_control( 'out_of_stock_bg', [ 'label' => __('Out of Stock Background', 'cannaiq-menus'), 'type' => \Elementor\Controls_Manager::COLOR, 'default' => '#f3f4f6', 'condition' => [ 'format' => 'badge', ], ] ); $this->add_group_control( \Elementor\Group_Control_Typography::get_type(), [ 'name' => 'typography', 'selector' => '{{WRAPPER}} .cannaiq-stock-indicator', ] ); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); // Get stock status $in_stock = true; $quantity = null; if ($settings['source'] === 'custom') { $in_stock = $settings['custom_in_stock'] === 'yes'; } else { global $cannaiq_current_product; if (isset($cannaiq_current_product)) { $status = $cannaiq_current_product['Status'] ?? ''; $in_stock = ($status === 'Active' || $status === 'In Stock' || !empty($cannaiq_current_product['in_stock'])); // Get quantity if available $quantity = $cannaiq_current_product['POSMetaData']['children'][0]['quantity'] ?? $cannaiq_current_product['quantity'] ?? null; } } // Hide if in stock and setting enabled if ($in_stock && $settings['hide_if_in_stock'] === 'yes') { return; } // Determine display text $text = $in_stock ? $settings['in_stock_text'] : $settings['out_of_stock_text']; if ($in_stock && $settings['show_quantity'] === 'yes' && $quantity !== null) { $text .= ' (' . intval($quantity) . ')'; } // Colors $color = $in_stock ? $settings['in_stock_color'] : $settings['out_of_stock_color']; $bg_color = $in_stock ? $settings['in_stock_bg'] : $settings['out_of_stock_bg']; // Build classes $classes = [ 'cannaiq-stock-indicator', $in_stock ? 'cannaiq-stock-indicator--in-stock' : 'cannaiq-stock-indicator--out-of-stock', ]; if ($settings['format'] === 'badge') { $classes[] = 'cannaiq-stock-indicator--badge'; } // Build style $style = sprintf('color: %s;', esc_attr($color)); if ($settings['format'] === 'badge') { $style .= sprintf(' background-color: %s;', esc_attr($bg_color)); } ?>