assets
2 years ago
comparison-cookie.php
2 years ago
comparison-field-value.php
2 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
342 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 | |
| 268 | if(!$data) $data = []; |
| 269 | foreach ( $data as $product_id => $attribute ) { |
| 270 | ?> |
| 271 | <td class="first--row"> |
| 272 | <?php |
| 273 | foreach ( $attribute as $value ) { |
| 274 | echo wp_kses('<span class="comparison-attribute-badge">' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 275 | } |
| 276 | ?> |
| 277 | </td> |
| 278 | |
| 279 | <?php |
| 280 | } |
| 281 | |
| 282 | $need_tds = count($this->product_ids) - count($data); |
| 283 | |
| 284 | for ($x = 1; $x <= $need_tds; $x++) { |
| 285 | echo '<td class="first--row"></td>'; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | private function print_custom_meta( $slug) { |
| 290 | |
| 291 | foreach ($this->product_ids as $product_id) { |
| 292 | if($product_id) { |
| 293 | $meta_value = get_post_meta( $product_id, $slug, true ); |
| 294 | |
| 295 | echo '<td class="first--row">'; |
| 296 | |
| 297 | if ( gettype( $meta_value ) == 'array' ) { |
| 298 | foreach ( $meta_value as $value ) { |
| 299 | echo wp_kses('<span class="comparison-meta-badge" > ' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 300 | } |
| 301 | } else { |
| 302 | echo esc_html(get_post_meta( $product_id, $slug, true )); |
| 303 | } |
| 304 | |
| 305 | echo '</td>'; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | private function print_color_attribute( $slug, $data ) { |
| 311 | foreach ( $data as $product_id => $datum ) { |
| 312 | ?> |
| 313 | <td class="first--row"> |
| 314 | <?php |
| 315 | |
| 316 | foreach ( $datum as $attribute ) { |
| 317 | foreach ( $attribute['value'] as $value ) { |
| 318 | echo wp_kses('<span class="comparison-color-badge" style="background-color:' . $value . '"></span> ', UtilsHelper::get_kses_array()); |
| 319 | } |
| 320 | } |
| 321 | ?> |
| 322 | </td> |
| 323 | |
| 324 | <?php |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | private function print_tr( $slug, $data ) { |
| 329 | foreach ( $data as $product_id => $datum ) { |
| 330 | if ( $slug == 'dimension' ) { |
| 331 | $datum = wc_format_dimensions( $datum ); |
| 332 | } |
| 333 | ?> |
| 334 | <td class="first--row"> |
| 335 | <?php echo wp_kses($datum, UtilsHelper::get_kses_array()) ?> |
| 336 | </td> |
| 337 | |
| 338 | <?php |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | } |