PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.84
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.84
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / widgets / Woo_Product_Stock / Woo_Product_Stock.php

Woo_Product_Stock.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.84, at includes/widgets/Woo_Product_Stock/Woo_Product_Stock.php

271 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Woo Product Stock widget.
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use Elementor\Group_Control_Typography;
12
13 if (!defined('ABSPATH')) {
14 exit;
15 }
16
17 /**
18 * Displays product stock status.
19 */
20 class Woo_Product_Stock extends Abstract_Single_Widget
21 {
22 public function get_name(): string
23 {
24 return 'woo_product_stock';
25 }
26
27 public function get_title(): string
28 {
29 return esc_html__('Product Stock', 'king-addons');
30 }
31
32 public function get_icon(): string
33 {
34 return 'king-addons-icon king-addons-woo-product-stock';
35 }
36
37 public function get_categories(): array
38 {
39 return ['king-addons-woo-builder'];
40 }
41
42 public function get_style_depends(): array
43 {
44 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-stock-style'];
45 }
46
47 protected function register_controls(): void
48 {
49 $this->start_controls_section(
50 'section_content',
51 [
52 'label' => esc_html__('Content', 'king-addons'),
53 'tab' => Controls_Manager::TAB_CONTENT,
54 ]
55 );
56
57 $this->add_control(
58 'text_in_stock',
59 [
60 'label' => esc_html__('In stock text', 'king-addons'),
61 'type' => Controls_Manager::TEXT,
62 'dynamic' => ['active' => true],
63 'default' => esc_html__('In stock', 'king-addons'),
64 ]
65 );
66
67 $this->add_control(
68 'text_out_stock',
69 [
70 'label' => esc_html__('Out of stock text', 'king-addons'),
71 'type' => Controls_Manager::TEXT,
72 'dynamic' => ['active' => true],
73 'default' => esc_html__('Out of stock', 'king-addons'),
74 ]
75 );
76
77 $this->add_control(
78 'text_on_backorder',
79 [
80 'label' => sprintf(__('On backorder text %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
81 'type' => Controls_Manager::TEXT,
82 'dynamic' => ['active' => true],
83 'default' => esc_html__('Available on backorder', 'king-addons'),
84 ]
85 );
86
87 $this->add_control(
88 'show_quantity',
89 [
90 'label' => sprintf(__('Show quantity %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
91 'type' => Controls_Manager::SWITCHER,
92 'return_value' => 'yes',
93 ]
94 );
95
96 $this->add_control(
97 'show_status_icon',
98 [
99 'label' => sprintf(__('Show status icon %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
100 'type' => Controls_Manager::SWITCHER,
101 'return_value' => 'yes',
102 'default' => 'yes',
103 ]
104 );
105
106 $this->add_control(
107 'low_stock_threshold',
108 [
109 'label' => sprintf(__('Low stock threshold %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
110 'type' => Controls_Manager::NUMBER,
111 'min' => 1,
112 'description' => esc_html__('Show low stock message when below threshold.', 'king-addons'),
113 ]
114 );
115
116 $this->add_control(
117 'text_low_stock',
118 [
119 'label' => esc_html__('Low stock text (Pro)', 'king-addons'),
120 'type' => Controls_Manager::TEXT,
121 'dynamic' => ['active' => true],
122 'default' => esc_html__('Only {qty} left', 'king-addons'),
123 ]
124 );
125
126 $this->add_control(
127 'hide_if_in_stock',
128 [
129 'label' => sprintf(__('Hide if in stock %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
130 'type' => Controls_Manager::SWITCHER,
131 'return_value' => 'yes',
132 ]
133 );
134
135 $this->end_controls_section();
136
137 $this->start_controls_section(
138 'section_style',
139 [
140 'label' => esc_html__('Style', 'king-addons'),
141 'tab' => Controls_Manager::TAB_STYLE,
142 ]
143 );
144
145 $this->add_group_control(
146 Group_Control_Typography::get_type(),
147 [
148 'name' => 'typography',
149 'selector' => '{{WRAPPER}} .ka-woo-product-stock',
150 ]
151 );
152
153 $this->add_control(
154 'color_in',
155 [
156 'label' => esc_html__('Color: In stock', 'king-addons'),
157 'type' => Controls_Manager::COLOR,
158 'selectors' => [
159 '{{WRAPPER}} .ka-woo-product-stock--in' => 'color: {{VALUE}};',
160 ],
161 ]
162 );
163
164 $this->add_control(
165 'color_low',
166 [
167 'label' => esc_html__('Color: Low stock (Pro)', 'king-addons'),
168 'type' => Controls_Manager::COLOR,
169 'selectors' => [
170 '{{WRAPPER}} .ka-woo-product-stock--low' => 'color: {{VALUE}};',
171 ],
172 ]
173 );
174
175 $this->add_control(
176 'color_out',
177 [
178 'label' => esc_html__('Color: Out of stock', 'king-addons'),
179 'type' => Controls_Manager::COLOR,
180 'selectors' => [
181 '{{WRAPPER}} .ka-woo-product-stock--out' => 'color: {{VALUE}};',
182 ],
183 ]
184 );
185
186 $this->add_control(
187 'color_backorder',
188 [
189 'label' => sprintf(__('Color: Backorder %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
190 'type' => Controls_Manager::COLOR,
191 'selectors' => [
192 '{{WRAPPER}} .ka-woo-product-stock--backorder' => 'color: {{VALUE}};',
193 ],
194 ]
195 );
196
197 $this->add_responsive_control(
198 'margin',
199 [
200 'label' => esc_html__('Margin', 'king-addons'),
201 'type' => Controls_Manager::DIMENSIONS,
202 'size_units' => ['px', 'em', '%'],
203 'selectors' => [
204 '{{WRAPPER}} .ka-woo-product-stock' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
205 ],
206 ]
207 );
208
209 $this->end_controls_section();
210 }
211
212 protected function render(): void
213 {
214 $product = $this->get_product();
215 if (!$product) {
216 $this->render_missing_product_notice();
217 return;
218 }
219
220 $settings = $this->get_settings_for_display();
221 $can_pro = king_addons_can_use_pro();
222
223 $is_in_stock = $product->is_in_stock();
224 $qty = $product->get_stock_quantity();
225
226 if (!empty($settings['hide_if_in_stock']) && $can_pro && $is_in_stock && empty($settings['low_stock_threshold'])) {
227 return;
228 }
229
230 $text = $settings['text_in_stock'] ?? esc_html__('In stock', 'king-addons');
231 $class = 'ka-woo-product-stock ka-woo-product-stock--in';
232 $icon = '';
233 if (!empty($settings['show_status_icon']) && $can_pro) {
234 $icon = '<span class="ka-woo-product-stock__icon" aria-hidden="true"></span>';
235 }
236
237 if (!$is_in_stock) {
238 $text = $settings['text_out_stock'] ?? esc_html__('Out of stock', 'king-addons');
239 $class = 'ka-woo-product-stock ka-woo-product-stock--out';
240 } elseif ($product->backorders_allowed() && !empty($settings['text_on_backorder']) && $can_pro) {
241 $text = $settings['text_on_backorder'];
242 $class = 'ka-woo-product-stock ka-woo-product-stock--backorder';
243 }
244
245 if ($is_in_stock && $can_pro && !empty($settings['low_stock_threshold']) && is_numeric($qty)) {
246 $threshold = (int) $settings['low_stock_threshold'];
247 // A backorder item sits at qty 0 while still being "in stock";
248 // announcing "0 left" there reads as a mistake.
249 if ($threshold > 0 && $qty > 0 && $qty <= $threshold) {
250 $low_text = $settings['text_low_stock'] ?: esc_html__('Only {qty} left', 'king-addons');
251 $low_text = str_replace('{qty}', (string) $qty, $low_text);
252 $text = $low_text;
253 $class = 'ka-woo-product-stock ka-woo-product-stock--low';
254 }
255 }
256
257 if (!empty($settings['show_quantity']) && $can_pro && $is_in_stock && is_numeric($qty) && empty($settings['low_stock_threshold'])) {
258 $text .= ' (' . (int) $qty . ')';
259 }
260
261 echo '<div class="' . esc_attr($class) . '">' . $icon . esc_html($text) . '</div>';
262 }
263 }
264
265
266
267
268
269
270
271