PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.3.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.3.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / Integrations / Elementor / Widgets / ProductQuickAddButton.php
ProductQuickAddButton.php
423 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\Integrations\Elementor\Widgets;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Product Quick Add Button widget.
11 */
12 class ProductQuickAddButton extends \Elementor\Widget_Base {
13 /**
14 * Default icon.
15 */
16 public const DEFAULT_ICON = array(
17 'value' => 'fas fa-plus',
18 'library' => 'fa-solid',
19 );
20
21 /**
22 * Get widget name.
23 *
24 * @return string
25 */
26 public function get_name() {
27 return 'surecart-product-quick-add-button';
28 }
29
30 /**
31 * Get widget title.
32 *
33 * @return string
34 */
35 public function get_title() {
36 return esc_html__( 'Product Quick Add Button', 'surecart' );
37 }
38
39 /**
40 * Get widget icon.
41 *
42 * @return string
43 */
44 public function get_icon() {
45 return 'eicon-plus-square';
46 }
47
48 /**
49 * Get the widget keywords.
50 *
51 * @return array
52 */
53 public function get_keywords() {
54 return array( 'surecart', 'shop', 'button', 'quick' );
55 }
56
57 /**
58 * Get the widget categories.
59 *
60 * @return array
61 */
62 public function get_categories() {
63 return array( 'surecart-elementor-elements' );
64 }
65
66 /**
67 * Get the default button label.
68 *
69 * @return string
70 */
71 protected function get_default_label(): string {
72 return esc_html__( 'Add', 'surecart' );
73 }
74
75 /**
76 * Get the style dependencies.
77 *
78 * @return array
79 */
80 public function get_style_depends() {
81 return array( 'surecart-wp-buttons', 'surecart-wp-button', 'wp-block-button', 'surecart-product-quick-view-button' );
82 }
83
84 /**
85 * Register the widget content settings.
86 *
87 * @return void
88 */
89 protected function register_content_settings() {
90 $this->start_controls_section(
91 'section_design',
92 [
93 'label' => esc_html__( 'Design', 'surecart' ),
94 ]
95 );
96
97 $this->add_control(
98 'label',
99 [
100 'label' => esc_html__( 'Button Text', 'surecart' ),
101 'type' => \Elementor\Controls_Manager::TEXT,
102 'default' => $this->get_default_label(),
103 ]
104 );
105
106 $this->add_control(
107 'icon_position',
108 [
109 'label' => esc_html__( 'Icon Position', 'surecart' ),
110 'type' => \Elementor\Controls_Manager::SELECT,
111 'default' => 'before',
112 'options' => [
113 'before' => esc_html__( 'Before', 'surecart' ),
114 'after' => esc_html__( 'After', 'surecart' ),
115 ],
116 ]
117 );
118
119 $this->add_control(
120 'quick_view_button_type',
121 [
122 'label' => esc_html__( 'Icon & Text', 'surecart' ),
123 'type' => \Elementor\Controls_Manager::SELECT,
124 'default' => 'both',
125 'options' => [
126 'icon' => esc_html__( 'Icon', 'surecart' ),
127 'text' => esc_html__( 'Text', 'surecart' ),
128 'both' => esc_html__( 'Both', 'surecart' ),
129 ],
130 ]
131 );
132
133 $this->add_control(
134 'show_on_hover',
135 array(
136 'label' => esc_html__( 'Show on Hover', 'surecart' ),
137 'type' => \Elementor\Controls_Manager::SWITCHER,
138 'label_on' => esc_html__( 'Yes', 'surecart' ),
139 'label_off' => esc_html__( 'No', 'surecart' ),
140 'default' => 'no',
141 )
142 );
143
144 $this->end_controls_section();
145
146 $this->start_controls_section(
147 'section_settings',
148 [
149 'label' => esc_html__( 'Settings', 'surecart' ),
150 ]
151 );
152
153 $this->add_control(
154 'direct_add_to_cart',
155 array(
156 'label' => esc_html__( 'Direct add to cart', 'surecart' ),
157 'type' => \Elementor\Controls_Manager::SWITCHER,
158 'label_on' => esc_html__( 'Yes', 'surecart' ),
159 'label_off' => esc_html__( 'No', 'surecart' ),
160 'return_value' => 'yes',
161 'default' => 'yes',
162 )
163 );
164
165 $this->end_controls_section();
166
167 $this->start_controls_section(
168 'section_icon_settings',
169 [
170 'label' => esc_html__( 'Icon', 'surecart' ),
171 ]
172 );
173
174 $this->add_control(
175 'selected_icon',
176 [
177 'label' => esc_html__( 'Icon', 'surecart' ),
178 'type' => \Elementor\Controls_Manager::ICONS,
179 'fa4compatibility' => 'icon',
180 'skin' => 'inline',
181 'label_block' => false,
182 'recommended' => [
183 'fa-solid' => [
184 'plus',
185 ],
186 ],
187 'default' => self::DEFAULT_ICON,
188 ]
189 );
190
191 $this->end_controls_section();
192 }
193
194 /**
195 * Register the widget style settings.
196 *
197 * @return void
198 */
199 protected function register_style_settings() {
200 $button_selector = '{{WRAPPER}} .wp-block-surecart-product-quick-view-button';
201 $button_icon_selector = '{{WRAPPER}} .elementor-button-icon svg';
202
203 $this->start_controls_section(
204 'section_style',
205 [
206 'label' => esc_html__( 'Button', 'surecart' ),
207 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
208 ]
209 );
210
211 $this->add_group_control(
212 \Elementor\Group_Control_Typography::get_type(),
213 [
214 'name' => 'button_typography',
215 'global' => [
216 'default' => \Elementor\Core\Kits\Documents\Tabs\Global_Typography::TYPOGRAPHY_PRIMARY,
217 ],
218 'fields_options' => [
219 'typography' => [ 'default' => 'yes' ],
220 'line_height' => [
221 'default' => [
222 'unit' => 'px',
223 'size' => 16,
224 ],
225 ],
226 ],
227 'selector' => $button_selector,
228 ]
229 );
230
231 $this->add_control(
232 'content_align',
233 [
234 'label' => esc_html__( 'Content Alignment', 'surecart' ),
235 'type' => \Elementor\Controls_Manager::CHOOSE,
236 'options' => [
237 'flex-start' => [
238 'title' => esc_html__( 'Start', 'surecart' ),
239 'icon' => 'eicon-h-align-left',
240 ],
241 'center' => [
242 'title' => esc_html__( 'Center', 'surecart' ),
243 'icon' => 'eicon-h-align-center',
244 ],
245 'flex-end' => [
246 'title' => esc_html__( 'End', 'surecart' ),
247 'icon' => 'eicon-h-align-right',
248 ],
249 ],
250 'default' => 'flex-start',
251 'selectors' => [
252 $button_selector => 'justify-content: {{VALUE}};',
253 ],
254 ]
255 );
256
257 $this->add_control(
258 'button_text_color',
259 [
260 'label' => esc_html__( 'Text Color', 'surecart' ),
261 'type' => \Elementor\Controls_Manager::COLOR,
262 'selectors' => [
263 $button_selector => 'color: {{VALUE}}',
264 $button_icon_selector => 'fill: {{VALUE}}',
265 ],
266 'default' => '#ffffff',
267 ]
268 );
269
270 $this->add_control(
271 'button_background_color',
272 [
273 'label' => esc_html__( 'Background Color', 'surecart' ),
274 'type' => \Elementor\Controls_Manager::COLOR,
275 'selectors' => [
276 $button_selector => 'background-color: {{VALUE}}',
277 ],
278 ]
279 );
280
281 $this->add_group_control(
282 \Elementor\Group_Control_Border::get_type(),
283 [
284 'name' => 'border',
285 'selector' => $button_selector,
286 ]
287 );
288
289 $this->add_control(
290 'border_radius',
291 array(
292 'label' => esc_html__( 'Border Radius', 'surecart' ),
293 'type' => \Elementor\Controls_Manager::DIMENSIONS,
294 'size_units' => [ 'px', 'em', '%' ],
295 'selectors' => array(
296 $button_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
297 ),
298 )
299 );
300
301 $this->add_responsive_control(
302 'padding',
303 [
304 'type' => \Elementor\Controls_Manager::DIMENSIONS,
305 'label' => esc_html__( 'Padding', 'surecart' ),
306 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
307 'selectors' => [
308 $button_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
309 ],
310 ]
311 );
312
313 $this->add_responsive_control(
314 'margin',
315 [
316 'type' => \Elementor\Controls_Manager::DIMENSIONS,
317 'label' => esc_html__( 'Margin', 'surecart' ),
318 'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
319 'selectors' => [
320 $button_selector => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
321 ],
322 ]
323 );
324
325 $this->end_controls_section();
326 }
327
328 /**
329 * Register the widget controls.
330 *
331 * @return void
332 */
333 protected function register_controls() {
334 $this->register_content_settings();
335 $this->register_style_settings();
336 }
337
338 /**
339 * Render the widget output on the frontend.
340 *
341 * @return void
342 */
343 protected function render() {
344 $settings = $this->get_settings_for_display();
345 $product_id = get_the_ID();
346 $product = sc_get_product();
347 $is_add_to_cart = ! empty( $settings['direct_add_to_cart'] ) && 'yes' === $settings['direct_add_to_cart'];
348 $should_direct_add = $is_add_to_cart && empty( $product->has_options );
349 $show_on_hover_class = 'yes' === $settings['show_on_hover'] ? 'is-style-show-on-hover ' : '';
350 $attributes = array(
351 'icon_position' => $settings['icon_position'] ?? 'before',
352 'quick_view_button_type' => $settings['quick_view_button_type'] ?? 'both',
353 'label' => $settings['label'] ?? $this->get_default_label(),
354 );
355 $show_icon = in_array( $attributes['quick_view_button_type'], [ 'icon', 'both' ], true ) && ( ! empty( $settings['icon'] ) || ! empty( $settings['selected_icon']['value'] ) );
356 $show_text = in_array( $attributes['quick_view_button_type'], [ 'text', 'both' ], true );
357 $quick_view_link = add_query_arg( 'product-quick-view', $product_id );
358 $is_disabled = empty( $quick_view_link ) ? 'true' : null;
359
360 if ( $should_direct_add ) {
361 $is_disabled = empty( $product->in_stock ) ? 'true' : null;
362 $aria_label = empty( $product->in_stock ) ? __( 'Sold Out', 'surecart' ) : __( 'Add to Cart', 'surecart' );
363
364 $this->add_render_attribute( 'quick-add-button', 'disabled', $is_disabled );
365 $this->add_render_attribute( 'quick-add-button', 'aria-label', $aria_label );
366 $this->add_render_attribute( 'quick-add-button', 'data-wp-on--click', 'callbacks.handleSubmit' );
367 $this->add_render_attribute( 'quick-add-button', 'data-wp-on--keydown', 'callbacks.handleSubmit' );
368 } else {
369 $this->add_render_attribute(
370 'quick-add-button',
371 'data-wp-context',
372 wp_json_encode(
373 [
374 'url' => sanitize_url( $quick_view_link ),
375 ],
376 JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
377 )
378 );
379 $this->add_render_attribute( 'quick-add-button', 'aria-label', __( 'Quick Add Product', 'surecart' ) );
380 $this->add_render_attribute( 'quick-add-button', 'data-wp-on--click', 'actions.open' );
381 $this->add_render_attribute( 'quick-add-button', 'data-wp-on--keydown', 'actions.open' );
382 $this->add_render_attribute( 'quick-add-button', 'data-wp-on--mouseenter', 'actions.prefetch' );
383 $this->add_render_attribute( 'quick-add-button', 'data-wp-interactive', '{ "namespace": "surecart/product-quick-view" }' );
384 }
385
386 $this->add_render_attribute( 'quick-add-button', 'class', 'wp-block-surecart-product-quick-view-button wp-block-button__link sc-button__link elementor-button elementor-button-link elementor-size-sm ' . $show_on_hover_class );
387 $this->add_render_attribute( 'quick-add-button', 'data-wp-class--loading', 'state.loading' );
388 $this->add_render_attribute( 'quick-add-button', 'data-wp-class--sc-button__link--busy', 'state.loading' );
389 $this->add_render_attribute( 'quick-add-button', 'aria-disabled', $is_disabled );
390
391 if ( $show_icon && ! empty( $settings['selected_icon']['value'] ) ) {
392 $this->add_render_attribute( 'icon', 'class', 'elementor-button-icon' );
393 }
394
395 if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
396 \SureCart::block()->quickView()->render();
397 }
398
399 ?>
400 <div
401 role="button"
402 tabindex="0"
403 <?php echo $this->get_render_attribute_string( 'quick-add-button' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
404 >
405 <span class="sc-spinner" aria-hidden="true"></span>
406 <?php if ( $show_icon && 'before' === $attributes['icon_position'] ) : ?>
407 <span <?php echo $this->get_render_attribute_string( 'icon' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
408 <?php \Elementor\Icons_Manager::render_icon( $settings['selected_icon'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
409 </span>
410 <?php endif; ?>
411 <?php if ( $show_text ) : ?>
412 <span class="sc-button__link-text"><?php echo esc_html( empty( $product->in_stock ) ? __( 'Sold Out', 'surecart' ) : $attributes['label'] ); ?></span>
413 <?php endif; ?>
414 <?php if ( $show_icon && 'after' === $attributes['icon_position'] ) : ?>
415 <span <?php echo $this->get_render_attribute_string( 'icon' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
416 <?php \Elementor\Icons_Manager::render_icon( $settings['selected_icon'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
417 </span>
418 <?php endif; ?>
419 </div>
420 <?php
421 }
422 }
423