PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.63
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.63
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_Price / Woo_Product_Price.php

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

270 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Woo Product Price 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 WooCommerce product price.
19 */
20 class Woo_Product_Price extends Abstract_Single_Widget
21 {
22 /**
23 * Get widget name.
24 */
25 public function get_name(): string
26 {
27 return 'woo_product_price';
28 }
29
30 /**
31 * Get widget title.
32 */
33 public function get_title(): string
34 {
35 return esc_html__('Product Price', 'king-addons');
36 }
37
38 /**
39 * Get widget icon.
40 */
41 public function get_icon(): string
42 {
43 return 'eicon-product-price';
44 }
45
46 /**
47 * Get categories.
48 *
49 * @return array<int,string>
50 */
51 public function get_categories(): array
52 {
53 return ['king-addons-woo-builder'];
54 }
55
56 /**
57 * Style dependencies.
58 *
59 * @return array
60 */
61 public function get_style_depends(): array
62 {
63 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-price-style'];
64 }
65
66 /**
67 * Controls.
68 */
69 protected function register_controls(): void
70 {
71 $this->start_controls_section(
72 'section_content',
73 [
74 'label' => esc_html__('Content', 'king-addons'),
75 'tab' => Controls_Manager::TAB_CONTENT,
76 ]
77 );
78
79 $this->add_control(
80 'display_mode',
81 [
82 'label' => esc_html__('Display', 'king-addons'),
83 'type' => Controls_Manager::SELECT,
84 'options' => [
85 'standard' => esc_html__('Standard (WooCommerce)', 'king-addons'),
86 'regular_only' => sprintf(__('Regular price only %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
87 'sale_only' => sprintf(__('Sale price only %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
88 ],
89 'default' => 'standard',
90 ]
91 );
92
93 $this->add_control(
94 'show_discount',
95 [
96 'label' => sprintf(__('Show discount percent %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
97 'type' => Controls_Manager::SWITCHER,
98 'return_value' => 'yes',
99 ]
100 );
101
102 $this->add_control(
103 'discount_format',
104 [
105 'label' => sprintf(__('Discount format %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
106 'type' => Controls_Manager::TEXT,
107 'default' => '-{percent}%',
108 'description' => esc_html__('Use {percent} placeholder.', 'king-addons'),
109 ]
110 );
111
112 $this->end_controls_section();
113
114 $this->start_controls_section(
115 'section_style',
116 [
117 'label' => esc_html__('Style', 'king-addons'),
118 'tab' => Controls_Manager::TAB_STYLE,
119 ]
120 );
121
122 $this->add_group_control(
123 Group_Control_Typography::get_type(),
124 [
125 'name' => 'typography',
126 'selector' => '{{WRAPPER}} .ka-woo-product-price',
127 ]
128 );
129
130 $this->add_control(
131 'color',
132 [
133 'label' => esc_html__('Price Color', 'king-addons'),
134 'type' => Controls_Manager::COLOR,
135 'selectors' => [
136 '{{WRAPPER}} .ka-woo-product-price .price, {{WRAPPER}} .ka-woo-product-price .price .amount' => 'color: {{VALUE}};',
137 ],
138 ]
139 );
140
141 $this->add_control(
142 'sale_color',
143 [
144 'label' => esc_html__('Sale Price Color', 'king-addons'),
145 'type' => Controls_Manager::COLOR,
146 'selectors' => [
147 '{{WRAPPER}} .ka-woo-product-price .price ins .amount' => 'color: {{VALUE}};',
148 ],
149 ]
150 );
151
152 $this->add_control(
153 'regular_color',
154 [
155 'label' => esc_html__('Regular Price Color', 'king-addons'),
156 'type' => Controls_Manager::COLOR,
157 'selectors' => [
158 '{{WRAPPER}} .ka-woo-product-price .price del .amount' => 'color: {{VALUE}};',
159 ],
160 ]
161 );
162
163 $this->add_control(
164 'discount_color',
165 [
166 'label' => esc_html__('Discount Badge Color', 'king-addons'),
167 'type' => Controls_Manager::COLOR,
168 'selectors' => [
169 '{{WRAPPER}} .ka-woo-product-price__discount' => 'color: {{VALUE}}; background: rgba(0,0,0,0.04);',
170 ],
171 'condition' => [
172 'show_discount' => 'yes',
173 ],
174 ]
175 );
176
177 $this->add_responsive_control(
178 'margin',
179 [
180 'label' => esc_html__('Margin', 'king-addons'),
181 'type' => Controls_Manager::DIMENSIONS,
182 'size_units' => ['px', 'em', '%'],
183 'selectors' => [
184 '{{WRAPPER}} .ka-woo-product-price' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
185 ],
186 ]
187 );
188
189 $this->add_responsive_control(
190 'padding',
191 [
192 'label' => esc_html__('Padding', 'king-addons'),
193 'type' => Controls_Manager::DIMENSIONS,
194 'size_units' => ['px', 'em', '%'],
195 'selectors' => [
196 '{{WRAPPER}} .ka-woo-product-price' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
197 ],
198 ]
199 );
200
201 $this->end_controls_section();
202 }
203
204 /**
205 * Render widget.
206 */
207 protected function render(): void
208 {
209 $product = $this->get_product();
210 if (!$product) {
211 $this->render_missing_product_notice();
212 return;
213 }
214
215 $settings = $this->get_settings_for_display();
216 $display_mode = $settings['display_mode'] ?? 'standard';
217 $can_pro = king_addons_can_use_pro();
218
219 if ($display_mode !== 'standard' && !$can_pro) {
220 $display_mode = 'standard';
221 }
222
223 $regular_raw = $product->get_regular_price();
224 $sale_raw = $product->get_sale_price();
225 // For variable products Woo returns min price; ensure float casting.
226 $regular = $regular_raw !== '' ? (float) $regular_raw : null;
227 $sale = $sale_raw !== '' ? (float) $sale_raw : null;
228
229 $price_html = $product->get_price_html();
230 $regular_html = $regular ? wc_price($regular) : '';
231 $sale_html = $sale ? wc_price($sale) : '';
232
233 if ('regular_only' === $display_mode) {
234 $price_html = $regular_html ?: $price_html;
235 } elseif ('sale_only' === $display_mode) {
236 $price_html = $sale_html ?: $price_html;
237 } elseif ($product->is_on_sale() && $regular_html && $sale_html) {
238 $price_html = '<span class="ka-woo-product-price__regular">' . $regular_html . '</span><span class="ka-woo-product-price__sale">' . $sale_html . '</span>';
239 }
240
241 $discount_html = '';
242 if (!empty($settings['show_discount']) && $can_pro && $product->is_on_sale()) {
243 $regular_for_calc = $regular;
244 $sale_for_calc = $sale;
245 if ($product->is_type('variable')) {
246 $regular_for_calc = (float) $product->get_variation_regular_price('min');
247 $sale_for_calc = (float) $product->get_variation_sale_price('min');
248 }
249 if ($regular_for_calc > 0 && $sale_for_calc > 0 && $sale_for_calc < $regular_for_calc) {
250 $percent = max(0, round((($regular_for_calc - $sale_for_calc) / $regular_for_calc) * 100));
251 $format = $settings['discount_format'] ?: '-{percent}%';
252 $discount_html = str_replace('{percent}', (string) $percent, $format);
253 $discount_html = '<span class="ka-woo-product-price__discount">' . esc_html($discount_html) . '</span>';
254 }
255 }
256
257 echo '<div class="ka-woo-product-price">';
258 echo wp_kses_post($price_html);
259 echo $discount_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
260 echo '</div>';
261 }
262 }
263
264
265
266
267
268
269
270