PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution with eCommerce Templates & Woo Widgets / 3.0.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution with eCommerce Templates & Woo Widgets v3.0.0
4.9.5 4.9.4 4.9.3 4.9.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 3 years ago comparison-cookie.php 3 years ago comparison-field-value.php 3 years 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
290 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 WC_Product;
11
12 class Comparison_Field_Value {
13
14 public $product_ids = [] ;
15 private $generated_attributes = [];
16
17 public function get_value( WC_Product $product, $slug , $data = null) {
18
19 return $this->set_value( $product, $slug, $data);
20 }
21
22 private function set_value( WC_Product $product, $slug, $data = null) {
23 switch ( $slug ) {
24 case 'url':
25 return $product->add_to_cart_url();
26 case 'image':
27 return $product->get_image();
28 case 'title':
29 return $product->get_title();
30 case 'price':
31 $price['regular'] = $product->get_regular_price();
32 $price['sale'] = $product->get_sale_price();
33 $price['price'] = $product->get_price();
34 $price['htm'] = $product->get_price_html();
35
36 return $price;
37 case 'description':
38 return $product->get_description();
39 case 'availability':
40 return $product->get_stock_status();
41 case 'sku':
42 return $product->get_sku();
43 case 'weight':
44 return $product->get_weight();
45 case 'dimension':
46 return $product->get_dimensions( false );
47 case 'attributes':
48 return $this->get_attributes( $product, $data );
49 case 'height':
50 return $product->get_height();
51 }
52
53 return '';
54 }
55
56 public function get_attributes( WC_Product $product , $data = null) {
57
58 if(!$data) $data = [] ;
59
60 $formatted_attributes[$product->get_id()] = [];
61
62 if ( $product->has_attributes() ) {
63
64 $attributes = $product->get_attributes();
65 foreach ( $attributes as $attribute ) {
66 $attribute_name = wc_attribute_label( $attribute->get_name() );
67 $attribute_slug = strtolower(str_replace(' ', '_', $attribute_name));
68 if(in_array($attribute_slug, $data)){
69 $formatted_attributes[$product->get_id()][$attribute_slug] = explode(', ', $product->get_attribute( $attribute_name ) );
70 }
71 }
72
73 }
74
75 return $formatted_attributes;
76 }
77
78 public function get_html( $slug, $data ) {
79 switch ( $slug ) {
80 case 'first_tr':
81 ?>
82 <tr>
83 <th style="vertical-align: middle;"> <?php
84 esc_html_e( 'Product Name', 'shopengine' ) ?> </th>
85 <?php
86 $this->print_first_tr( $data ); ?>
87 </tr>
88
89
90 <?php
91 break;
92 case 'attributes':
93
94 foreach ($data as $slug => $attributes ){
95
96 ?>
97 <tr>
98 <th style="vertical-align: middle;"> <?php
99 echo $slug ?> </th>
100 <?php
101 $this->print_attributes( $slug, $attributes ); ?>
102 </tr>
103 <?php
104 }
105 break;
106 case 'custom_meta':
107
108 foreach ($data as $meta ){
109
110 ?>
111 <tr>
112 <th style="vertical-align: middle;"> <?php
113 echo ucwords( preg_replace( '~([^a-z0-9\-])~i', ' ', $meta ) ) ?> </th>
114 <?php
115 $this->print_custom_meta( $meta ); ?>
116 </tr>
117 <?php
118 }
119 break;
120 case 'color':
121 ?>
122 <tr>
123 <th style="vertical-align: middle;"> <?php
124 echo ucwords( str_replace( '_', ' ', $slug ) ) ?> </th>
125 <?php
126 $this->print_color_attribute( $slug, $data ); ?>
127 </tr>
128 <?php
129 break;
130 default:
131
132 ?>
133 <tr>
134 <th style="vertical-align: middle;"> <?php
135 echo ucwords( str_replace( '_', ' ', $slug ) ) ?> </th>
136 <?php
137 $this->print_tr( $slug, $data ); ?>
138 </tr>
139 <?php
140 break;
141 }
142 }
143
144 public function get_add_to_cart( WC_Product $product , $args = array() ) {
145
146 if ( $product ) {
147 $defaults = array(
148 'quantity' => 1,
149 'class' => implode(
150 ' ',
151 array_filter(
152 array(
153 'compare-cart-btn',
154 'button',
155 'product_type_' . $product->get_type(),
156 $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
157 $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',
158 )
159 )
160 ),
161 'attributes' => array(
162 'data-product_id' => $product->get_id(),
163 'data-product_sku' => $product->get_sku(),
164 'aria-label' => $product->add_to_cart_description(),
165 'rel' => 'nofollow',
166 ),
167 );
168
169 $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );
170
171 if ( isset( $args['attributes']['aria-label'] ) ) {
172 $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );
173 }
174 $cart = esc_html__("Go Cart Page", "shopengine");
175
176 echo apply_filters(
177 'woocommerce_modal_add_to_cart_link', // WPCS: XSS ok.
178 sprintf(
179 '<a title="' . $cart . '" href="%s" data-quantity="%s" class="%s" %s>%s</a>',
180 esc_url( $product->add_to_cart_url() ),
181 esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ),
182 esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ),
183 isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '',
184 esc_html( $product->add_to_cart_text() )
185 ),
186 $product,
187 $args
188 );
189 }
190 }
191
192 private function print_first_tr( $data ) {
193 foreach ( $data['image'] as $product_id => $datum ) {
194 $product = wc_get_product( $product_id );
195 ?>
196 <td class="first--row">
197 <?php
198 echo 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' ) );
199 echo $datum;
200 echo isset($data['title'][ $product_id ]) ? '<h4>'.$data['title'][ $product_id ].'</h4>': '';
201 echo isset($data['price'][ $product_id ]['htm']) ? '<div>'.$data['price'][ $product_id ]['htm'].'</div>': '';
202 ?>
203 <div class="comparison-add-to-cart">
204 <?php $this->get_add_to_cart( $product );?>
205 </div>
206 </td>
207 <?php
208 }
209 }
210
211 private function print_attributes( $slug, $data) {
212
213 if(!$data) $data = [];
214 foreach ( $data as $product_id => $attribute ) {
215 ?>
216 <td class="first--row">
217 <?php
218 foreach ( $attribute as $value ) {
219 echo '<span class="comparison-attribute-badge">' . $value . '</span> ';
220 }
221 ?>
222 </td>
223
224 <?php
225 }
226
227 $need_tds = count($this->product_ids) - count($data);
228
229 for ($x = 1; $x <= $need_tds; $x++) {
230 echo '<td class="first--row"></td>';
231 }
232
233 }
234
235 private function print_custom_meta( $slug) {
236
237 foreach ($this->product_ids as $product_id) {
238 if($product_id) {
239 $meta_value = get_post_meta( $product_id, $slug, true );
240
241 echo '<td class="first--row">';
242
243 if ( gettype( $meta_value ) == 'array' ) {
244 foreach ( $meta_value as $value ) {
245 echo '<span class="comparison-meta-badge" > ' . $value . '</span> ';
246 }
247 } else {
248 echo get_post_meta( $product_id, $slug, true );
249 }
250
251 echo '</td>';
252 }
253 }
254
255 }
256
257 private function print_color_attribute( $slug, $data ) {
258 foreach ( $data as $product_id => $datum ) {
259 ?>
260 <td class="first--row">
261 <?php
262
263 foreach ( $datum as $attribute ) {
264 foreach ( $attribute['value'] as $value ) {
265 echo '<span class="comparison-color-badge" style="background-color:' . $value . '"></span> ';
266 }
267 }
268 ?>
269 </td>
270
271 <?php
272 }
273 }
274
275 private function print_tr( $slug, $data ) {
276 foreach ( $data as $product_id => $datum ) {
277 if ( $slug == 'dimension' ) {
278 $datum = wc_format_dimensions( $datum );
279 }
280 ?>
281 <td class="first--row">
282 <?php
283 echo $datum ?>
284 </td>
285
286 <?php
287 }
288 }
289
290 }