route.php
314 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Comparison; |
| 4 | |
| 5 | use ShopEngine\Base\Api; |
| 6 | use ShopEngine\Modules\Swatches\Helper; |
| 7 | use ShopEngine\Modules\Swatches\Swatches; |
| 8 | |
| 9 | class Route extends Api { |
| 10 | |
| 11 | public function config() { |
| 12 | |
| 13 | $this->prefix = 'comparison'; |
| 14 | $this->param = ""; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | private function prepare_html($content = []) { |
| 19 | |
| 20 | $attr = []; |
| 21 | $ids = []; |
| 22 | |
| 23 | if(empty($content)) { |
| 24 | |
| 25 | return '<div>'.esc_html__('No product is added for comparison, please add some product to compare', 'shopengine').'</div>'; |
| 26 | } |
| 27 | |
| 28 | foreach($content as $pid) { |
| 29 | |
| 30 | if(empty($pid)) { |
| 31 | continue; |
| 32 | } |
| 33 | |
| 34 | $prod = wc_get_product($pid); |
| 35 | |
| 36 | if(empty($prod)) { |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | $ids[] = $pid; |
| 41 | |
| 42 | $attr['image'][$pid] = $prod->get_image(); |
| 43 | $attr['url'][$pid] = $prod->add_to_cart_url(); |
| 44 | $attr['name'][$pid] = $prod->get_name(); |
| 45 | |
| 46 | |
| 47 | $attr['stock'][$pid] = $prod->get_stock_status(); |
| 48 | $attr['stock2'][$pid] = $prod->get_stock_quantity(); |
| 49 | $attr['sku'][$pid] = $prod->get_sku(); |
| 50 | //$attr['type'][$pid] = $prod->get_type(); |
| 51 | |
| 52 | $attr['price'][$pid]['regular'] = $prod->get_regular_price(); |
| 53 | $attr['price'][$pid]['sale'] = $prod->get_sale_price(); |
| 54 | $attr['price'][$pid]['price'] = $prod->get_price(); |
| 55 | $attr['price'][$pid]['htm'] = $prod->get_price_html(); |
| 56 | |
| 57 | |
| 58 | if($prod->get_rating_counts()) { |
| 59 | |
| 60 | $attr['rating_count'][$pid] = $prod->get_rating_counts(); |
| 61 | } |
| 62 | |
| 63 | if($prod->get_average_rating()) { |
| 64 | |
| 65 | $attr['rating'][$pid] = $prod->get_average_rating(); |
| 66 | } |
| 67 | |
| 68 | if($prod->get_review_count()) { |
| 69 | |
| 70 | $attr['review_count'][$pid] = $prod->get_review_count(); |
| 71 | } |
| 72 | |
| 73 | if($prod->has_weight()) { |
| 74 | |
| 75 | $attr['weight'][$pid] = $prod->get_weight(); |
| 76 | } |
| 77 | |
| 78 | if($prod->has_attributes()) { |
| 79 | |
| 80 | $ats = $prod->get_attributes(); |
| 81 | |
| 82 | foreach($ats as $at) { |
| 83 | |
| 84 | $at_id = $at->get_id(); |
| 85 | $at_id = empty($at_id) ? md5($at->get_name()) : $at_id; |
| 86 | |
| 87 | $assigned_terms = $at->get_options(); |
| 88 | |
| 89 | $attr['attr'][$pid][$at_id]['name'] = wc_attribute_label($at->get_name()); |
| 90 | $attr['attr'][$pid][$at_id]['pa_name'] = $at->get_name(); |
| 91 | $attr['attr'][$pid][$at_id]['txo'] = ''; |
| 92 | |
| 93 | $attr['attr_loop'][$at_id]['nm'] = $attr['attr'][$pid][$at_id]['name']; |
| 94 | $attr['attr_loop'][$at_id]['pid'][$pid] = $pid; |
| 95 | |
| 96 | if($at->is_taxonomy()) { |
| 97 | |
| 98 | $txo_meta = Helper::get_tax_attribute($at->get_name()); |
| 99 | |
| 100 | $attr['attr'][$pid][$at_id]['txo'] = $txo_meta->attribute_type; |
| 101 | |
| 102 | foreach($assigned_terms as $assigned_term) { |
| 103 | |
| 104 | $term = get_term_by('id', $assigned_term, $at->get_name()); |
| 105 | |
| 106 | if($txo_meta->attribute_type === Swatches::PA_COLOR) { |
| 107 | |
| 108 | $t_val = get_term_meta($assigned_term, 'shopengine_color', true); |
| 109 | |
| 110 | $attr['attr'][$pid][$at_id]['tval'][$assigned_term] = $t_val; |
| 111 | } |
| 112 | |
| 113 | $attr['attr'][$pid][$at_id]['val'][$assigned_term] = $term->name; |
| 114 | } |
| 115 | |
| 116 | } else { |
| 117 | |
| 118 | foreach($assigned_terms as $idx => $term) { |
| 119 | $attr['attr'][$pid][$at_id]['val'][$idx] = $term; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if($prod->has_dimensions()) { |
| 126 | |
| 127 | $attr['dim'][$pid]['len'] = $prod->get_length(); |
| 128 | $attr['dim'][$pid]['wd'] = $prod->get_width(); |
| 129 | $attr['dim'][$pid]['ht'] = $prod->get_height(); |
| 130 | |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ?> |
| 135 | |
| 136 | <div> |
| 137 | <div class="shopengine-comparison-container"> |
| 138 | <div class="shopengine-comparison"> |
| 139 | <h2><?php echo esc_html__('Product comparison', 'shopengine') ?></h2> |
| 140 | |
| 141 | <div class="comparison-table-wrap"> |
| 142 | |
| 143 | <table class="table table-bordered"> |
| 144 | |
| 145 | <tbody> <?php |
| 146 | |
| 147 | foreach($attr as $key => $info) { ?> |
| 148 | |
| 149 | <tr> <?php |
| 150 | |
| 151 | if($key === 'image') { |
| 152 | |
| 153 | echo '<td> </td>'; |
| 154 | |
| 155 | foreach($ids as $id) { |
| 156 | ?> |
| 157 | <td class="first--row"> |
| 158 | <a class="shopengine-remove-action badge-comparison" data-pid="<?php echo esc_attr( $id ); ?>"><i class="eicon-close"></i></a> |
| 159 | <?php echo empty($info[$id]) ? '' : $info[$id]; ?> |
| 160 | <br> |
| 161 | <a href="<?php echo esc_url($attr['url'][$id]); ?>" class="compare-cart-btn"><?php echo esc_html__('Add to cart', 'shopengine'); ?></a> |
| 162 | </td> |
| 163 | <?php |
| 164 | } |
| 165 | |
| 166 | } elseif( |
| 167 | $key === 'name' || |
| 168 | $key === 'rating' || |
| 169 | $key === 'stock' || |
| 170 | $key === 'weight' || |
| 171 | $key === 'sku' |
| 172 | ) { |
| 173 | |
| 174 | echo '<th>' . ucfirst($key) . ' </th>'; |
| 175 | |
| 176 | foreach($ids as $id) { |
| 177 | |
| 178 | echo '<td> ' . (empty($info[$id]) ? ' - ' : $info[$id]) . ' </td>'; |
| 179 | } |
| 180 | |
| 181 | } elseif($key === 'price') { |
| 182 | |
| 183 | echo '<th>' . ucfirst($key) . ' </th>'; |
| 184 | |
| 185 | foreach($ids as $id) { |
| 186 | |
| 187 | echo '<td><span class="price">' . (empty($info[$id]['htm']) ? '' : $info[$id]['htm']) . '</span></td>'; |
| 188 | } |
| 189 | |
| 190 | } elseif($key === 'dim') { |
| 191 | |
| 192 | echo '<th> ' . esc_html__('Dimensions', 'shopengine') . ' </th>'; |
| 193 | |
| 194 | foreach($ids as $id) { |
| 195 | |
| 196 | if(!isset($info[$id])) { |
| 197 | |
| 198 | echo '<td> </td>'; |
| 199 | |
| 200 | continue; |
| 201 | } |
| 202 | |
| 203 | echo '<td>' . implode(' * ', $info[$id]) . '</td>'; |
| 204 | } |
| 205 | |
| 206 | } elseif($key === 'attr') { |
| 207 | |
| 208 | |
| 209 | } ?> |
| 210 | |
| 211 | </tr> <?php |
| 212 | } ?> |
| 213 | |
| 214 | <?php |
| 215 | |
| 216 | if(!empty($attr['attr'])) { |
| 217 | |
| 218 | echo '<tr><th colspan="' . (count($ids) + 1) . '">' . esc_html__('Available options', 'shopengine') . ' :</th></tr>'; |
| 219 | |
| 220 | foreach($attr['attr_loop'] as $atr_id => $a_inf) { |
| 221 | |
| 222 | echo '<tr>'; |
| 223 | echo '<th>' . ($a_inf['nm']) . '</th>'; |
| 224 | |
| 225 | foreach($ids as $id) { |
| 226 | |
| 227 | if(isset($attr['attr'][$id][$atr_id])) { |
| 228 | |
| 229 | if($attr['attr'][$id][$atr_id]['txo'] === Swatches::PA_COLOR) { |
| 230 | |
| 231 | $div = ''; |
| 232 | |
| 233 | foreach($attr['attr'][$id][$atr_id]['tval'] as $tid => $t_vl) { |
| 234 | $div .= '<span class="shopengine-comparison-color-attr" |
| 235 | style="background-color: ' . esc_attr($t_vl) . ';"></span>'; |
| 236 | } |
| 237 | |
| 238 | echo '<td> ' . $div . ' </td>'; |
| 239 | |
| 240 | } elseif($attr['attr'][$id][$atr_id]['pa_name'] === 'pa_color') { |
| 241 | |
| 242 | /** |
| 243 | * todo - Should we give the color div for woocom default color? |
| 244 | * In that case how do we validate it is a valid color name? |
| 245 | * |
| 246 | */ |
| 247 | echo '<td> ' . implode(', ', $attr['attr'][$id][$atr_id]['val']) . ' </td>'; |
| 248 | |
| 249 | } else { |
| 250 | |
| 251 | echo '<td> ' . implode(', ', $attr['attr'][$id][$atr_id]['val']) . ' </td>'; |
| 252 | } |
| 253 | |
| 254 | } else { |
| 255 | echo '<td> - </td>'; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | echo '</tr>'; |
| 260 | } |
| 261 | } ?> |
| 262 | |
| 263 | </tbody> |
| 264 | </table> |
| 265 | </div> |
| 266 | </div> |
| 267 | </div> |
| 268 | </div> |
| 269 | <?php |
| 270 | |
| 271 | } |
| 272 | |
| 273 | public function get_comparison_table() { |
| 274 | |
| 275 | $data = $this->request->get_params(); |
| 276 | $idd = $data['pid']; |
| 277 | |
| 278 | $cck = empty($_COOKIE[Comparison::COOKIE_KEY]) ? '' : $_COOKIE[Comparison::COOKIE_KEY]; |
| 279 | $cck = explode(',', $cck); |
| 280 | $content = array_combine($cck, $cck); |
| 281 | $content[$idd] = $idd; |
| 282 | |
| 283 | unset($content['']); |
| 284 | |
| 285 | $val = implode(',', $content); |
| 286 | |
| 287 | setcookie(Comparison::COOKIE_KEY, $val, strtotime('+' . Comparison::COOKIE_TIME_IN_DAYS . ' days'), '/'); |
| 288 | |
| 289 | $this->prepare_html($content); |
| 290 | |
| 291 | exit(); |
| 292 | } |
| 293 | |
| 294 | public function post_remove() { |
| 295 | |
| 296 | $data = $this->request->get_params(); |
| 297 | $idd = $data['pid']; |
| 298 | |
| 299 | $cck = empty($_COOKIE[Comparison::COOKIE_KEY]) ? '' : $_COOKIE[Comparison::COOKIE_KEY]; |
| 300 | $cck = explode(',', $cck); |
| 301 | $content = array_combine($cck, $cck); |
| 302 | |
| 303 | unset($content[$idd], $content['']); |
| 304 | |
| 305 | $val = implode(',', $content); |
| 306 | |
| 307 | setcookie(Comparison::COOKIE_KEY, $val, strtotime('+' . Comparison::COOKIE_TIME_IN_DAYS . ' days'), '/'); |
| 308 | |
| 309 | $this->prepare_html($content); |
| 310 | |
| 311 | exit(); |
| 312 | } |
| 313 | } |
| 314 |