PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 1.0.0
ShopBuilder – WooCommerce Builder For Elementor v1.0.0
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / WishList / WishlistFns.php

WishlistFns.php in ShopBuilder – WooCommerce Builder For Elementor 1.0.0, at app/Modules/WishList/WishlistFns.php

467 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace RadiusTheme\SB\Modules\WishList;
4
5 use RadiusTheme\SB\Helpers\Fns;
6 use RadiusTheme\SB\Traits\SingletonTrait;
7 use WC_Product;
8
9 // Do not allow directly accessing this file.
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit( 'This script cannot be accessed directly.' );
12 }
13
14 class WishlistFns {
15
16 use SingletonTrait;
17
18 const WC_MENU_SLUG = 'wishlist';
19
20 /**
21 * Remove product id
22 *
23 * @param int $product_id
24 *
25 * @return bool|int
26 */
27 public function remove_product( $product_id ) {
28
29 if ( ! $this->is_exists_in_wishlist( $product_id ) ) {
30 return 0;
31 }
32 $pIds = $this->get_wishlist_ids();
33 $pIds = array_diff( $pIds, [ $product_id ] );
34 if ( is_user_logged_in() ) {
35 $this->update_user_wishlist_ids( $pIds );
36 } else {
37 $cookie_name = $this->get_cookie_name();
38 if ( empty( $pIds ) ) {
39 setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
40 $_COOKIE[ $cookie_name ] = false;
41 } else {
42 setcookie( $cookie_name, json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
43 $_COOKIE[ $cookie_name ] = json_encode( $pIds );
44 }
45 }
46
47 return true;
48 }
49
50 public static function get_page_url() {
51 $page_id = Fns::get_option( 'modules', 'wishlist', 'page' );
52
53 return get_permalink( $page_id );
54 }
55
56 /**
57 * Product Add
58 *
59 * @param integer $product_id
60 */
61 public function add_product( $product_id ) {
62
63 if ( $this->is_exists_in_wishlist( $product_id ) ) {
64 return 0;
65 }
66
67 $pIds = $this->get_wishlist_ids();
68 $pIds[] = $product_id;
69 if ( is_user_logged_in() ) {
70 $this->update_user_wishlist_ids( $pIds );
71 } else {
72 $cookie_name = $this->get_cookie_name();
73
74 setcookie( $cookie_name, json_encode( $pIds ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
75 $_COOKIE[ $cookie_name ] = json_encode( $pIds );
76 }
77
78 return 1;
79 }
80
81 /**
82 * [get_products_data] generate wishlist products data
83 *
84 * @return array product list
85 */
86 public function get_products_data() {
87
88 $ids = $this->get_wishlist_ids();
89
90 if ( empty( $ids ) ) {
91 return [];
92 }
93
94 $args = [
95 'include' => $ids,
96 ];
97
98 $products = wc_get_products( $args );
99
100 $products_data = [];
101
102 $fields = $this->get_field_ids();
103
104 $fields = array_filter(
105 $fields,
106 function ( $field ) {
107 return 'pa_' === substr( $field, 0, 3 );
108 },
109 ARRAY_FILTER_USE_KEY
110 );
111
112 $data_none = '-';
113
114 foreach ( $products as $product ) {
115
116 $rating_count = $product->get_rating_count();
117 $average = $product->get_average_rating();
118 $quantity_args = [
119 'input_value' => 1,
120 'min_value' => $product->get_min_purchase_quantity(),
121 'max_value' => $product->get_max_purchase_quantity(),
122 ];
123
124 $products_data[ $product->get_id() ] = [
125 'id' => $product->get_id(),
126 'remove' => $product->get_id(),
127 'image' => $product->get_image() ? $product->get_image() : $data_none,
128 'title' => $product->get_title() ? $product->get_title() : $data_none,
129 'image_id' => $product->get_image_id(),
130 'permalink' => $product->get_permalink(),
131 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
132 'rating' => wc_get_rating_html( $average, $rating_count ),
133 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
134 'quantity' => woocommerce_quantity_input( $quantity_args, $product, false ),
135 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
136 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
137 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
138 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
139 'availability' => $this->availability_html( $product ),
140 ];
141
142 foreach ( $fields as $field_id => $field_name ) {
143 if ( taxonomy_exists( $field_id ) ) {
144 $products_data[ $product->get_id() ][ $field_id ] = [];
145 $terms = get_the_terms( $product->get_id(), $field_id );
146 if ( ! empty( $terms ) ) {
147 foreach ( $terms as $term ) {
148 $term = sanitize_term( $term, $field_id );
149 $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
150 }
151 } else {
152 $products_data[ $product->get_id() ][ $field_id ][] = '-';
153 }
154 $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
155 }
156 }
157 }
158
159 return $products_data;
160 }
161
162 /**
163 * Table field list
164 *
165 * @return array Table Field list
166 */
167 public function get_field_ids() {
168
169 $default_show = [
170 'info',
171 'price',
172 'availability',
173 'add_to_cart',
174 'remove',
175 ];
176
177 $fields_settings = Fns::get_option( 'modules', 'wishlist', 'page_show_fields', [] );
178
179 if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
180 $fields = $fields_settings;
181 } else {
182 $fields = $default_show;
183 }
184
185 return $fields;
186 }
187
188
189 /**
190 * Get default fields List
191 * return array
192 */
193 function get_default_fields() {
194 $fields = [
195 'info' => esc_html__( 'Products', 'shopbuilder' ),
196 'image' => esc_html__( 'Image', 'shopbuilder' ),
197 'title' => esc_html__( 'Title', 'shopbuilder' ),
198 'price' => esc_html__( 'Price', 'shopbuilder' ),
199 'quantity' => esc_html__( 'Quantity', 'shopbuilder' ),
200 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ),
201 'remove' => esc_html__( 'Remove', 'shopbuilder' ),
202 'description' => esc_html__( 'Description', 'shopbuilder' ),
203 'availability' => esc_html__( 'Availability', 'shopbuilder' ),
204 'sku' => esc_html__( 'Sku', 'shopbuilder' ),
205 'weight' => esc_html__( 'Weight', 'shopbuilder' ),
206 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ),
207 ];
208
209 return apply_filters( 'rtsb/module/wishlist/list_default_fields', $fields );
210 }
211
212 /**
213 * [get_cookie_name] Get cookie name
214 *
215 * @return string [string]
216 */
217 public function get_cookie_name() {
218 $name = Wishlist::KEY;
219 if ( is_multisite() ) {
220 $name .= '_' . get_current_blog_id();
221 }
222
223 return $name;
224 }
225
226
227 /**
228 * display_field
229 *
230 * @param string $field_id
231 * @param array $product
232 *
233 * @return void [html]
234 */
235 public function display_field( $field_id, $product ) {
236
237 $type = $field_id;
238
239 if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
240 $type = 'attribute';
241 }
242
243 switch ( $type ) {
244 case 'info':
245 ?>
246 <a href="<?php echo get_permalink( $product['id'] ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a>
247 <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product['title'] ); ?></a>
248 <?php
249 break;
250
251 case 'remove':
252 ?>
253 <a href="#" class="rtsb-wishlist-remove"
254 data-product_id="<?php echo esc_attr( $product['id'] ); ?>"><i class="rtsb-icon-trash-empty"></i></a>
255 <?php
256 break;
257
258 case 'image':
259 ?>
260 <a href="<?php echo get_permalink( $product['id'] ); ?>"> <?php echo wp_kses( $product['image'], Fns::allowedHtml( 'image' ) ); ?> </a>
261 <?php
262 break;
263
264 case 'title':
265 ?>
266 <a href="<?php the_permalink( $product['id'] ); ?>"><?php echo esc_html( $product[ $field_id ] ); ?></a>
267 <?php
268 break;
269
270 case 'price':
271 echo wp_kses_post( $product[ $field_id ] );
272 break;
273
274 case 'quantity':
275 Fns::print_html( $product[ $field_id ] );
276 break;
277
278 case 'ratting':
279 echo '<span class="rtsb-wl-product-ratting">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
280 break;
281
282 case 'add_to_cart':
283 echo apply_filters( 'rtsb/modules/wishlist/add_to_cart_btn', $product[ $field_id ] );
284 break;
285
286 case 'attribute':
287 echo wp_kses_post( $product[ $field_id ] );
288 break;
289
290 case 'weight':
291 if ( $product[ $field_id ] ) {
292 $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
293 echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit );
294 }
295 break;
296
297 case 'description':
298 echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
299 break;
300
301 default:
302 echo wp_kses_post( $product[ $field_id ] );
303 break;
304 }
305
306 }
307
308 /**
309 * Field name
310 *
311 * @param string $field
312 *
313 * @return string|void
314 */
315 public function field_name( $field, $custom = false ) {
316
317 if ( empty( $field ) ) {
318 return;
319 }
320
321 if ( $custom === true ) {
322 return $field;
323 }
324
325 $default = $this->get_default_fields();
326
327 $str = substr( $field, 0, 3 );
328 if ( 'pa_' === $str ) {
329 $field_name = wc_attribute_label( $field );
330 } else {
331 $field_name = $default[ $field ];
332 }
333
334 return $field_name;
335
336 }
337
338
339 /**
340 * Get wishlist product ids
341 *
342 * @return array
343 */
344 public function get_wishlist_ids() {
345 $ids = [];
346 if ( is_user_logged_in() ) {
347 $listIds = get_user_meta( get_current_user_id(), Wishlist::KEY, true );
348 if ( is_array( $listIds ) && ! empty( $listIds ) ) {
349 $ids = array_map( 'absint', $listIds );
350 }
351 } else {
352 $cookie_name = $this->get_cookie_name();
353 if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
354 $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
355 }
356 }
357
358 return $ids;
359 }
360
361 public function update_user_wishlist_ids( $product_ids ) {
362 $uid = get_current_user_id();
363 update_user_meta( $uid, Wishlist::KEY, $product_ids );
364 }
365
366 /**
367 * @param $product_id
368 *
369 * @return bool
370 */
371 public function is_exists_in_wishlist( $product_id ) {
372 $id = $product_id;
373 $list = $this->get_wishlist_ids();
374 if ( is_array( $list ) ) {
375 return in_array( $id, $list, true );
376 } else {
377 return false;
378 }
379
380 }
381
382
383 /**
384 * @param WC_Product $product
385 *
386 * @return string
387 */
388 public function availability_html( $product ) {
389 $html = '';
390 $availability = $product->get_availability();
391
392 if ( empty( $availability['availability'] ) ) {
393 $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' );
394 }
395
396 if ( ! empty( $availability['availability'] ) ) {
397 ob_start();
398
399 wc_get_template(
400 'single-product/stock.php',
401 [
402 'product' => $product,
403 'class' => $availability['class'],
404 'availability' => $availability['availability'],
405 ]
406 );
407
408 $html = ob_get_clean();
409 }
410
411 return apply_filters( 'woocommerce_get_stock_html', $html, $product );
412 }
413
414
415 /**
416 * Generate Cart button
417 *
418 * @param WC_Product $product
419 */
420 public function add_to_cart_html( $product ) {
421 if ( ! $product ) {
422 return;
423 }
424
425 $defaults = [
426 'quantity' => 1,
427 'class' => implode(
428 ' ',
429 array_filter(
430 [
431 'htcompare-cart-button button',
432 'product_type_' . $product->get_type(),
433 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
434 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
435 ]
436 )
437 ),
438 'attributes' => [
439 'data-product_id' => $product->get_id(),
440 'data-product_sku' => $product->get_sku(),
441 'aria-label' => $product->add_to_cart_description(),
442 'rel' => 'nofollow',
443 ],
444 ];
445
446 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
447
448 if ( isset( $args['attributes']['aria-label'] ) ) {
449 $args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] );
450 }
451
452 return apply_filters(
453 'woocommerce_loop_add_to_cart_link',
454 sprintf(
455 '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
456 esc_url( $product->add_to_cart_url() ),
457 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
458 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
459 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
460 esc_html( $product->add_to_cart_text() )
461 ),
462 $product,
463 $args
464 );
465 }
466 }
467