| 1 |
<?php |
| 2 |
|
| 3 |
namespace ShopEngine\Modules\Comparison; |
| 4 |
|
| 5 |
use ShopEngine\Base\Api; |
| 6 |
use ShopEngine\Utils\Helper; |
| 7 |
|
| 8 |
class Route extends Api { |
| 9 |
|
| 10 |
public function config() { |
| 11 |
|
| 12 |
$this->prefix = 'comparison'; |
| 13 |
$this->param = ""; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
public function get_comparison_table() { |
| 18 |
$data = $this->request->get_params(); |
| 19 |
$product_id = $data['pid']; |
| 20 |
|
| 21 |
if( $product_id ) Comparison_Cookie::add_product_id($product_id); |
| 22 |
$product_ids = Comparison_Cookie::get_product_ids($product_id); |
| 23 |
|
| 24 |
Comparison_Helper::get_html($product_ids); |
| 25 |
exit(); |
| 26 |
} |
| 27 |
|
| 28 |
public function post_remove() { |
| 29 |
|
| 30 |
$data = $this->request->get_params(); |
| 31 |
$pid = $data['pid']; |
| 32 |
|
| 33 |
Comparison_Cookie::remove_product_id($pid); |
| 34 |
|
| 35 |
$product_ids = Comparison_Cookie::get_product_ids(); |
| 36 |
if (($key = array_search($pid, $product_ids)) !== false) { |
| 37 |
unset($product_ids[$key]); |
| 38 |
} |
| 39 |
$share_url = home_url( '?comparison=yes&product_ids=' . implode( ',', $product_ids ) ); |
| 40 |
|
| 41 |
wp_send_json([ |
| 42 |
"status" => "Success", |
| 43 |
"share_url" => $product_ids ? $share_url : home_url( '?comparison=yes' ), |
| 44 |
]); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function get_comparison_bar_data() { |
| 50 |
|
| 51 |
$product_ids = Comparison_Cookie::get_product_ids(); |
| 52 |
|
| 53 |
$content = ''; |
| 54 |
if ( empty( $product_ids ) ) { |
| 55 |
$content .= '<h1 class="shopengine-no-comparison-product-for-bar">' . esc_html__( 'No product is added for comparison, please add some product to compare', |
| 56 |
'shopengine' ) . '</h1>'; |
| 57 |
return $content; |
| 58 |
} |
| 59 |
|
| 60 |
foreach ( $product_ids as $product_id ) { |
| 61 |
$image = get_the_post_thumbnail_url( $product_id, 'shop_thumbnail' ); |
| 62 |
$content .= ' |
| 63 |
<div class="comparison-for-bottom-bar-item"> |
| 64 |
<a class="shopengine-comparison-bar-action badge-comparison" data-pid="'.esc_attr( $product_id ).'"><i class="eicon-close"></i></a> |
| 65 |
<img src="'.esc_url($image).'" alt=""> |
| 66 |
</div>'; |
| 67 |
} |
| 68 |
|
| 69 |
return $content; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function get_attributes() { |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
$attributes = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies"); |
| 78 |
|
| 79 |
$formatted_attributes = []; |
| 80 |
foreach ($attributes as $attribute){ |
| 81 |
$formatted_attributes[$attribute->attribute_name] = $attribute->attribute_label; |
| 82 |
} |
| 83 |
|
| 84 |
wp_send_json([ |
| 85 |
"status" => "success", |
| 86 |
"result" => $formatted_attributes, |
| 87 |
"message" => "Available attributes" |
| 88 |
] ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public function get_custom_meta() { |
| 93 |
|
| 94 |
wp_send_json([ |
| 95 |
"status" => "success", |
| 96 |
"result" => Comparison_Helper::get_products_meta_keys(), |
| 97 |
"message" => "Products Meta keys" |
| 98 |
] ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
} |
| 104 |
|