PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.7.2
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.7.2
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / modules / comparison / comparison-field-value.php
shopengine / modules / comparison Last commit date
assets 2 years ago comparison-cookie.php 2 years ago comparison-field-value.php 1 year ago comparison-helper.php 3 years ago comparison-share.php 3 years ago comparison.php 3 years ago group-meta.php 3 years ago route.php 3 years ago
comparison-field-value.php
337 lines
1 <?php
2
3
4 namespace ShopEngine\Modules\Comparison;
5
6
7 use ShopEngine\Core\Register\Module_List;
8 use ShopEngine\Modules\Swatches\Helper;
9 use ShopEngine\Modules\Swatches\Swatches;
10 use ShopEngine\Utils\Helper as UtilsHelper;
11 use WC_Product;
12
13 class Comparison_Field_Value {
14
15 public $product_ids = [] ;
16 private $generated_attributes = [];
17
18 public function get_value( WC_Product $product, $slug , $data = null) {
19
20 return $this->set_value( $product, $slug, $data);
21 }
22
23 private function set_value( WC_Product $product, $slug, $data = null) {
24 switch ( $slug ) {
25 case 'url':
26 return $product->add_to_cart_url();
27 case 'image':
28 return $product->get_image();
29 case 'title':
30 return $product->get_title();
31 case 'price':
32 $price['regular'] = $product->get_regular_price();
33 $price['sale'] = $product->get_sale_price();
34 $price['price'] = $product->get_price();
35 $price['htm'] = $product->get_price_html();
36
37 return $price;
38 case 'description':
39 return $product->get_description();
40 case 'availability':
41 return $product->get_stock_status();
42 case 'sku':
43 return $product->get_sku();
44 case 'weight':
45 return $product->get_weight();
46 case 'dimension':
47 return $product->get_dimensions( false );
48 case 'attributes':
49 return $this->get_attributes( $product, $data );
50 case 'height':
51 return $product->get_height();
52 }
53
54 return '';
55 }
56
57 public function get_attributes( WC_Product $product , $data = null) {
58
59 if(!$data) $data = [] ;
60
61 $formatted_attributes[$product->get_id()] = [];
62
63 if ( $product->has_attributes() ) {
64
65 $attributes = $product->get_attributes();
66 foreach ( $attributes as $attribute ) {
67 $attribute_name = wc_attribute_label( $attribute->get_name() );
68 $attribute_slug = strtolower(str_replace(' ', '_', $attribute_name));
69 if(in_array($attribute_slug, $data)){
70 $formatted_attributes[$product->get_id()][$attribute_slug] = explode(', ', $product->get_attribute( $attribute_name ) );
71 }
72 }
73
74 }
75
76 return $formatted_attributes;
77 }
78
79 public function get_html( $slug, $data ) {
80 switch ( $slug ) {
81 case 'first_tr':
82 ?>
83 <tr>
84 <th style="vertical-align: middle;"> <?php
85 esc_html_e( 'Product Name', 'shopengine' ) ?> </th>
86 <?php
87 $this->print_first_tr( $data ); ?>
88 </tr>
89
90
91 <?php
92 break;
93 case 'attributes':
94
95 foreach ($data as $slug => $attributes ){
96
97 ?>
98 <tr>
99 <th style="vertical-align: middle;"> <?php
100 echo esc_html($slug) ?> </th>
101 <?php
102 $this->print_attributes( $slug, $attributes ); ?>
103 </tr>
104 <?php
105 }
106 break;
107 case 'custom_meta':
108
109 foreach ($data as $meta ){
110
111 ?>
112 <tr>
113 <th style="vertical-align: middle;"> <?php
114 echo esc_html(ucwords( preg_replace( '~([^a-z0-9\-])~i', ' ', $meta ) )) ?> </th>
115 <?php
116 $this->print_custom_meta( $meta ); ?>
117 </tr>
118 <?php
119 }
120 break;
121 case 'color':
122 ?>
123 <tr>
124 <th style="vertical-align: middle;"> <?php
125 echo esc_html(ucwords( str_replace( '_', ' ', $slug ) )) ?> </th>
126 <?php
127 $this->print_color_attribute( $slug, $data ); ?>
128 </tr>
129 <?php
130 break;
131 case 'availability':
132 ?>
133 <tr>
134 <th style="vertical-align: middle;"> <?php
135 echo esc_html__( 'Availability', 'shopengine' ) ?> </th>
136 <?php
137 $this->print_tr( $slug, $data ); ?>
138 </tr>
139 <?php
140 break;
141 case 'weight':
142 ?>
143 <tr>
144 <th style="vertical-align: middle;"> <?php
145 echo esc_html__( 'Weight', 'shopengine' ) ?> </th>
146 <?php
147 $this->print_tr( $slug, $data ); ?>
148 </tr>
149 <?php
150 break;
151 case 'description':
152 ?>
153 <tr>
154 <th style="vertical-align: middle;"> <?php
155 echo esc_html__( 'Description', 'shopengine' ) ?> </th>
156 <?php
157 $this->print_tr( $slug, $data ); ?>
158 </tr>
159 <?php
160 break;
161 case 'height':
162 ?>
163 <tr>
164 <th style="vertical-align: middle;"> <?php
165 echo esc_html__( 'Height', 'shopengine' ) ?> </th>
166 <?php
167 $this->print_tr( $slug, $data ); ?>
168 </tr>
169 <?php
170 break;
171 case 'dimension':
172 ?>
173 <tr>
174 <th style="vertical-align: middle;"> <?php
175 echo esc_html__( 'Dimension', 'shopengine' ) ?> </th>
176 <?php
177 $this->print_tr( $slug, $data ); ?>
178 </tr>
179 <?php
180 break;
181 default:
182 ?>
183 <tr>
184 <th style="vertical-align: middle;"> <?php
185 echo esc_html(ucwords( str_replace( '_', ' ', $slug ) ));
186 ?>
187 </th>
188 <?php
189 $this->print_tr( $slug, $data ); ?>
190 </tr>
191 <?php
192 break;
193 }
194 }
195
196 public function get_add_to_cart( WC_Product $product , $args = array() ) {
197
198 if ( $product ) {
199 $defaults = array(
200 'quantity' => 1,
201 'class' => implode(
202 ' ',
203 array_filter(
204 array(
205 'compare-cart-btn',
206 'button',
207 'product_type_' . $product->get_type(),
208 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
209 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
210 )
211 )
212 ),
213 'attributes' => array(
214 'data-product_id' => $product->get_id(),
215 'data-product_sku' => $product->get_sku(),
216 'aria-label' => $product->add_to_cart_description(),
217 'rel' => 'nofollow',
218 ),
219 );
220
221 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
222
223 if ( isset( $args['attributes']['aria-label'] ) ) {
224 $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
225 }
226 $cart = esc_html__("Go Cart Page", "shopengine");
227
228 $html = apply_filters(
229 'woocommerce_modal_add_to_cart_link', // WPCS: XSS ok.
230 sprintf(
231 '<a title="' . $cart . '" href="%s" data-quantity="%s" class="%s" %s>%s</a>',
232 esc_url( $product->add_to_cart_url() ),
233 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
234 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
235 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
236 esc_html( $product->add_to_cart_text() )
237 ),
238 $product,
239 $args
240 );
241
242 echo wp_kses($html, UtilsHelper::get_kses_array());
243 }
244 }
245
246 private function print_first_tr( $data ) {
247 foreach ( $data['image'] as $product_id => $datum ) {
248 $product = wc_get_product( $product_id );
249 ?>
250 <td class="first--row">
251 <?php
252 $remove_button = sprintf( '<a class="shopengine-remove-action badge-comparison" data-pid="%s"><i class="eicon-close"></i> %s</a>', esc_attr( $product_id ), esc_html__( 'Remove', 'shopengine' ) );
253 echo wp_kses($remove_button, UtilsHelper::get_kses_array());
254 echo wp_kses($datum, UtilsHelper::get_kses_array());
255 echo wp_kses((isset($data['title'][ $product_id ]) ? '<h4>'.$data['title'][ $product_id ].'</h4>': ''), UtilsHelper::get_kses_array());
256 echo wp_kses((isset($data['price'][ $product_id ]['htm']) ? '<div>'.$data['price'][ $product_id ]['htm'].'</div>': ''), UtilsHelper::get_kses_array());
257 ?>
258 <div class="comparison-add-to-cart">
259 <?php $this->get_add_to_cart( $product );?>
260 </div>
261 </td>
262 <?php
263 }
264 }
265
266 private function print_attributes($slug, $data) {
267 if (!$data) $data = [];
268
269 foreach ($this->product_ids as $product_id) {
270 ?>
271 <td class="first--row">
272 <?php
273 if (!empty($data[$product_id])) {
274 foreach ($data[$product_id] as $value) {
275 echo wp_kses('<span class="comparison-attribute-badge">' . $value . '</span> ', UtilsHelper::get_kses_array());
276 }
277 }
278 ?>
279 </td>
280 <?php
281 }
282 }
283
284 private function print_custom_meta( $slug) {
285
286 foreach ($this->product_ids as $product_id) {
287 if($product_id) {
288 $meta_value = get_post_meta( $product_id, $slug, true );
289
290 echo '<td class="first--row">';
291
292 if ( gettype( $meta_value ) == 'array' ) {
293 foreach ( $meta_value as $value ) {
294 echo wp_kses('<span class="comparison-meta-badge" > ' . $value . '</span> ', UtilsHelper::get_kses_array());
295 }
296 } else {
297 echo esc_html(get_post_meta( $product_id, $slug, true ));
298 }
299
300 echo '</td>';
301 }
302 }
303 }
304
305 private function print_color_attribute( $slug, $data ) {
306 foreach ( $data as $product_id => $datum ) {
307 ?>
308 <td class="first--row">
309 <?php
310
311 foreach ( $datum as $attribute ) {
312 foreach ( $attribute['value'] as $value ) {
313 echo wp_kses('<span class="comparison-color-badge" style="background-color:' . $value . '"></span> ', UtilsHelper::get_kses_array());
314 }
315 }
316 ?>
317 </td>
318
319 <?php
320 }
321 }
322
323 private function print_tr( $slug, $data ) {
324 foreach ( $data as $product_id => $datum ) {
325 if ( $slug == 'dimension' ) {
326 $datum = wc_format_dimensions( $datum );
327 }
328 ?>
329 <td class="first--row">
330 <?php echo wp_kses($datum, UtilsHelper::get_kses_array()) ?>
331 </td>
332
333 <?php
334 }
335 }
336
337 }