assets
7 months ago
comparison-cookie.php
2 years ago
comparison-field-value.php
10 months 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
370 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 | if(!$data) $data = []; |
| 59 | $product_id = $product->get_id(); |
| 60 | $formatted_attributes[$product_id] = []; |
| 61 | |
| 62 | if (!$product->has_attributes()) return $formatted_attributes; |
| 63 | |
| 64 | foreach ($product->get_attributes() as $attribute) { |
| 65 | $attribute_name = wc_attribute_label($attribute->get_name()); |
| 66 | $attribute_slug = $attribute->is_taxonomy() |
| 67 | ? wc_attribute_taxonomy_slug($attribute->get_name()) |
| 68 | : strtolower(str_replace(' ', '_', $attribute_name)); |
| 69 | |
| 70 | // Find matching slug (exact or partial match) |
| 71 | $matched_slug = $this->find_matching_slug($attribute_slug, $data); |
| 72 | if (!$matched_slug) continue; |
| 73 | |
| 74 | // Use the actual attribute name for display, not the matched slug |
| 75 | $display_key = strtolower(str_replace(' ', '_', $attribute_name)); |
| 76 | |
| 77 | // Get attribute values |
| 78 | if ($attribute->is_taxonomy()) { |
| 79 | $terms = wp_get_post_terms($product_id, $attribute->get_name(), ['fields' => 'names']); |
| 80 | $formatted_attributes[$product_id][$display_key] = !is_wp_error($terms) && !empty($terms) ? $terms : []; |
| 81 | } else { |
| 82 | $values = $product->get_attribute($attribute_name); |
| 83 | $formatted_attributes[$product_id][$display_key] = !empty($values) ? array_map('trim', explode(', ', $values)) : []; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return $formatted_attributes; |
| 88 | } |
| 89 | |
| 90 | private function find_matching_slug($attribute_slug, $data) { |
| 91 | if (empty($data) || !is_array($data)) return null; |
| 92 | |
| 93 | // Exact match first |
| 94 | if (in_array($attribute_slug, $data, true)) return $attribute_slug; |
| 95 | |
| 96 | // Partial match |
| 97 | foreach ($data as $data_slug) { |
| 98 | if (is_string($data_slug) && ( |
| 99 | strpos($attribute_slug, $data_slug) === 0 || |
| 100 | strpos($attribute_slug, $data_slug . '-') !== false |
| 101 | )) { |
| 102 | return $data_slug; |
| 103 | } |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | public function get_html( $slug, $data ) { |
| 109 | switch ( $slug ) { |
| 110 | case 'first_tr': |
| 111 | ?> |
| 112 | <tr> |
| 113 | <th style="vertical-align: middle;"> <?php |
| 114 | esc_html_e( 'Product Name', 'shopengine' ) ?> </th> |
| 115 | <?php |
| 116 | $this->print_first_tr( $data ); ?> |
| 117 | </tr> |
| 118 | |
| 119 | |
| 120 | <?php |
| 121 | break; |
| 122 | case 'attributes': |
| 123 | if (!empty($data) && is_array($data)) { |
| 124 | foreach ($data as $attribute_key => $attributes ){ |
| 125 | // Convert slug back to readable format for display |
| 126 | $display_name = ucwords(str_replace('_', ' ', $attribute_key)); |
| 127 | ?> |
| 128 | <tr> |
| 129 | <th style="vertical-align: middle;"> <?php |
| 130 | echo esc_html($display_name) ?> </th> |
| 131 | <?php |
| 132 | $this->print_attributes( $attribute_key, $attributes ); ?> |
| 133 | </tr> |
| 134 | <?php |
| 135 | } |
| 136 | } |
| 137 | break; |
| 138 | case 'custom_meta': |
| 139 | if (!empty($data) && is_array($data)) { |
| 140 | foreach ($data as $meta ){ |
| 141 | if (!empty($meta)) { |
| 142 | ?> |
| 143 | <tr> |
| 144 | <th style="vertical-align: middle;"> <?php |
| 145 | echo esc_html(ucwords( preg_replace( '~([^a-z0-9\-])~i', ' ', $meta ) )) ?> </th> |
| 146 | <?php |
| 147 | $this->print_custom_meta( $meta ); ?> |
| 148 | </tr> |
| 149 | <?php |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | break; |
| 154 | case 'color': |
| 155 | ?> |
| 156 | <tr> |
| 157 | <th style="vertical-align: middle;"> <?php |
| 158 | echo esc_html(ucwords( str_replace( '_', ' ', $slug ) )) ?> </th> |
| 159 | <?php |
| 160 | $this->print_color_attribute( $slug, $data ); ?> |
| 161 | </tr> |
| 162 | <?php |
| 163 | break; |
| 164 | case 'availability': |
| 165 | ?> |
| 166 | <tr> |
| 167 | <th style="vertical-align: middle;"> <?php |
| 168 | echo esc_html__( 'Availability', 'shopengine' ) ?> </th> |
| 169 | <?php |
| 170 | $this->print_tr( $slug, $data ); ?> |
| 171 | </tr> |
| 172 | <?php |
| 173 | break; |
| 174 | case 'weight': |
| 175 | ?> |
| 176 | <tr> |
| 177 | <th style="vertical-align: middle;"> <?php |
| 178 | echo esc_html__( 'Weight', 'shopengine' ) ?> </th> |
| 179 | <?php |
| 180 | $this->print_tr( $slug, $data ); ?> |
| 181 | </tr> |
| 182 | <?php |
| 183 | break; |
| 184 | case 'description': |
| 185 | ?> |
| 186 | <tr> |
| 187 | <th style="vertical-align: middle;"> <?php |
| 188 | echo esc_html__( 'Description', 'shopengine' ) ?> </th> |
| 189 | <?php |
| 190 | $this->print_tr( $slug, $data ); ?> |
| 191 | </tr> |
| 192 | <?php |
| 193 | break; |
| 194 | case 'height': |
| 195 | ?> |
| 196 | <tr> |
| 197 | <th style="vertical-align: middle;"> <?php |
| 198 | echo esc_html__( 'Height', 'shopengine' ) ?> </th> |
| 199 | <?php |
| 200 | $this->print_tr( $slug, $data ); ?> |
| 201 | </tr> |
| 202 | <?php |
| 203 | break; |
| 204 | case 'dimension': |
| 205 | ?> |
| 206 | <tr> |
| 207 | <th style="vertical-align: middle;"> <?php |
| 208 | echo esc_html__( 'Dimension', 'shopengine' ) ?> </th> |
| 209 | <?php |
| 210 | $this->print_tr( $slug, $data ); ?> |
| 211 | </tr> |
| 212 | <?php |
| 213 | break; |
| 214 | default: |
| 215 | ?> |
| 216 | <tr> |
| 217 | <th style="vertical-align: middle;"> <?php |
| 218 | echo esc_html(ucwords( str_replace( '_', ' ', $slug ) )); |
| 219 | ?> |
| 220 | </th> |
| 221 | <?php |
| 222 | $this->print_tr( $slug, $data ); ?> |
| 223 | </tr> |
| 224 | <?php |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public function get_add_to_cart( WC_Product $product , $args = array() ) { |
| 230 | |
| 231 | if ( $product ) { |
| 232 | $defaults = array( |
| 233 | 'quantity' => 1, |
| 234 | 'class' => implode( |
| 235 | ' ', |
| 236 | array_filter( |
| 237 | array( |
| 238 | 'compare-cart-btn', |
| 239 | 'button', |
| 240 | 'product_type_' . $product->get_type(), |
| 241 | $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', |
| 242 | $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', |
| 243 | ) |
| 244 | ) |
| 245 | ), |
| 246 | 'attributes' => array( |
| 247 | 'data-product_id' => $product->get_id(), |
| 248 | 'data-product_sku' => $product->get_sku(), |
| 249 | 'aria-label' => $product->add_to_cart_description(), |
| 250 | 'rel' => 'nofollow', |
| 251 | ), |
| 252 | ); |
| 253 | |
| 254 | $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product ); |
| 255 | |
| 256 | if ( isset( $args['attributes']['aria-label'] ) ) { |
| 257 | $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); |
| 258 | } |
| 259 | $cart = esc_html__("Go Cart Page", "shopengine"); |
| 260 | |
| 261 | $html = apply_filters( |
| 262 | 'woocommerce_modal_add_to_cart_link', // WPCS: XSS ok. |
| 263 | sprintf( |
| 264 | '<a title="' . $cart . '" href="%s" data-quantity="%s" class="%s" %s>%s</a>', |
| 265 | esc_url( $product->add_to_cart_url() ), |
| 266 | esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), |
| 267 | esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), |
| 268 | isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', |
| 269 | esc_html( $product->add_to_cart_text() ) |
| 270 | ), |
| 271 | $product, |
| 272 | $args |
| 273 | ); |
| 274 | |
| 275 | echo wp_kses($html, UtilsHelper::get_kses_array()); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | private function print_first_tr( $data ) { |
| 280 | foreach ( $data['image'] as $product_id => $datum ) { |
| 281 | $product = wc_get_product( $product_id ); |
| 282 | ?> |
| 283 | <td class="first--row"> |
| 284 | <?php |
| 285 | $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' ) ); |
| 286 | echo wp_kses($remove_button, UtilsHelper::get_kses_array()); |
| 287 | echo wp_kses($datum, UtilsHelper::get_kses_array()); |
| 288 | echo wp_kses((isset($data['title'][ $product_id ]) ? '<h4>'.$data['title'][ $product_id ].'</h4>': ''), UtilsHelper::get_kses_array()); |
| 289 | echo wp_kses((isset($data['price'][ $product_id ]['htm']) ? '<div>'.$data['price'][ $product_id ]['htm'].'</div>': ''), UtilsHelper::get_kses_array()); |
| 290 | ?> |
| 291 | <div class="comparison-add-to-cart"> |
| 292 | <?php $this->get_add_to_cart( $product );?> |
| 293 | </div> |
| 294 | </td> |
| 295 | <?php |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private function print_attributes($slug, $data) { |
| 300 | if (!$data) $data = []; |
| 301 | |
| 302 | foreach ($this->product_ids as $product_id) { |
| 303 | ?> |
| 304 | <td class="first--row"> |
| 305 | <?php |
| 306 | if (!empty($data[$product_id])) { |
| 307 | foreach ($data[$product_id] as $value) { |
| 308 | echo wp_kses('<span class="comparison-attribute-badge">' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 309 | } |
| 310 | } |
| 311 | ?> |
| 312 | </td> |
| 313 | <?php |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | private function print_custom_meta( $slug) { |
| 318 | |
| 319 | foreach ($this->product_ids as $product_id) { |
| 320 | if($product_id) { |
| 321 | $meta_value = get_post_meta( $product_id, $slug, true ); |
| 322 | |
| 323 | echo '<td class="first--row">'; |
| 324 | |
| 325 | if ( gettype( $meta_value ) == 'array' ) { |
| 326 | foreach ( $meta_value as $value ) { |
| 327 | echo wp_kses('<span class="comparison-meta-badge" > ' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 328 | } |
| 329 | } else { |
| 330 | echo esc_html(get_post_meta( $product_id, $slug, true )); |
| 331 | } |
| 332 | |
| 333 | echo '</td>'; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | private function print_color_attribute( $slug, $data ) { |
| 339 | foreach ( $data as $product_id => $datum ) { |
| 340 | ?> |
| 341 | <td class="first--row"> |
| 342 | <?php |
| 343 | |
| 344 | foreach ( $datum as $attribute ) { |
| 345 | foreach ( $attribute['value'] as $value ) { |
| 346 | echo wp_kses('<span class="comparison-color-badge" style="background-color:' . $value . '"></span> ', UtilsHelper::get_kses_array()); |
| 347 | } |
| 348 | } |
| 349 | ?> |
| 350 | </td> |
| 351 | |
| 352 | <?php |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | private function print_tr( $slug, $data ) { |
| 357 | foreach ( $data as $product_id => $datum ) { |
| 358 | if ( $slug == 'dimension' ) { |
| 359 | $datum = wc_format_dimensions( $datum ); |
| 360 | } |
| 361 | ?> |
| 362 | <td class="first--row"> |
| 363 | <?php echo wp_kses($datum, UtilsHelper::get_kses_array()) ?> |
| 364 | </td> |
| 365 | |
| 366 | <?php |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | } |