PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 1.1.4
ShopBuilder – WooCommerce Builder For Elementor v1.1.4
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
← All changes | app/Modules/Compare/CompareFns.php +414 -421 2.1.111.1.4 View file →
@@ -1,421 +1,414 @@
1 -<?php
2 -
3 -namespace RadiusTheme\SB\Modules\Compare;
4 -
5 -use RadiusTheme\SB\Helpers\Fns;
6 -use RadiusTheme\SB\Traits\SingletonTrait;
7 -use WP_Error;
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 CompareFns {
15 -
16 - use SingletonTrait;
17 -
18 - /**
19 - * @return false|string
20 - */
21 - public function get_page_url() {
22 - $page_id = Fns::get_option( 'modules', 'compare', 'page' );
23 - return get_permalink( $page_id );
24 - }
25 -
26 - /**
27 - * Get Fields List
28 - *
29 - * @return array
30 - */
31 - static function get_available_list_fields() {
32 - $attribute_list = [];
33 -
34 - if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
35 - $attribute_list = wc_get_attribute_taxonomies();
36 - }
37 -
38 - $fields = self::get_list_default_fields();
39 -
40 - if ( count( $attribute_list ) > 0 ) {
41 - foreach ( $attribute_list as $attribute ) {
42 - $fields[ 'pa_' . $attribute->attribute_name ] = $attribute->attribute_label;
43 - }
44 - }
45 -
46 - return $fields;
47 - }
48 -
49 -
50 - /**
51 - * Get default fields List
52 - * return array
53 - */
54 - static function get_list_default_fields() {
55 - $fields = [
56 - 'title' => esc_html__( 'Title', 'shopbuilder' ),
57 - 'rating' => esc_html__( 'Rating', 'shopbuilder' ),
58 - 'price' => esc_html__( 'Price', 'shopbuilder' ),
59 - 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ),
60 - 'description' => esc_html__( 'Description', 'shopbuilder' ),
61 - 'availability' => esc_html__( 'Availability', 'shopbuilder' ),
62 - 'sku' => esc_html__( 'Sku', 'shopbuilder' ),
63 - 'weight' => esc_html__( 'Weight', 'shopbuilder' ),
64 - 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ),
65 - ];
66 -
67 - return apply_filters( 'rtsb/module/compare/table_default_fields', $fields );
68 - }
69 -
70 -
71 - /**
72 - * Table field list
73 - *
74 - * @return array Table Field list
75 - */
76 - static function get_list_field_ids() {
77 -
78 - $default_show = array_keys( self::get_list_default_fields() );
79 - $fields_settings = Fns::get_option( 'modules', 'compare', 'page_show_fields', [] );
80 - if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
81 - $fields = $fields_settings;
82 - } else {
83 - $fields = $default_show;
84 - }
85 -
86 - return $fields;
87 - }
88 -
89 - /**
90 - * @param $field
91 - *
92 - * @return mixed|string|void
93 - */
94 - public function field_name( $field ) {
95 -
96 - if ( empty( $field ) ) {
97 - return;
98 - }
99 -
100 - $default = self::get_list_default_fields();
101 -
102 - $str = substr( $field, 0, 3 );
103 - if ( 'pa_' === $str ) {
104 - $field_name = wc_attribute_label( $field );
105 - } else {
106 - $field_name = isset( $default[ $field ] ) ? $default[ $field ] : '';
107 - }
108 -
109 - return $field_name;
110 - }
111 -
112 - public function display_field( $field_id, $product ) {
113 -
114 - $type = $field_id;
115 - if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
116 - $type = 'attribute';
117 - }
118 - switch ( $type ) {
119 - case 'primary':
120 - ?>
121 - <div class="rtsb-compare-primary-content-area">
122 - <a href="#" class="rtsb-compare-remove" data-product_id="<?php echo absint( $product['id'] ); ?>">
123 - <i class="rtsb-icon rtsb-icon-trash-empty"></i>
124 - </a>
125 - <a href="<?php echo esc_url( get_permalink( $product['id'] ) ); ?>" class="rtsb-compare-product-image">
126 - <?php
127 - if ( ! empty( $product['primary']['image'] ) ) {
128 - echo wp_kses( $product['primary']['image'], Fns::allowedHtml( 'image' ) );
129 - }
130 - ?>
131 - </a>
132 - </div>
133 - <?php
134 - break;
135 -
136 - case 'title':
137 - echo '<a href="' . esc_url( get_permalink( $product['id'] ) ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
138 - break;
139 -
140 - case 'rating':
141 - echo '<span class="rtsb-compare-product-rating">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
142 - break;
143 -
144 - case 'price':
145 - echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>';
146 - break;
147 -
148 - case 'add_to_cart':
149 - Fns::print_html( apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] ) );
150 - break;
151 -
152 - case 'weight':
153 - if ( $product[ $field_id ] ) {
154 - $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
155 - Fns::print_html( wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ) );
156 - }
157 - break;
158 -
159 - case 'description':
160 - Fns::print_html( apply_filters( 'woocommerce_short_description', $product[ $field_id ] ) );
161 - break;
162 -
163 - default:
164 - echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : '';
165 - break;
166 - }
167 - }
168 -
169 - public function get_products_data() {
170 - $ids = $this->get_compared_product_ids();
171 -
172 - // For shareable link
173 - if ( empty( $ids ) ) {
174 - return [];
175 - }
176 -
177 - $args = [
178 - 'include' => $ids,
179 - ];
180 -
181 - $products = wc_get_products( $args );
182 -
183 - $products_data = [];
184 -
185 - $field_ids = $this->get_list_field_ids();
186 -
187 - $tax_field_ids = array_filter(
188 - $field_ids,
189 - function ( $field_id ) {
190 - return 'pa_' === substr( $field_id, 0, 3 );
191 - },
192 - ARRAY_FILTER_USE_KEY
193 - );
194 -
195 - $data_none = '-';
196 -
197 - foreach ( $products as $product ) {
198 -
199 - $rating_count = $product->get_rating_count();
200 - $average = $product->get_average_rating();
201 -
202 - $products_data[ $product->get_id() ] = [
203 - 'primary' => [
204 - 'image' => $product->get_image() ? $product->get_image( 'ever-compare-image' ) : $data_none,
205 - ],
206 - 'id' => $product->get_id(),
207 - 'title' => $product->get_title() ? $product->get_title() : $data_none,
208 - 'image_id' => $product->get_image_id(),
209 - 'permalink' => $product->get_permalink(),
210 - 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
211 - 'rating' => wc_get_rating_html( $average, $rating_count ),
212 - 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
213 - 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
214 - 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
215 - 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
216 - 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
217 - 'availability' => $this->availability_html( $product ),
218 - ];
219 -
220 - if ( ! empty( $tax_field_ids ) ) {
221 - foreach ( $tax_field_ids as $field_id ) {
222 - if ( taxonomy_exists( $field_id ) ) {
223 - $products_data[ $product->get_id() ][ $field_id ] = [];
224 - $terms = get_the_terms( $product->get_id(), $field_id );
225 - if ( ! empty( $terms ) ) {
226 - foreach ( $terms as $term ) {
227 - $term = sanitize_term( $term, $field_id );
228 - $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
229 - }
230 - } else {
231 - $products_data[ $product->get_id() ][ $field_id ][] = '-';
232 - }
233 - $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
234 - }
235 - }
236 - }
237 - }
238 -
239 - return $products_data;
240 - }
241 -
242 - /**
243 - * @param $product
244 - *
245 - * @return mixed|void|null
246 - */
247 - public function add_to_cart_html( $product ) {
248 -
249 - $defaults = [
250 - 'quantity' => 1,
251 - 'class' => implode(
252 - ' ',
253 - array_filter(
254 - [
255 - 'rtsb-compare-cart-button button',
256 - 'product_type_' . $product->get_type(),
257 - $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
258 - $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
259 - ]
260 - )
261 - ),
262 - 'attributes' => [
263 - 'data-product_id' => $product->get_id(),
264 - 'data-product_sku' => $product->get_sku(),
265 - 'aria-label' => $product->add_to_cart_description(),
266 - 'rel' => 'nofollow',
267 - ],
268 - ];
269 -
270 - $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
271 -
272 - if ( isset( $args['attributes']['aria-label'] ) ) {
273 - $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
274 - }
275 -
276 - return apply_filters(
277 - 'woocommerce_loop_add_to_cart_link',
278 - sprintf(
279 - '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
280 - esc_url( $product->add_to_cart_url() ),
281 - esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
282 - esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
283 - isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
284 - esc_html( $product->add_to_cart_text() )
285 - ),
286 - $product,
287 - $args
288 - );
289 - }
290 -
291 - public function availability_html( $product ) {
292 - $html = '';
293 - $availability = $product->get_availability();
294 -
295 - if ( empty( $availability['availability'] ) ) {
296 - $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' );
297 - }
298 -
299 - if ( ! empty( $availability['availability'] ) ) {
300 - ob_start();
301 -
302 - wc_get_template(
303 - 'single-product/stock.php',
304 - [
305 - 'product' => $product,
306 - 'class' => $availability['class'],
307 - 'availability' => $availability['availability'],
308 - ]
309 - );
310 -
311 - $html = ob_get_clean();
312 - }
313 -
314 - return apply_filters( 'woocommerce_get_stock_html', $html, $product );
315 - }
316 -
317 - public function add_product( $id ) {
318 - $cookie_name = $this->get_cookie_name();
319 -
320 - if ( $this->is_exists_in_list( $id ) ) {
321 - return new \WP_Error( 'rtsb-compare-product-exist', esc_html__( 'Product already in compare list!.', 'shopbuilder' ) );
322 - }
323 -
324 - $product_ids = $this->get_compared_product_ids();
325 -
326 - // Reached Maximum Limit
327 - if ( $this->reached_max_limit() ) {
328 - return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) );
329 - }
330 -
331 - $product_ids[] = $id;
332 -
333 - setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
334 -
335 - $_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids );
336 -
337 - return true;
338 - }
339 -
340 - /**
341 - * Remove product from compare list
342 - *
343 - * @param integer $product_id
344 - *
345 - * @return bool|WP_Error
346 - */
347 - public function remove_product( $product_id ) {
348 -
349 - if ( ! $this->is_exists_in_list( $product_id ) ) {
350 - return new WP_Error( 'rtsb_compare_product_not_exist', esc_html__( 'Product not in compare list!.', 'shopbuilder' ) );
351 - }
352 - $delete_status = false;
353 - $product_ids = $this->get_compared_product_ids();
354 - if ( is_array( $product_ids ) && ! empty( $product_ids ) ) {
355 - foreach ( $product_ids as $prod_key => $productId ) {
356 - if ( $productId == $product_id ) {
357 - unset( $product_ids[ $prod_key ] );
358 - $delete_status = true;
359 - break;
360 - }
361 - }
362 - }
363 -
364 - $cookie_name = $this->get_cookie_name();
365 -
366 - setcookie( $cookie_name, wp_json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
367 - $_COOKIE[ $cookie_name ] = wp_json_encode( $product_ids );
368 -
369 - return $delete_status;
370 - }
371 -
372 - public function get_cookie_name() {
373 - $name = 'rtsb_compare_list';
374 - if ( is_multisite() ) {
375 - $name .= '_' . get_current_blog_id();
376 - }
377 -
378 - return $name;
379 - }
380 -
381 - /**
382 - * @param $id
383 - *
384 - * @return bool
385 - */
386 - public function is_exists_in_list( $id ) {
387 - $list = $this->get_compared_product_ids();
388 -
389 - return in_array( $id, $list );
390 - }
391 -
392 - public function get_compared_product_ids() {
393 - $cookie_name = $this->get_cookie_name();
394 - $ids = [];
395 - if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
396 - $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
397 - }
398 -
399 - return $ids;
400 - }
401 -
402 - public function reached_max_limit() {
403 -
404 - $product_ids = $this->get_compared_product_ids();
405 - $max_limit = absint( Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' ) );
406 -
407 - // Remove if product is not exist.
408 - foreach ( $product_ids as $id ) {
409 - if ( 'publish' !== get_post_status( $id ) ) {
410 - $this->remove_product( $id );
411 - unset( $product_ids[ $id ] );
412 - }
413 - }
414 -
415 - if ( $max_limit && count( $product_ids ) >= $max_limit ) {
416 - return true;
417 - }
418 -
419 - return false;
420 - }
421 -}
1 +<?php
2 +
3 +namespace RadiusTheme\SB\Modules\Compare;
4 +
5 +use RadiusTheme\SB\Helpers\Fns;
6 +use RadiusTheme\SB\Traits\SingletonTrait;
7 +use WP_Error;
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 CompareFns {
15 +
16 + use SingletonTrait;
17 +
18 + /**
19 + * @return false|string
20 + */
21 + public function get_page_url() {
22 + $page_id = Fns::get_option( 'modules', 'compare', 'page' );
23 + return get_permalink( $page_id );
24 + }
25 +
26 + /**
27 + * Get Fields List
28 + *
29 + * @return array
30 + */
31 + static function get_available_list_fields() {
32 + $attribute_list = [];
33 +
34 + if ( function_exists( 'wc_get_attribute_taxonomies' ) ) {
35 + $attribute_list = wc_get_attribute_taxonomies();
36 + }
37 +
38 + $fields = self::get_list_default_fields();
39 +
40 + if ( count( $attribute_list ) > 0 ) {
41 + foreach ( $attribute_list as $attribute ) {
42 + $fields[ 'pa_' . $attribute->attribute_name ] = $attribute->attribute_label;
43 + }
44 + }
45 +
46 + return $fields;
47 + }
48 +
49 +
50 + /**
51 + * Get default fields List
52 + * return array
53 + */
54 + static function get_list_default_fields() {
55 + $fields = [
56 + 'title' => esc_html__( 'Title', 'shopbuilder' ),
57 + 'rating' => esc_html__( 'Rating', 'shopbuilder' ),
58 + 'price' => esc_html__( 'Price', 'shopbuilder' ),
59 + 'add_to_cart' => esc_html__( 'Add To Cart', 'shopbuilder' ),
60 + 'description' => esc_html__( 'Description', 'shopbuilder' ),
61 + 'availability' => esc_html__( 'Availability', 'shopbuilder' ),
62 + 'sku' => esc_html__( 'Sku', 'shopbuilder' ),
63 + 'weight' => esc_html__( 'Weight', 'shopbuilder' ),
64 + 'dimensions' => esc_html__( 'Dimensions', 'shopbuilder' ),
65 + ];
66 +
67 + return apply_filters( 'rtsb/module/compare/table_default_fields', $fields );
68 + }
69 +
70 +
71 + /**
72 + * Table field list
73 + *
74 + * @return array Table Field list
75 + */
76 + static function get_list_field_ids() {
77 +
78 + $default_show = array_keys( self::get_list_default_fields() );
79 + $fields_settings = Fns::get_option( 'modules', 'compare', 'page_show_fields', [] );
80 + if ( isset( $fields_settings ) && ( is_array( $fields_settings ) ) && count( $fields_settings ) > 1 ) {
81 + $fields = $fields_settings;
82 + } else {
83 + $fields = $default_show;
84 + }
85 +
86 + return $fields;
87 + }
88 +
89 + /**
90 + * @param $field
91 + *
92 + * @return mixed|string|void
93 + */
94 + public function field_name( $field ) {
95 +
96 + if ( empty( $field ) ) {
97 + return;
98 + }
99 +
100 + $default = self::get_list_default_fields();
101 +
102 + $str = substr( $field, 0, 3 );
103 + if ( 'pa_' === $str ) {
104 + $field_name = wc_attribute_label( $field );
105 + } else {
106 + $field_name = isset( $default[ $field ] ) ? $default[ $field ] : '';
107 + }
108 +
109 + return $field_name;
110 +
111 + }
112 +
113 + public function display_field( $field_id, $product ) {
114 +
115 + $type = $field_id;
116 + if ( 'pa_' === substr( $field_id, 0, 3 ) ) {
117 + $type = 'attribute';
118 + }
119 + switch ( $type ) {
120 + case 'primary':
121 + ?>
122 + <div class="rtsb-compare-primary-content-area">
123 + <a href="#" class="rtsb-compare-remove" data-product_id="<?php echo absint( $product['id'] ); ?>">
124 + <i class="rtsb-icon rtsb-icon-trash-empty"></i>
125 + </a>
126 + <a href="<?php echo get_permalink( $product['id'] ); ?>" class="rtsb-compare-product-image">
127 + <?php
128 + if ( ! empty( $product['primary']['image'] ) ) {
129 + echo wp_kses( $product['primary']['image'], Fns::allowedHtml( 'image' ) );
130 + }
131 + ?>
132 + </a>
133 + </div>
134 + <?php
135 + break;
136 +
137 + case 'title':
138 + echo '<a href="' . get_permalink( $product['id'] ) . '" class="rtsb-compare-product-title">' . esc_html( $product[ $field_id ] ) . '</a>';
139 + break;
140 +
141 + case 'rating':
142 + echo '<span class="rtsb-compare-product-rating">' . wp_kses_post( $product[ $field_id ] ) . '</span>';
143 + break;
144 +
145 + case 'price':
146 + echo '<div class="rtsb-compare-product-price">' . wp_kses_post( $product[ $field_id ] ) . '</div>';
147 + break;
148 +
149 + case 'add_to_cart':
150 + echo apply_filters( 'rtsb/module/compare/add_to_cart_btn', $product[ $field_id ] );
151 + break;
152 +
153 + case 'weight':
154 + if ( $product[ $field_id ] ) {
155 + $unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : '';
156 + echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit );
157 + }
158 + break;
159 +
160 + case 'description':
161 + echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] );
162 + break;
163 +
164 + default:
165 + echo isset( $product[ $field_id ] ) ? wp_kses_post( $product[ $field_id ] ) : '';
166 + break;
167 + }
168 + }
169 +
170 + public function get_products_data() {
171 + $ids = $this->get_compared_product_ids();
172 +
173 + // For shareable link
174 + if ( empty( $ids ) ) {
175 + return [];
176 + }
177 +
178 + $args = [
179 + 'include' => $ids,
180 + ];
181 +
182 + $products = wc_get_products( $args );
183 +
184 + $products_data = [];
185 +
186 + $field_ids = $this->get_list_field_ids();
187 +
188 + $tax_field_ids = array_filter(
189 + $field_ids,
190 + function ( $field_id ) {
191 + return 'pa_' === substr( $field_id, 0, 3 );
192 + },
193 + ARRAY_FILTER_USE_KEY
194 + );
195 +
196 + $data_none = '-';
197 +
198 + foreach ( $products as $product ) {
199 +
200 + $rating_count = $product->get_rating_count();
201 + $average = $product->get_average_rating();
202 +
203 + $products_data[ $product->get_id() ] = [
204 + 'primary' => [
205 + 'image' => $product->get_image() ? $product->get_image( 'ever-compare-image' ) : $data_none,
206 + ],
207 + 'id' => $product->get_id(),
208 + 'title' => $product->get_title() ? $product->get_title() : $data_none,
209 + 'image_id' => $product->get_image_id(),
210 + 'permalink' => $product->get_permalink(),
211 + 'price' => $product->get_price_html() ? $product->get_price_html() : $data_none,
212 + 'rating' => wc_get_rating_html( $average, $rating_count ),
213 + 'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) : $data_none,
214 + 'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ),
215 + 'description' => $product->get_short_description() ? $product->get_short_description() : $data_none,
216 + 'weight' => $product->get_weight() ? $product->get_weight() : $data_none,
217 + 'sku' => $product->get_sku() ? $product->get_sku() : $data_none,
218 + 'availability' => $this->availability_html( $product ),
219 + ];
220 +
221 + if ( ! empty( $tax_field_ids ) ) {
222 + foreach ( $tax_field_ids as $field_id ) {
223 + if ( taxonomy_exists( $field_id ) ) {
224 + $products_data[ $product->get_id() ][ $field_id ] = [];
225 + $terms = get_the_terms( $product->get_id(), $field_id );
226 + if ( ! empty( $terms ) ) {
227 + foreach ( $terms as $term ) {
228 + $term = sanitize_term( $term, $field_id );
229 + $products_data[ $product->get_id() ][ $field_id ][] = $term->name;
230 + }
231 + } else {
232 + $products_data[ $product->get_id() ][ $field_id ][] = '-';
233 + }
234 + $products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] );
235 + }
236 + }
237 + }
238 + }
239 +
240 + return $products_data;
241 + }
242 +
243 + /**
244 + * @param $product
245 + *
246 + * @return mixed|void|null
247 + */
248 + public function add_to_cart_html( $product ) {
249 +
250 + $defaults = [
251 + 'quantity' => 1,
252 + 'class' => implode(
253 + ' ',
254 + array_filter(
255 + [
256 + 'rtsb-compare-cart-button button',
257 + 'product_type_' . $product->get_type(),
258 + $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
259 + $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
260 + ]
261 + )
262 + ),
263 + 'attributes' => [
264 + 'data-product_id' => $product->get_id(),
265 + 'data-product_sku' => $product->get_sku(),
266 + 'aria-label' => $product->add_to_cart_description(),
267 + 'rel' => 'nofollow',
268 + ],
269 + ];
270 +
271 + $args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product );
272 +
273 + if ( isset( $args['attributes']['aria-label'] ) ) {
274 + $args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] );
275 + }
276 +
277 + return apply_filters(
278 + 'woocommerce_loop_add_to_cart_link',
279 + sprintf(
280 + '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>',
281 + esc_url( $product->add_to_cart_url() ),
282 + esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
283 + esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
284 + isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
285 + esc_html( $product->add_to_cart_text() )
286 + ),
287 + $product,
288 + $args
289 + );
290 + }
291 +
292 + public function availability_html( $product ) {
293 + $html = '';
294 + $availability = $product->get_availability();
295 +
296 + if ( empty( $availability['availability'] ) ) {
297 + $availability['availability'] = esc_html__( 'In stock', 'shopbuilder' );
298 + }
299 +
300 + if ( ! empty( $availability['availability'] ) ) {
301 + ob_start();
302 +
303 + wc_get_template(
304 + 'single-product/stock.php',
305 + [
306 + 'product' => $product,
307 + 'class' => $availability['class'],
308 + 'availability' => $availability['availability'],
309 + ]
310 + );
311 +
312 + $html = ob_get_clean();
313 + }
314 +
315 + return apply_filters( 'woocommerce_get_stock_html', $html, $product );
316 + }
317 +
318 + public function add_product( $id ) {
319 + $cookie_name = $this->get_cookie_name();
320 +
321 + if ( $this->is_exists_in_list( $id ) ) {
322 + return new \WP_Error( 'rtsb-compare-product-exist', esc_html__( 'Product already in compare list!.', 'shopbuilder' ) );
323 + }
324 +
325 + $product_ids = $this->get_compared_product_ids();
326 +
327 + // Reached Maximum Limit
328 + if ( $this->reached_max_limit() ) {
329 + return new \WP_Error( 'rtsb-compare-reached_max_limit', esc_html__( 'Reached maximum number of compare!.', 'shopbuilder' ) );
330 + }
331 +
332 + $product_ids[] = $id;
333 +
334 + setcookie( $cookie_name, json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
335 +
336 + $_COOKIE[ $cookie_name ] = json_encode( $product_ids );
337 +
338 + return true;
339 + }
340 +
341 + /**
342 + * Remove product from compare list
343 + *
344 + * @param integer $product_id
345 + *
346 + * @return bool|WP_Error
347 + */
348 + public function remove_product( $product_id ) {
349 +
350 + if ( ! $this->is_exists_in_list( $product_id ) ) {
351 + return new WP_Error( 'rtsb_compare_product_not_exist', esc_html__( 'Product not in compare list!.', 'shopbuilder' ) );
352 + }
353 + $delete_status = false;
354 + $product_ids = $this->get_compared_product_ids();
355 + if ( is_array( $product_ids ) && ! empty( $product_ids ) ) {
356 + foreach ( $product_ids as $prod_key => $productId ) {
357 + if ( $productId == $product_id ) {
358 + unset( $product_ids[ $prod_key ] );
359 + $delete_status = true;
360 + break;
361 + }
362 + }
363 + }
364 +
365 + $cookie_name = $this->get_cookie_name();
366 +
367 + setcookie( $cookie_name, json_encode( $product_ids ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false );
368 + $_COOKIE[ $cookie_name ] = json_encode( $product_ids );
369 +
370 + return $delete_status;
371 + }
372 +
373 + public function get_cookie_name() {
374 + $name = 'rtsb_compare_list';
375 + if ( is_multisite() ) {
376 + $name .= '_' . get_current_blog_id();
377 + }
378 +
379 + return $name;
380 + }
381 +
382 + /**
383 + * @param $id
384 + *
385 + * @return bool
386 + */
387 + public function is_exists_in_list( $id ) {
388 + $list = $this->get_compared_product_ids();
389 +
390 + return in_array( $id, $list );
391 + }
392 +
393 + public function get_compared_product_ids() {
394 + $cookie_name = $this->get_cookie_name();
395 + $ids = [];
396 + if ( isset( $_COOKIE[ $cookie_name ] ) && is_array( json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) ) ) {
397 + $ids = array_map( 'absint', json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) );
398 + }
399 +
400 + return $ids;
401 + }
402 +
403 + public function reached_max_limit() {
404 +
405 + $product_ids = $this->get_compared_product_ids();
406 + $max_limit = Fns::get_option( 'modules', 'compare', 'max_limit', 10, 'checkbox' );
407 + if ( $max_limit && count( $product_ids ) >= $max_limit ) {
408 + return true;
409 + }
410 +
411 + return false;
412 + }
413 +
414 +}