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_Variations / Woo_Product_Variations.php

Woo_Product_Variations.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_Variations/Woo_Product_Variations.php

290 lines 9.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 Variations / Swatches widget.
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use King_Addons\Woo_Builder\Context as Woo_Context;
12 use WC_Product_Variable;
13
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18 /**
19 * Renders variation attributes selector.
20 */
21 class Woo_Product_Variations extends Abstract_Single_Widget
22 {
23 /**
24 * Widget slug.
25 *
26 * @return string
27 */
28 public function get_name(): string
29 {
30 return 'woo_product_variations';
31 }
32
33 /**
34 * Widget title.
35 *
36 * @return string
37 */
38 public function get_title(): string
39 {
40 return esc_html__('Product Variations', 'king-addons');
41 }
42
43 /**
44 * Widget icon.
45 *
46 * @return string
47 */
48 public function get_icon(): string
49 {
50 return 'eicon-product-variations';
51 }
52
53 /**
54 * Widget categories.
55 *
56 * @return array<int,string>
57 */
58 public function get_categories(): array
59 {
60 return ['king-addons-woo-builder'];
61 }
62
63 /**
64 * Style dependencies.
65 *
66 * @return array<int,string>
67 */
68 public function get_style_depends(): array
69 {
70 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-variations-style'];
71 }
72
73 /**
74 * Script dependencies.
75 *
76 * @return array<int,string>
77 */
78 public function get_script_depends(): array
79 {
80 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-variations-script'];
81 }
82
83 protected function register_controls(): void
84 {
85 $this->start_controls_section(
86 'section_content',
87 [
88 'label' => esc_html__('Content', 'king-addons'),
89 'tab' => Controls_Manager::TAB_CONTENT,
90 ]
91 );
92
93 $this->add_control(
94 'swatches',
95 [
96 'label' => sprintf(__('Enable swatches %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 'swatch_source',
104 [
105 'label' => sprintf(__('Swatch source %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
106 'type' => Controls_Manager::SELECT,
107 'options' => [
108 'auto' => esc_html__('Term meta (color/image) fallback to text', 'king-addons'),
109 'text' => esc_html__('Text only', 'king-addons'),
110 ],
111 'default' => 'auto',
112 'condition' => [
113 'swatches' => 'yes',
114 ],
115 ]
116 );
117
118 $this->add_control(
119 'show_selected',
120 [
121 'label' => sprintf(__('Show selected value %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
122 'type' => Controls_Manager::SWITCHER,
123 'return_value' => 'yes',
124 'default' => 'yes',
125 ]
126 );
127
128 $this->add_control(
129 'unavailable_behavior',
130 [
131 'label' => sprintf(__('Unavailable behavior %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
132 'type' => Controls_Manager::SELECT,
133 'options' => [
134 'disable' => esc_html__('Disable', 'king-addons'),
135 'hide' => esc_html__('Hide', 'king-addons'),
136 ],
137 'default' => 'disable',
138 ]
139 );
140
141 $this->add_control(
142 'swatch_shape',
143 [
144 'label' => sprintf(__('Swatch shape %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
145 'type' => Controls_Manager::SELECT,
146 'options' => [
147 'square' => esc_html__('Square', 'king-addons'),
148 'round' => esc_html__('Round', 'king-addons'),
149 ],
150 'default' => 'square',
151 'condition' => [
152 'swatches' => 'yes',
153 ],
154 ]
155 );
156
157 $this->add_responsive_control(
158 'swatch_size',
159 [
160 'label' => sprintf(__('Swatch size (px) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
161 'type' => Controls_Manager::SLIDER,
162 'range' => [
163 'px' => ['min' => 16, 'max' => 80],
164 ],
165 'selectors' => [
166 '{{WRAPPER}} .ka-woo-variations__swatch' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
167 ],
168 'condition' => [
169 'swatches' => 'yes',
170 ],
171 ]
172 );
173
174 $this->add_responsive_control(
175 'swatch_spacing',
176 [
177 'label' => sprintf(__('Swatch spacing (px) %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
178 'type' => Controls_Manager::SLIDER,
179 'range' => [
180 'px' => ['min' => 0, 'max' => 24],
181 ],
182 'selectors' => [
183 '{{WRAPPER}} .ka-woo-variations__swatches' => 'gap: {{SIZE}}{{UNIT}};',
184 ],
185 'condition' => [
186 'swatches' => 'yes',
187 ],
188 ]
189 );
190
191 $this->end_controls_section();
192 }
193
194 protected function render(): void
195 {
196 if (
197 !class_exists('WooCommerce') ||
198 !function_exists('wc_attribute_taxonomy_name') ||
199 !function_exists('wc_get_product_terms') ||
200 !function_exists('wc_attribute_label') ||
201 !function_exists('wc_get_template')
202 ) {
203 return;
204 }
205
206 $product = $this->get_product();
207 if (!$product) {
208 $this->render_missing_product_notice();
209 return;
210 }
211
212 if (!$product instanceof WC_Product_Variable) {
213 if (class_exists(Woo_Context::class) && Woo_Context::is_editor()) {
214 echo '<div class="king-addons-woo-builder-notice">' . esc_html__('Variations widget works only for variable products.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
215 }
216 return;
217 }
218
219 $settings = $this->get_settings_for_display();
220 $can_pro = king_addons_can_use_pro();
221
222 $use_swatches = !empty($settings['swatches']) && $can_pro;
223 $unavailable_behavior = $settings['unavailable_behavior'] ?? 'disable';
224 $unavailable_behavior = in_array($unavailable_behavior, ['disable', 'hide'], true) ? $unavailable_behavior : 'disable';
225 $show_selected = !empty($settings['show_selected']);
226 $shape = $settings['swatch_shape'] ?? 'square';
227
228 // Build swatch map from attributes/terms for Pro swatches.
229 $swatch_map = [];
230 if ($use_swatches) {
231 $attributes = $product->get_variation_attributes();
232 foreach ($attributes as $attr_name => $options) {
233 $taxonomy = wc_attribute_taxonomy_name(str_replace('attribute_', '', $attr_name));
234 $swatches = [];
235 if (taxonomy_exists($taxonomy)) {
236 $terms = wc_get_product_terms($product->get_id(), $taxonomy, ['fields' => 'all']);
237 foreach ($terms as $term) {
238 $color_raw = get_term_meta($term->term_id, 'color', true);
239 $color = '';
240 if (is_string($color_raw)) {
241 $hex = sanitize_hex_color($color_raw);
242 $color = $hex ? $hex : '';
243 }
244 $thumb_id = get_term_meta($term->term_id, 'thumbnail_id', true);
245 $image = $thumb_id ? esc_url_raw(wp_get_attachment_image_url($thumb_id, 'thumbnail')) : '';
246 $swatches[$term->slug] = [
247 'label' => sanitize_text_field($term->name),
248 'color' => $color,
249 'image' => $image,
250 ];
251 }
252 } else {
253 foreach ($options as $opt) {
254 $swatches[$opt] = [
255 'label' => sanitize_text_field(wc_attribute_label($attr_name) . ' ' . $opt),
256 'color' => '',
257 'image' => '',
258 ];
259 }
260 }
261 $swatch_map[$attr_name] = $swatches;
262 }
263 }
264
265 $data = [
266 'swatches' => $use_swatches ? 'yes' : 'no',
267 'unavailable' => $unavailable_behavior,
268 'show_selected' => $show_selected ? 'yes' : 'no',
269 'shape' => $shape,
270 'map' => $swatch_map,
271 'source' => in_array(($settings['swatch_source'] ?? 'auto'), ['auto', 'text'], true) ? ($settings['swatch_source'] ?? 'auto') : 'auto',
272 ];
273
274 echo '<div class="ka-woo-variations" data-swatches="' . ($use_swatches ? 'yes' : 'no') . '" data-unavailable="' . esc_attr($unavailable_behavior) . '" data-swatches-map="' . esc_attr(wp_json_encode($data)) . '">';
275 wc_get_template('single-product/add-to-cart/variable.php', [
276 'available_variations' => $product->get_available_variations(),
277 'attributes' => $product->get_variation_attributes(),
278 'selected_attributes' => $product->get_default_attributes(),
279 ]);
280 echo '</div>';
281 }
282 }
283
284
285
286
287
288
289
290