PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
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 / Badges / BadgesFns.php

BadgesFns.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/Modules/Badges/BadgesFns.php

179 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Main FilterHooks class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Modules\Badges;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SBPRO\Modules\PreOrder\PreOrderFns;
12 use WC_Product;
13
14 defined( 'ABSPATH' ) || exit();
15
16 /**
17 * Main FilterHooks class.
18 */
19 class BadgesFns {
20 /**
21 * @var array
22 */
23 private static $cache = [];
24
25 /**
26 * Get product min/max price.
27 *
28 * @return array|false
29 */
30 public static function get_product_badges_list() {
31 $cache_key = 'get_product_badges_list';
32 // Reduce Calculation.
33 if ( isset( self::$cache[ $cache_key ] ) ) {
34 return self::$cache[ $cache_key ];
35 }
36 // If the cached result doesn't exist, fetch it from the database.
37 $options = Fns::get_options( 'modules', 'product_badges' );
38 $badges_field = json_decode( $options['badges_field'] ?? '', true );
39 self::$cache[ $cache_key ] = $badges_field;
40
41 return $badges_field;
42 }
43
44 /**
45 * Get Badges.
46 *
47 * @param object $product Product Object.
48 *
49 * @return array|mixed
50 */
51 public static function get_product_badges_for_current_product( $product = null ) {
52 $available_badges = [];
53
54 if ( is_null( $product ) ) {
55 global $product;
56 }
57
58 if ( ! $product instanceof WC_Product ) {
59 return [];
60 }
61
62 $product_id = $product->get_id();
63 $cache_key = 'badges_cache_for_' . $product_id;
64
65 // Reduce Calculation.
66 if ( isset( self::$cache[ $cache_key ] ) ) {
67 return self::$cache[ $cache_key ];
68 }
69
70 $active_product_badges = self::get_product_badges_list();
71
72 if ( ! is_array( $active_product_badges ) || ! count( $active_product_badges ) ) {
73 self::$cache[ $cache_key ] = [];
74
75 return [];
76 }
77
78 foreach ( $active_product_badges as $index => $badge ) {
79 $product_badges_exclude_for = $badge['exclude_product'] ?? '';
80 $exclude_for = Fns::multiselect_settings_field_value( $product_badges_exclude_for );
81 $exclude_for = array_map( 'absint', $exclude_for );
82
83 if ( count( $exclude_for ) && in_array( $product_id, $exclude_for, true ) ) {
84 continue;
85 }
86 $badge_condition = $badge['badge_condition'] ?? '';
87 if ( 'dynamic' == $badge_condition ) {
88 $badge_for = $badge['badge_for'] ?? '';
89 switch ( $badge_for ) {
90 case 'on_sale':
91 if ( ! $product->is_on_sale() ) {
92 continue 2;
93 }
94 break;
95 case 'featured':
96 if ( ! $product->is_featured() ) {
97 continue 2;
98 }
99 break;
100 case 'out_of_stock':
101 if ( $product->is_in_stock() ) {
102 continue 2;
103 }
104 break;
105 case 'best_selling':
106 $minimum_sale = $badge['minimum_sale_count'] ?? 1;
107 $best_selling = Fns::best_selling_products_ids( $minimum_sale );
108 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
109 if ( ! is_array( $best_selling ) || ! in_array( $product_id, $best_selling ) ) {
110 continue 2;
111 }
112 break;
113 case 'new_arrival':
114 $new_days = $badge['new_arrival_days'] ?? 30;
115 if ( ! Fns::is_new_product( $product, $new_days ) ) {
116 continue 2;
117 }
118 break;
119 case 'pre_order':
120 $is_on_pre_order = rtsb()->has_pro() && ( PreOrderFns::is_on_pre_order( $product ) || PreOrderFns::variation_is_on_pre_order( $product ) );
121 if ( ! ( Fns::is_module_active( 'pre_order' ) && $is_on_pre_order ) ) {
122 continue 2;
123 }
124 break;
125 default:
126 break;
127 }
128 }
129
130 $product_badges_apply_for = $badge['apply_for'] ?? '';
131
132 if ( 'product_cat' === $product_badges_apply_for ) {
133 $term_ids = [];
134 $terms = get_the_terms( $product_id, 'product_cat' );
135
136 if ( ! is_wp_error( $terms ) ) {
137 foreach ( $terms as $term ) {
138 $term_ids[] = $term->term_id;
139 }
140 }
141
142 $applicable_cat = $badge['applicable_categories'] ?? '';
143 $applicable_cat = Fns::multiselect_settings_field_value( $applicable_cat );
144 $applicable_cat = array_map( 'absint', $applicable_cat );
145 $has_cat = array_intersect( $term_ids, $applicable_cat );
146
147 if ( is_array( $applicable_cat ) && count( $applicable_cat ) && ! count( $has_cat ) ) {
148 continue;
149 }
150 } else {
151 $applicable_products = $badge['applicable_products'] ?? '';
152 $applicable_products = Fns::multiselect_settings_field_value( $applicable_products );
153 $applicable_products = array_map( 'absint', $applicable_products );
154
155 if ( is_array( $applicable_products ) && count( $applicable_products ) && ! in_array( $product_id, $applicable_products, true ) ) {
156 continue;
157 }
158 }
159
160 $available_badges[] = $badge;
161 }
162
163 self::$cache[ $cache_key ] = $available_badges;
164
165 return $available_badges;
166 }
167
168 /**
169 * Is allowed badge.
170 *
171 * @return bool
172 */
173 public static function is_allowed_badge_showing() {
174 $badge_applied = self::get_product_badges_for_current_product();
175
176 return is_array( $badge_applied ) && count( $badge_applied );
177 }
178 }
179