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
291 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 | default: |
| 132 | |
| 133 | ?> |
| 134 | <tr> |
| 135 | <th style="vertical-align: middle;"> <?php |
| 136 | echo esc_html(ucwords( str_replace( '_', ' ', $slug ) )) ?> </th> |
| 137 | <?php |
| 138 | $this->print_tr( $slug, $data ); ?> |
| 139 | </tr> |
| 140 | <?php |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public function get_add_to_cart( WC_Product $product , $args = array() ) { |
| 146 | |
| 147 | if ( $product ) { |
| 148 | $defaults = array( |
| 149 | 'quantity' => 1, |
| 150 | 'class' => implode( |
| 151 | ' ', |
| 152 | array_filter( |
| 153 | array( |
| 154 | 'compare-cart-btn', |
| 155 | 'button', |
| 156 | 'product_type_' . $product->get_type(), |
| 157 | $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', |
| 158 | $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', |
| 159 | ) |
| 160 | ) |
| 161 | ), |
| 162 | 'attributes' => array( |
| 163 | 'data-product_id' => $product->get_id(), |
| 164 | 'data-product_sku' => $product->get_sku(), |
| 165 | 'aria-label' => $product->add_to_cart_description(), |
| 166 | 'rel' => 'nofollow', |
| 167 | ), |
| 168 | ); |
| 169 | |
| 170 | $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product ); |
| 171 | |
| 172 | if ( isset( $args['attributes']['aria-label'] ) ) { |
| 173 | $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] ); |
| 174 | } |
| 175 | $cart = esc_html__("Go Cart Page", "shopengine"); |
| 176 | |
| 177 | $html = apply_filters( |
| 178 | 'woocommerce_modal_add_to_cart_link', // WPCS: XSS ok. |
| 179 | sprintf( |
| 180 | '<a title="' . $cart . '" href="%s" data-quantity="%s" class="%s" %s>%s</a>', |
| 181 | esc_url( $product->add_to_cart_url() ), |
| 182 | esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), |
| 183 | esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), |
| 184 | isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', |
| 185 | esc_html( $product->add_to_cart_text() ) |
| 186 | ), |
| 187 | $product, |
| 188 | $args |
| 189 | ); |
| 190 | |
| 191 | echo wp_kses($html, UtilsHelper::get_kses_array()); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private function print_first_tr( $data ) { |
| 196 | foreach ( $data['image'] as $product_id => $datum ) { |
| 197 | $product = wc_get_product( $product_id ); |
| 198 | ?> |
| 199 | <td class="first--row"> |
| 200 | <?php |
| 201 | $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' ) ); |
| 202 | echo wp_kses($remove_button, UtilsHelper::get_kses_array()); |
| 203 | echo wp_kses($datum, UtilsHelper::get_kses_array()); |
| 204 | echo wp_kses((isset($data['title'][ $product_id ]) ? '<h4>'.$data['title'][ $product_id ].'</h4>': ''), UtilsHelper::get_kses_array()); |
| 205 | echo wp_kses((isset($data['price'][ $product_id ]['htm']) ? '<div>'.$data['price'][ $product_id ]['htm'].'</div>': ''), UtilsHelper::get_kses_array()); |
| 206 | ?> |
| 207 | <div class="comparison-add-to-cart"> |
| 208 | <?php $this->get_add_to_cart( $product );?> |
| 209 | </div> |
| 210 | </td> |
| 211 | <?php |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private function print_attributes( $slug, $data) { |
| 216 | |
| 217 | if(!$data) $data = []; |
| 218 | foreach ( $data as $product_id => $attribute ) { |
| 219 | ?> |
| 220 | <td class="first--row"> |
| 221 | <?php |
| 222 | foreach ( $attribute as $value ) { |
| 223 | echo wp_kses('<span class="comparison-attribute-badge">' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 224 | } |
| 225 | ?> |
| 226 | </td> |
| 227 | |
| 228 | <?php |
| 229 | } |
| 230 | |
| 231 | $need_tds = count($this->product_ids) - count($data); |
| 232 | |
| 233 | for ($x = 1; $x <= $need_tds; $x++) { |
| 234 | echo '<td class="first--row"></td>'; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | private function print_custom_meta( $slug) { |
| 239 | |
| 240 | foreach ($this->product_ids as $product_id) { |
| 241 | if($product_id) { |
| 242 | $meta_value = get_post_meta( $product_id, $slug, true ); |
| 243 | |
| 244 | echo '<td class="first--row">'; |
| 245 | |
| 246 | if ( gettype( $meta_value ) == 'array' ) { |
| 247 | foreach ( $meta_value as $value ) { |
| 248 | echo wp_kses('<span class="comparison-meta-badge" > ' . $value . '</span> ', UtilsHelper::get_kses_array()); |
| 249 | } |
| 250 | } else { |
| 251 | echo esc_html(get_post_meta( $product_id, $slug, true )); |
| 252 | } |
| 253 | |
| 254 | echo '</td>'; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | private function print_color_attribute( $slug, $data ) { |
| 260 | foreach ( $data as $product_id => $datum ) { |
| 261 | ?> |
| 262 | <td class="first--row"> |
| 263 | <?php |
| 264 | |
| 265 | foreach ( $datum as $attribute ) { |
| 266 | foreach ( $attribute['value'] as $value ) { |
| 267 | echo wp_kses('<span class="comparison-color-badge" style="background-color:' . $value . '"></span> ', UtilsHelper::get_kses_array()); |
| 268 | } |
| 269 | } |
| 270 | ?> |
| 271 | </td> |
| 272 | |
| 273 | <?php |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | private function print_tr( $slug, $data ) { |
| 278 | foreach ( $data as $product_id => $datum ) { |
| 279 | if ( $slug == 'dimension' ) { |
| 280 | $datum = wc_format_dimensions( $datum ); |
| 281 | } |
| 282 | ?> |
| 283 | <td class="first--row"> |
| 284 | <?php echo wp_kses($datum, UtilsHelper::get_kses_array()) ?> |
| 285 | </td> |
| 286 | |
| 287 | <?php |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | } |