route.php
256 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Comparison; |
| 4 | |
| 5 | use ShopEngine\Base\Api; |
| 6 | |
| 7 | class Route extends Api { |
| 8 | |
| 9 | public function config() { |
| 10 | |
| 11 | $this->prefix = 'comparison'; |
| 12 | $this->param = ""; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | private function prepare_html($content = []) { |
| 17 | |
| 18 | $attr = []; |
| 19 | $ids = []; |
| 20 | |
| 21 | foreach($content as $pid) { |
| 22 | |
| 23 | if(empty($pid)) { |
| 24 | continue; |
| 25 | } |
| 26 | |
| 27 | $prod = wc_get_product($pid); |
| 28 | |
| 29 | if(empty($prod)) { |
| 30 | continue; |
| 31 | } |
| 32 | |
| 33 | $ids[] = $pid; |
| 34 | |
| 35 | $attr['image'][$pid] = $prod->get_image(); |
| 36 | $attr['name'][$pid] = $prod->get_name(); |
| 37 | |
| 38 | |
| 39 | $attr['stock'][$pid] = $prod->get_stock_status(); |
| 40 | $attr['sku'][$pid] = $prod->get_sku(); |
| 41 | //$attr['type'][$pid] = $prod->get_type(); |
| 42 | |
| 43 | $attr['price'][$pid]['regular'] = $prod->get_regular_price(); |
| 44 | $attr['price'][$pid]['sale'] = $prod->get_sale_price(); |
| 45 | $attr['price'][$pid]['price'] = $prod->get_price(); |
| 46 | $attr['price'][$pid]['htm'] = $prod->get_price_html(); |
| 47 | |
| 48 | |
| 49 | if($prod->get_rating_counts()) { |
| 50 | |
| 51 | $attr['rating_count'][$pid] = $prod->get_rating_counts(); |
| 52 | } |
| 53 | |
| 54 | if($prod->get_average_rating()) { |
| 55 | |
| 56 | $attr['rating'][$pid] = $prod->get_average_rating(); |
| 57 | } |
| 58 | |
| 59 | if($prod->get_review_count()) { |
| 60 | |
| 61 | $attr['review_count'][$pid] = $prod->get_review_count(); |
| 62 | } |
| 63 | |
| 64 | if($prod->has_weight()) { |
| 65 | |
| 66 | $attr['weight'][$pid] = $prod->get_weight(); |
| 67 | } |
| 68 | |
| 69 | if($prod->has_attributes()) { |
| 70 | |
| 71 | $ats = $prod->get_attributes(); |
| 72 | |
| 73 | foreach($ats as $at) { |
| 74 | |
| 75 | $attr['attr'][$pid][$at->get_id()]['name'] = wc_attribute_label($at->get_name()); |
| 76 | |
| 77 | if($at->is_taxonomy()) { |
| 78 | |
| 79 | $terms = get_terms($at->get_name()); |
| 80 | foreach($terms as $term) { |
| 81 | $attr['attr'][$pid][$at->get_id()]['val'][$term->term_id] = $term->name; |
| 82 | } |
| 83 | |
| 84 | } else { |
| 85 | |
| 86 | $terms = $at->get_options(); |
| 87 | |
| 88 | foreach($terms as $idx => $term) { |
| 89 | $attr['attr'][$pid][$at->get_id()]['val'][$idx] = $term; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if($prod->has_dimensions()) { |
| 96 | |
| 97 | $attr['dim'][$pid]['len'] = $prod->get_length(); |
| 98 | $attr['dim'][$pid]['wd'] = $prod->get_width(); |
| 99 | $attr['dim'][$pid]['ht'] = $prod->get_height(); |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | ?> |
| 105 | |
| 106 | <div> |
| 107 | <div class="shopengine-comparison-container"> |
| 108 | <div class="shopengine-comparison"> |
| 109 | <h2><?php echo __('Product comparison', 'shopengine') ?></h2> |
| 110 | |
| 111 | <div class="comparison-table-wrap"> |
| 112 | <table class="table table-bordered"> |
| 113 | |
| 114 | <thead> |
| 115 | |
| 116 | </thead> |
| 117 | |
| 118 | <tbody> <?php |
| 119 | |
| 120 | foreach($attr as $key => $info) { ?> |
| 121 | |
| 122 | <tr> <?php |
| 123 | |
| 124 | if($key === 'image') { |
| 125 | |
| 126 | echo '<td> </td>'; |
| 127 | |
| 128 | foreach($ids as $id) { |
| 129 | |
| 130 | echo '<td>'; |
| 131 | echo (empty($info[$id]) ? '' : $info[$id]); |
| 132 | echo '<a class="shopengine-remove-action badge-comparison" data-pid="' . $id . '">' . esc_html__('X', 'shopengine') . '</a>'; |
| 133 | echo ' </td>'; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | } elseif( |
| 138 | $key === 'name' || |
| 139 | $key === 'rating' || |
| 140 | $key === 'stock' || |
| 141 | $key === 'weight' || |
| 142 | $key === 'sku' |
| 143 | ) { |
| 144 | |
| 145 | echo '<td>' . ucfirst($key) . ' :</td>'; |
| 146 | |
| 147 | foreach($ids as $id) { |
| 148 | |
| 149 | echo '<td> ' . (empty($info[$id]) ? '' : $info[$id]) . ' </td>'; |
| 150 | } |
| 151 | |
| 152 | } elseif($key === 'price') { |
| 153 | |
| 154 | echo '<td>' . ucfirst($key) . ' :</td>'; |
| 155 | |
| 156 | foreach($ids as $id) { |
| 157 | |
| 158 | echo '<td><span class="price">' . (empty($info[$id]['htm']) ? '' : $info[$id]['htm']) . '</span></td>'; |
| 159 | } |
| 160 | |
| 161 | } elseif($key === 'dim') { |
| 162 | |
| 163 | echo '<td> ' . __('Dimensions', 'shopengine') . ' :</td>'; |
| 164 | |
| 165 | foreach($ids as $id) { |
| 166 | |
| 167 | if(!isset($info[$id])) { |
| 168 | |
| 169 | echo '<td> </td>'; |
| 170 | |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | echo '<td>' . implode(' * ', $info[$id]) . '</td>'; |
| 175 | } |
| 176 | |
| 177 | } elseif($key === 'attr') { |
| 178 | |
| 179 | echo '<td>' . __('Available options', 'shopengine') . ' :</td>'; |
| 180 | |
| 181 | foreach($ids as $id) { |
| 182 | |
| 183 | if(!isset($info[$id])) { |
| 184 | |
| 185 | echo '<td> </td>'; |
| 186 | |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | echo '<td>'; |
| 191 | |
| 192 | foreach($info[$id] as $item) { |
| 193 | |
| 194 | echo $item['name'] . ' : ' . implode(', ', $item['val']); |
| 195 | |
| 196 | echo '<br/>'; |
| 197 | } |
| 198 | echo '</td>'; |
| 199 | } |
| 200 | } ?> |
| 201 | |
| 202 | </tr> <?php |
| 203 | } ?> |
| 204 | |
| 205 | </tbody> |
| 206 | </table> |
| 207 | </div> |
| 208 | </div> |
| 209 | </div> |
| 210 | </div> |
| 211 | <?php |
| 212 | |
| 213 | } |
| 214 | |
| 215 | public function get_comparison_table() { |
| 216 | |
| 217 | $data = $this->request->get_params(); |
| 218 | $idd = $data['pid']; |
| 219 | |
| 220 | $cck = empty($_COOKIE[Comparison::COOKIE_KEY]) ? '' : $_COOKIE[Comparison::COOKIE_KEY]; |
| 221 | $cck = explode(',', $cck); |
| 222 | $content = array_combine($cck, $cck); |
| 223 | $content[$idd] = $idd; |
| 224 | |
| 225 | unset($content['']); |
| 226 | |
| 227 | $val = implode(',', $content); |
| 228 | |
| 229 | setcookie(Comparison::COOKIE_KEY, $val, strtotime('+' . Comparison::COOKIE_TIME_IN_DAYS . ' days'), '/'); |
| 230 | |
| 231 | $this->prepare_html($content); |
| 232 | |
| 233 | exit(); |
| 234 | } |
| 235 | |
| 236 | public function post_remove() { |
| 237 | |
| 238 | $data = $this->request->get_params(); |
| 239 | $idd = $data['pid']; |
| 240 | |
| 241 | $cck = empty($_COOKIE[Comparison::COOKIE_KEY]) ? '' : $_COOKIE[Comparison::COOKIE_KEY]; |
| 242 | $cck = explode(',', $cck); |
| 243 | $content = array_combine($cck, $cck); |
| 244 | |
| 245 | unset($content[$idd], $content['']); |
| 246 | |
| 247 | $val = implode(',', $content); |
| 248 | |
| 249 | setcookie(Comparison::COOKIE_KEY, $val, strtotime('+' . Comparison::COOKIE_TIME_IN_DAYS . ' days'), '/'); |
| 250 | |
| 251 | $this->prepare_html($content); |
| 252 | |
| 253 | exit(); |
| 254 | } |
| 255 | } |
| 256 |