PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.65
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.65
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 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / Quick_View_Product / ajax-handler.php

ajax-handler.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.65, at includes/widgets/Quick_View_Product/ajax-handler.php

182 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AJAX handler for Quick View Product.
4 *
5 * @package King_Addons
6 */
7
8 use King_Addons\Quick_View_Product;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * Render a controlled Add to Cart button without relying on Woo shortcode.
16 *
17 * @param \WC_Product $product Product instance.
18 *
19 * @return string
20 */
21 function king_addons_qv_render_add_to_cart_button(\WC_Product $product): string
22 {
23 if (!$product->is_purchasable() || !$product->is_in_stock()) {
24 return '<span class="king-addons-qv__add-to-cart button is-disabled">' . esc_html__('Unavailable', 'king-addons') . '</span>';
25 }
26
27 $classes = [
28 'king-addons-qv__add-to-cart',
29 'button',
30 'product_type_' . $product->get_type(),
31 ];
32
33 if ($product->supports('ajax_add_to_cart')) {
34 $classes[] = 'ajax_add_to_cart';
35 $classes[] = 'add_to_cart_button';
36 }
37
38 $attributes = [
39 'href' => $product->add_to_cart_url(),
40 'data-quantity' => 1,
41 'data-product_id' => $product->get_id(),
42 'data-product_sku' => $product->get_sku(),
43 'rel' => 'nofollow',
44 'class' => implode(' ', array_filter($classes)),
45 'aria-label' => wp_strip_all_tags($product->add_to_cart_description()),
46 ];
47
48 return '<a ' . wc_implode_html_attributes($attributes) . '><span class="king-addons-qv__add-to-cart-label">' . esc_html($product->add_to_cart_text()) . '</span></a>';
49 }
50
51 /**
52 * Render quick view HTML.
53 *
54 * @return void
55 */
56 function king_addons_handle_quick_view_ajax(): void
57 {
58 if (!class_exists('\WooCommerce')) {
59 wp_send_json_error(['message' => esc_html__('WooCommerce not active.', 'king-addons')], 400);
60 }
61
62 $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';
63 if (!wp_verify_nonce($nonce, 'king_addons_qv')) {
64 wp_send_json_error(['message' => esc_html__('Invalid nonce.', 'king-addons')], 400);
65 }
66
67 $product_id = isset($_POST['product_id']) ? (int) wp_unslash($_POST['product_id']) : 0;
68 if (!$product_id) {
69 wp_send_json_error(['message' => esc_html__('Invalid product.', 'king-addons')], 400);
70 }
71
72 $settings_json = isset($_POST['qv_settings']) ? (string) wp_unslash($_POST['qv_settings']) : '';
73 $settings = json_decode($settings_json, true);
74 if (!is_array($settings)) {
75 $settings = [];
76 }
77
78 $product = wc_get_product($product_id);
79 if (!$product) {
80 wp_send_json_error(['message' => esc_html__('Product not found.', 'king-addons')], 404);
81 }
82
83 $show_image = ($settings['show_image'] ?? 'yes') === 'yes';
84 $show_price = ($settings['show_price'] ?? 'yes') === 'yes';
85 $show_rating = ($settings['show_rating'] ?? 'yes') === 'yes';
86 $show_excerpt = ($settings['show_excerpt'] ?? 'yes') === 'yes';
87 $show_button = ($settings['show_button'] ?? 'yes') === 'yes';
88 $pro_gallery = ($settings['pro_gallery'] ?? 'no') === 'yes';
89 $pro_variations = ($settings['pro_variations'] ?? 'no') === 'yes';
90 $pro_sticky_cta = ($settings['pro_sticky_cta'] ?? 'no') === 'yes';
91
92 $html = '';
93
94 ob_start();
95 global $post;
96 $post = get_post($product_id);
97 if (!$post) {
98 wp_send_json_error(['message' => esc_html__('Product not found.', 'king-addons')], 404);
99 }
100 setup_postdata($post);
101
102 ?>
103 <div class="king-addons-qv <?php echo $pro_sticky_cta ? 'king-addons-qv--sticky' : ''; ?>">
104 <?php if ($show_image) : ?>
105 <div class="king-addons-qv__media">
106 <?php echo $product->get_image('large'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
107 <?php
108 if ($pro_gallery) {
109 $gallery_ids = $product->get_gallery_image_ids();
110 if (!empty($gallery_ids)) {
111 echo '<div class="king-addons-qv__thumbs">';
112 foreach ($gallery_ids as $gid) {
113 $thumb = wp_get_attachment_image($gid, 'thumbnail', false, ['class' => 'king-addons-qv__thumb']);
114 if ($thumb) {
115 echo '<div class="king-addons-qv__thumb-item">' . $thumb . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
116 }
117 }
118 echo '</div>';
119 }
120 }
121 ?>
122 </div>
123 <?php endif; ?>
124
125 <div class="king-addons-qv__content">
126 <h3 class="king-addons-qv__title"><?php echo esc_html($product->get_name()); ?></h3>
127
128 <?php if ($show_rating && wc_review_ratings_enabled()) : ?>
129 <div class="king-addons-qv__rating">
130 <?php echo wc_get_rating_html($product->get_average_rating()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
131 </div>
132 <?php endif; ?>
133
134 <?php if ($show_price) : ?>
135 <div class="king-addons-qv__price">
136 <?php echo $product->get_price_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
137 </div>
138 <?php endif; ?>
139
140 <?php if ($show_excerpt) : ?>
141 <div class="king-addons-qv__excerpt">
142 <?php echo wp_kses_post(wpautop($product->get_short_description())); ?>
143 </div>
144 <?php endif; ?>
145
146 <?php if ($show_button) : ?>
147 <div class="king-addons-qv__cta">
148 <?php
149 if ($product->is_type('variable')) {
150 if ($pro_variations) {
151 wc_get_template('single-product/add-to-cart/variable.php', ['product' => $product]);
152 } else {
153 echo '<p class="king-addons-qv__notice">' . esc_html__('Select options on the product page.', 'king-addons') . '</p>';
154 }
155 } elseif ($product->is_type('simple')) {
156 wc_get_template('single-product/add-to-cart/simple.php', ['product' => $product]);
157 } else {
158 echo king_addons_qv_render_add_to_cart_button($product); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
159 }
160 ?>
161 </div>
162 <?php endif; ?>
163 </div>
164 </div>
165 <?php
166
167 wp_reset_postdata();
168 $html = ob_get_clean();
169
170 wp_send_json_success(['html' => $html]);
171 }
172
173 add_action('wc_ajax_king_addons_qv', 'king_addons_handle_quick_view_ajax');
174 add_action('wc_ajax_nopriv_king_addons_qv', 'king_addons_handle_quick_view_ajax');
175
176
177
178
179
180
181
182