| 1 |
<?php |
| 2 |
namespace EverCompare\Frontend; |
| 3 |
/** |
| 4 |
* Button handlers class |
| 5 |
*/ |
| 6 |
class Manage_Compare { |
| 7 |
|
| 8 |
/** |
| 9 |
* [$_instance] |
| 10 |
* @var null |
| 11 |
*/ |
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* [$reached_max_limit] |
| 16 |
* @var boolean |
| 17 |
*/ |
| 18 |
public $reached_max_limit = false; |
| 19 |
|
| 20 |
public $max_limit; |
| 21 |
|
| 22 |
/** |
| 23 |
* [instance] Initializes a singleton instance |
| 24 |
* @return [Compare_Button] |
| 25 |
*/ |
| 26 |
public static function instance() { |
| 27 |
if ( is_null( self::$_instance ) ) { |
| 28 |
self::$_instance = new self(); |
| 29 |
} |
| 30 |
return self::$_instance; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Initialize the class |
| 35 |
*/ |
| 36 |
private function __construct() { |
| 37 |
$this->max_limit = intval( ever_compare_get_option( 'limit','ever_compare_table_settings_tabs', get_option( 'posts_per_page' ) )); |
| 38 |
add_action( 'init', [ $this, 'button_manager' ] ); |
| 39 |
add_action( 'ever_compare_before_table', [ $this, 'reached_max_limit_message' ], 1 ); |
| 40 |
add_action( 'ever_compare_after_table', [ $this, 'shareable_link' ], 1 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* [compare_button] |
| 45 |
* @return [void] |
| 46 |
*/ |
| 47 |
public function button_print(){ |
| 48 |
echo do_shortcode( '[evercompare_button]' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* [button_manager] Button Manager |
| 53 |
* @return [void] |
| 54 |
*/ |
| 55 |
public function button_manager(){ |
| 56 |
|
| 57 |
$enable_btn = ever_compare_get_option( 'btn_show_shoppage', 'ever_compare_settings_tabs', 'off' ); |
| 58 |
$product_enable_btn = ever_compare_get_option( 'btn_show_productpage', 'ever_compare_settings_tabs', 'on' ); |
| 59 |
|
| 60 |
$shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 61 |
$product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 62 |
|
| 63 |
|
| 64 |
// Shop Button Position |
| 65 |
if( $shop_page_btn_position != 'use_shortcode' && $enable_btn == 'on' ){ |
| 66 |
switch ( $shop_page_btn_position ) { |
| 67 |
|
| 68 |
case 'before_cart_btn': |
| 69 |
add_action( 'woocommerce_after_shop_loop_item', [ $this, 'button_print' ], 7 ); |
| 70 |
break; |
| 71 |
|
| 72 |
case 'top_thumbnail': |
| 73 |
add_action( 'woocommerce_before_shop_loop_item', [ $this, 'button_print' ], 5 ); |
| 74 |
break; |
| 75 |
|
| 76 |
case 'custom_position': |
| 77 |
$hook_name = ever_compare_get_option( 'shop_custom_hook_name', 'ever_compare_settings_tabs', '' ); |
| 78 |
$priority = ever_compare_get_option( 'shop_custom_hook_priority', 'ever_compare_settings_tabs', 10 ); |
| 79 |
if( !empty( $hook_name ) ){ |
| 80 |
add_action( $hook_name, [ $this, 'button_print' ], $priority ); |
| 81 |
} |
| 82 |
break; |
| 83 |
|
| 84 |
default: |
| 85 |
add_action( 'woocommerce_after_shop_loop_item', [ $this, 'button_print' ], 20 ); |
| 86 |
break; |
| 87 |
|
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Product Page Button Position |
| 92 |
if( $product_page_btn_position != 'use_shortcode' && $product_enable_btn == 'on' ){ |
| 93 |
switch ( $product_page_btn_position ) { |
| 94 |
|
| 95 |
case 'before_cart_btn': |
| 96 |
add_action( 'woocommerce_before_add_to_cart_button', [ $this, 'button_print' ], 20 ); |
| 97 |
break; |
| 98 |
|
| 99 |
case 'after_thumbnail': |
| 100 |
add_action( 'woocommerce_product_thumbnails', [ $this, 'button_print' ], 21 ); |
| 101 |
break; |
| 102 |
|
| 103 |
case 'after_summary': |
| 104 |
add_action( 'woocommerce_after_single_product_summary', [ $this, 'button_print' ], 11 ); |
| 105 |
break; |
| 106 |
|
| 107 |
case 'custom_position': |
| 108 |
$hook_name = ever_compare_get_option( 'product_custom_hook_name', 'ever_compare_settings_tabs', '' ); |
| 109 |
$priority = ever_compare_get_option( 'product_custom_hook_priority', 'ever_compare_settings_tabs', 10 ); |
| 110 |
if( !empty( $hook_name ) ){ |
| 111 |
add_action( $hook_name, [ $this, 'button_print' ], $priority ); |
| 112 |
} |
| 113 |
break; |
| 114 |
|
| 115 |
default: |
| 116 |
add_action( 'woocommerce_single_product_summary', [ $this, 'button_print' ], 31 ); |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Popup HTML Render |
| 123 |
*/ |
| 124 |
if( ever_compare_get_option( 'open_popup', 'ever_compare_settings_tabs', 'on' ) === 'on' ){ |
| 125 |
add_action( 'wp_footer', [ $this, 'pop_up_html' ] ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Manage maximum product added message |
| 130 |
*/ |
| 131 |
if( isset( $_COOKIE[ 'ever_compare_max_limit' ] ) && $_COOKIE[ 'ever_compare_max_limit' ] == 'yes' ) { |
| 132 |
setcookie( 'ever_compare_max_limit', 'no', 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 133 |
$this->reached_max_limit = true; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* [button_html] Button HTML |
| 141 |
* @param [type] $atts |
| 142 |
* @return [HTML] |
| 143 |
*/ |
| 144 |
public function button_html( $atts ) { |
| 145 |
$button_attr = apply_filters( 'evercompare_button_arg', $atts ); |
| 146 |
return ever_compare_get_template( 'evercompare-button-'.$atts['template_name'].'.php', $button_attr, false ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* [table_html] Wishlist table HTML |
| 151 |
* @return [HTML] |
| 152 |
*/ |
| 153 |
public function table_html( $atts ) { |
| 154 |
$table_attr = apply_filters( 'evercompare_table_arg', $atts ); |
| 155 |
return ever_compare_get_template( 'evercompare-table.php', $table_attr, false ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* [count_html] Count HTML |
| 160 |
* @param [type] $atts |
| 161 |
* @return [HTML] |
| 162 |
*/ |
| 163 |
public function count_html( $atts ) { |
| 164 |
$button_attr = apply_filters( 'evercompare_count_arg', $atts ); |
| 165 |
return ever_compare_get_template( 'evercompare-count.php', $button_attr, false ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* [pop_up_html] |
| 170 |
* @return [void] |
| 171 |
*/ |
| 172 |
public function pop_up_html(){ |
| 173 |
echo '<div class="htcompare-popup"><div class="htcompare-popup-content-area"><span class="htcompare-popup-close"> </span>'.do_shortcode( '[evercompare_table]' ).'</div></div>'; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* [reached_max_limit_message] |
| 178 |
* @return [void] |
| 179 |
*/ |
| 180 |
public function reached_max_limit_message(){ |
| 181 |
if( ! $this->reached_max_limit ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
$message = !empty( ever_compare_get_option('reached_max_limit_message','ever_compare_table_settings_tabs') ) ? ever_compare_get_option('reached_max_limit_message','ever_compare_table_settings_tabs') : esc_html__( 'You are already added the maximum number of products.', 'ever-compare' ); |
| 186 |
echo '<div class="ever-compare-message-error"><p>'. wp_kses_post( $message ).'</p></div>'; |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* [shareable_link] |
| 192 |
* @return [void] |
| 193 |
*/ |
| 194 |
public function shareable_link(){ |
| 195 |
|
| 196 |
$shareable_link = ever_compare_get_option( 'enable_shareable_link','ever_compare_table_settings_tabs','off' ); |
| 197 |
if ( $shareable_link !== 'on' ) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$ids = $this->get_compared_products(); |
| 202 |
|
| 203 |
if ( isset( $_GET['evcompare'] ) ) { |
| 204 |
$query_perametter_ids = sanitize_text_field( $_GET['evcompare'] ); |
| 205 |
if( !empty( $query_perametter_ids ) ){ |
| 206 |
$ids = explode( ',', $query_perametter_ids ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
$buttonText = ever_compare_get_option( 'shareable_link_button_text','ever_compare_table_settings_tabs','Copy shareable link' ) ? ever_compare_get_option( 'shareable_link_button_text','ever_compare_table_settings_tabs','Copy shareable link' ) : esc_html__( 'Copy shareable link', 'ever-compare' ); |
| 211 |
$aftercopy_buttonText = ever_compare_get_option( 'shareable_link_after_button_text','ever_compare_table_settings_tabs','Copied' ) ? ever_compare_get_option( 'shareable_link_after_button_text','ever_compare_table_settings_tabs','Copied' ) : esc_html__( 'Copied', 'ever-compare' ); |
| 212 |
$button_pos = ever_compare_get_option( 'linkshare_btn_pos','ever_compare_table_settings_tabs','right' ); |
| 213 |
|
| 214 |
$idsString = is_array( $ids ) ? implode( ',', $ids ) : ''; |
| 215 |
$shareablelink = $this->get_compare_page_url() . '?evcompare='.$idsString; |
| 216 |
?> |
| 217 |
<div class="ever-compare-shareable-link <?php echo esc_attr( $button_pos );?>"> |
| 218 |
<button class="evercompare-copy-link" data-copytext="<?php echo esc_attr( $aftercopy_buttonText ); ?>" data-btntext="<?php echo esc_attr( $buttonText ); ?>"><?php echo $buttonText; ?></button> |
| 219 |
<p style="display: none;" class="evercompare-share-link"><?php echo $shareablelink; ?></p> |
| 220 |
</div> |
| 221 |
<?php |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* [add_to_compare] Ajax callable function |
| 227 |
*/ |
| 228 |
public function add_to_compare( $id ) { |
| 229 |
|
| 230 |
$cookie_name = $this->get_cookie_name(); |
| 231 |
|
| 232 |
if ( $this->is_product_in_compare( $id ) ) { |
| 233 |
$this->compare_json_response(); |
| 234 |
} |
| 235 |
|
| 236 |
$products = $this->get_compared_products(); |
| 237 |
|
| 238 |
// Reached Maximum Limit |
| 239 |
if( $this->reached_max_limit() ) { |
| 240 |
setcookie( 'ever_compare_max_limit', 'yes', 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 241 |
$this->compare_json_response(); |
| 242 |
} |
| 243 |
|
| 244 |
$products[] = $id; |
| 245 |
|
| 246 |
setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 247 |
|
| 248 |
$_COOKIE[$cookie_name] = json_encode( $products ); |
| 249 |
|
| 250 |
$this->compare_json_response(); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* [reached_max_limit] |
| 256 |
* @return [bool] |
| 257 |
*/ |
| 258 |
public function reached_max_limit(){ |
| 259 |
|
| 260 |
$products = $this->get_compared_products(); |
| 261 |
|
| 262 |
if( $this->max_limit && count( $products ) >= $this->max_limit ) { |
| 263 |
$this->reached_max_limit = true; |
| 264 |
} else { |
| 265 |
$this->reached_max_limit = false; |
| 266 |
} |
| 267 |
|
| 268 |
return $this->reached_max_limit; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* [remove_from_compare] Ajax callable function |
| 273 |
* @return [json] |
| 274 |
*/ |
| 275 |
public function remove_from_compare( $id ) { |
| 276 |
|
| 277 |
$cookie_name = $this->get_cookie_name(); |
| 278 |
|
| 279 |
if ( ! $this->is_product_in_compare( $id ) ) { |
| 280 |
$this->compare_json_response(); |
| 281 |
} |
| 282 |
|
| 283 |
$products = $this->get_compared_products(); |
| 284 |
|
| 285 |
foreach ( $products as $prod_key => $product_id ) { |
| 286 |
if ( intval( $id ) == $product_id ) { |
| 287 |
unset( $products[ $prod_key ] ); |
| 288 |
$this->reached_max_limit = false; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
if ( empty( $products ) ) { |
| 293 |
setcookie( $cookie_name, false, 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 294 |
$_COOKIE[$cookie_name] = false; |
| 295 |
} else { |
| 296 |
setcookie( $cookie_name, json_encode( $products ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); |
| 297 |
$_COOKIE[$cookie_name] = json_encode( $products ); |
| 298 |
} |
| 299 |
|
| 300 |
$this->compare_json_response(); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* [get_response_html ] Compare product table HTML |
| 305 |
* @return [html] |
| 306 |
*/ |
| 307 |
public function get_response_html(){ |
| 308 |
|
| 309 |
$products = $this->get_compared_products_data(); |
| 310 |
$fields = $this->get_compare_fields(); |
| 311 |
|
| 312 |
$empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); |
| 313 |
$return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop'); |
| 314 |
|
| 315 |
$custom_heading = !empty( ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) : array(); |
| 316 |
|
| 317 |
$default_atts = array( |
| 318 |
'evercompare' => self::instance(), |
| 319 |
'products' => $products, |
| 320 |
'fields' => $fields, |
| 321 |
'heading_txt' => $custom_heading, |
| 322 |
'return_shop_button' => $return_shop_button, |
| 323 |
'empty_compare_text' => $empty_compare_text, |
| 324 |
); |
| 325 |
$table_attr = apply_filters( 'evercompare_response_html_args', $default_atts ); |
| 326 |
|
| 327 |
ever_compare_get_template( 'evercompare-table.php', $table_attr ); |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* [get_compared_products_data] generate compared products data |
| 333 |
* @return [array] product list |
| 334 |
*/ |
| 335 |
public function get_compared_products_data() { |
| 336 |
$ids = $this->get_compared_products(); |
| 337 |
|
| 338 |
// For shareable link |
| 339 |
$shareable_link = ever_compare_get_option('enable_shareable_link','ever_compare_table_settings_tabs','off'); |
| 340 |
if ( ( $shareable_link === 'on' ) && isset( $_GET['evcompare'] ) ) { |
| 341 |
$query_perametter_ids = sanitize_text_field( $_GET['evcompare'] ); |
| 342 |
if( !empty( $query_perametter_ids ) ){ |
| 343 |
$ids = explode( ',', $query_perametter_ids ); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
if ( empty( $ids ) ) { |
| 348 |
return array(); |
| 349 |
} |
| 350 |
|
| 351 |
$args = array( |
| 352 |
'include' => $ids, |
| 353 |
'limit' => $this->max_limit |
| 354 |
); |
| 355 |
|
| 356 |
$products = wc_get_products( $args ); |
| 357 |
|
| 358 |
$products_data = array(); |
| 359 |
|
| 360 |
$fields = $this->get_compare_fields(); |
| 361 |
|
| 362 |
$fields = array_filter( $fields, function( $field ) { |
| 363 |
return 'pa_' === substr( $field, 0, 3 ); |
| 364 |
}, ARRAY_FILTER_USE_KEY ); |
| 365 |
|
| 366 |
$data_none = '-'; |
| 367 |
|
| 368 |
foreach ( $products as $product ) { |
| 369 |
|
| 370 |
$rating_count = $product->get_rating_count(); |
| 371 |
$average = $product->get_average_rating(); |
| 372 |
|
| 373 |
$products_data[ $product->get_id() ] = array( |
| 374 |
'primary' => array( |
| 375 |
'image' => $product->get_image() ? $product->get_image('ever-compare-image') : $data_none, |
| 376 |
), |
| 377 |
'id' => $product->get_id(), |
| 378 |
'title' => $product->get_title() ? $product->get_title() : $data_none, |
| 379 |
'image_id' => $product->get_image_id(), |
| 380 |
'permalink' => $product->get_permalink(), |
| 381 |
'price' => $product->get_price_html() ? $product->get_price_html() : $data_none, |
| 382 |
'rating' => wc_get_rating_html( $average, $rating_count ), |
| 383 |
'add_to_cart' => $this->add_to_cart_html( $product ) ? $this->add_to_cart_html( $product ) :$data_none, |
| 384 |
'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ), |
| 385 |
'description' => $product->get_short_description() ? $product->get_short_description() : $data_none, |
| 386 |
'weight' => $product->get_weight() ? $product->get_weight() : $data_none, |
| 387 |
'sku' => $product->get_sku() ? $product->get_sku() : $data_none, |
| 388 |
'availability' => $this->availability_html( $product ), |
| 389 |
); |
| 390 |
|
| 391 |
foreach ( $fields as $field_id => $field_name ) { |
| 392 |
if ( taxonomy_exists( $field_id ) ) { |
| 393 |
$products_data[ $product->get_id() ][ $field_id ] = array(); |
| 394 |
$terms = get_the_terms( $product->get_id(), $field_id ); |
| 395 |
if ( ! empty( $terms ) ) { |
| 396 |
foreach ( $terms as $term ) { |
| 397 |
$term = sanitize_term( $term, $field_id ); |
| 398 |
$products_data[ $product->get_id() ][ $field_id ][] = $term->name; |
| 399 |
} |
| 400 |
} else { |
| 401 |
$products_data[ $product->get_id() ][ $field_id ][] = '-'; |
| 402 |
} |
| 403 |
$products_data[ $product->get_id() ][ $field_id ] = implode( ', ', $products_data[ $product->get_id() ][ $field_id ] ); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
return $products_data; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* [get_compare_fields] Table field list |
| 413 |
* @return [array] Table Field list |
| 414 |
*/ |
| 415 |
public function get_compare_fields() { |
| 416 |
$fields = array( |
| 417 |
'primary' => '' |
| 418 |
); |
| 419 |
|
| 420 |
$default_show = array( |
| 421 |
'title' => esc_html__( 'title', 'ever-compare' ), |
| 422 |
'ratting' => esc_html__( 'ratting', 'ever-compare' ), |
| 423 |
'price' => esc_html__( 'price', 'ever-compare' ), |
| 424 |
'add_to_cart' => esc_html__( 'add_to_cart', 'ever-compare' ), |
| 425 |
'description' => esc_html__( 'description', 'ever-compare' ), |
| 426 |
'availability' => esc_html__( 'availability', 'ever-compare' ), |
| 427 |
'sku' => esc_html__( 'sku', 'ever-compare' ), |
| 428 |
'weight' => esc_html__( 'weight', 'ever-compare' ), |
| 429 |
'dimensions' => esc_html__( 'dimensions', 'ever-compare' ), |
| 430 |
); |
| 431 |
|
| 432 |
$fields_settings = !empty( ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'show_fields', 'ever_compare_table_settings_tabs' ) : array(); |
| 433 |
|
| 434 |
if ( isset( $fields_settings ) && count( $fields_settings ) > 1 ) { |
| 435 |
$fields = $fields + $fields_settings; |
| 436 |
}else{ |
| 437 |
$fields = $fields + $default_show; |
| 438 |
} |
| 439 |
|
| 440 |
return $fields; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* [is_products_have_field] |
| 445 |
* @param [string] $field_id |
| 446 |
* @param [object] $products |
| 447 |
* @return boolean |
| 448 |
*/ |
| 449 |
public function is_products_have_field( $field_id, $products ) { |
| 450 |
foreach ( $products as $product_id => $product ) { |
| 451 |
if ( isset( $product[ $field_id ] ) && ( ! empty( $product[ $field_id ] ) && '-' !== $product[ $field_id ] && 'N/A' !== $product[ $field_id ] ) ) { |
| 452 |
return true; |
| 453 |
} |
| 454 |
} |
| 455 |
return false; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* [compare_display_field] |
| 460 |
* @param [string] $field_id |
| 461 |
* @param [array] $product |
| 462 |
* @return [html] |
| 463 |
*/ |
| 464 |
public function compare_display_field( $field_id, $product ) { |
| 465 |
|
| 466 |
$type = $field_id; |
| 467 |
|
| 468 |
if ( 'pa_' === substr( $field_id, 0, 3 ) ) { |
| 469 |
$type = 'attribute'; |
| 470 |
} |
| 471 |
|
| 472 |
switch ( $type ) { |
| 473 |
case 'primary': |
| 474 |
?> |
| 475 |
<div class="htcompare-primary-content-area"> |
| 476 |
<a href="#" class="htcompare-remove" data-product_title="<?php echo esc_attr( $product['title'] ); ?>" data-product_id="<?php echo esc_attr( $product['id'] ); ?>"> </a> |
| 477 |
<a href="<?php echo get_permalink( $product['id'] ); ?>" class="htcompare-product-image"> |
| 478 |
<?php echo $product['primary']['image']; ?> |
| 479 |
</a> |
| 480 |
</div> |
| 481 |
<?php |
| 482 |
break; |
| 483 |
|
| 484 |
case 'title': |
| 485 |
echo '<a href="'.get_permalink( $product['id'] ).'" class="htcompare-product-title">'.$product[ $field_id ].'</a>'; |
| 486 |
break; |
| 487 |
|
| 488 |
case 'ratting': |
| 489 |
echo '<span class="htcompare-product-ratting">'.wp_kses_post( $product[ $field_id ] ).'</span>'; |
| 490 |
break; |
| 491 |
|
| 492 |
case 'price': |
| 493 |
echo '<div class="htcompare-product-price">'.wp_kses_post( $product[ $field_id ] ).'</div>'; |
| 494 |
break; |
| 495 |
|
| 496 |
case 'add_to_cart': |
| 497 |
echo apply_filters( 'htcompare_add_to_cart_btn', $product[ $field_id ] ); |
| 498 |
break; |
| 499 |
|
| 500 |
case 'attribute': |
| 501 |
echo wp_kses_post( $product[ $field_id ] ); |
| 502 |
break; |
| 503 |
|
| 504 |
case 'weight': |
| 505 |
if ( $product[ $field_id ] ) { |
| 506 |
$unit = $product[ $field_id ] !== '-' ? get_option( 'woocommerce_weight_unit' ) : ''; |
| 507 |
echo wc_format_localized_decimal( $product[ $field_id ] ) . ' ' . esc_attr( $unit ); |
| 508 |
} |
| 509 |
break; |
| 510 |
|
| 511 |
case 'description': |
| 512 |
echo apply_filters( 'woocommerce_short_description', $product[ $field_id ] ); |
| 513 |
break; |
| 514 |
|
| 515 |
default: |
| 516 |
echo wp_kses_post( $product[ $field_id ] ); |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
/** |
| 522 |
* [add_to_cart_html] Generate Cart button |
| 523 |
* @param [object] $product |
| 524 |
*/ |
| 525 |
public function add_to_cart_html( $product ) { |
| 526 |
if ( ! $product ) return; |
| 527 |
|
| 528 |
$defaults = array( |
| 529 |
'quantity' => 1, |
| 530 |
'class' => implode( ' ', array_filter( array( |
| 531 |
'htcompare-cart-button button', |
| 532 |
'product_type_' . $product->get_type(), |
| 533 |
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '', |
| 534 |
$product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '', |
| 535 |
) ) ), |
| 536 |
'attributes' => array( |
| 537 |
'data-product_id' => $product->get_id(), |
| 538 |
'data-product_sku' => $product->get_sku(), |
| 539 |
'aria-label' => $product->add_to_cart_description(), |
| 540 |
'rel' => 'nofollow', |
| 541 |
), |
| 542 |
); |
| 543 |
|
| 544 |
$args = apply_filters( 'woocommerce_loop_add_to_cart_args', $defaults, $product ); |
| 545 |
|
| 546 |
if ( isset( $args['attributes']['aria-label'] ) ) { |
| 547 |
$args['attributes']['aria-label'] = strip_tags( $args['attributes']['aria-label'] ); |
| 548 |
} |
| 549 |
|
| 550 |
return apply_filters( 'woocommerce_loop_add_to_cart_link', |
| 551 |
sprintf( '<a href="%s" data-quantity="%s" class="%s add-to-cart-loop" %s><span>%s</span></a>', |
| 552 |
esc_url( $product->add_to_cart_url() ), |
| 553 |
esc_attr( isset( $args['quantity'] ) ? $args['quantity'] : 1 ), |
| 554 |
esc_attr( isset( $args['class'] ) ? $args['class'] : 'button' ), |
| 555 |
isset( $args['attributes'] ) ? wc_implode_html_attributes( $args['attributes'] ) : '', |
| 556 |
esc_html( $product->add_to_cart_text() ) |
| 557 |
), |
| 558 |
$product, $args ); |
| 559 |
} |
| 560 |
|
| 561 |
/** |
| 562 |
* [availability_html] |
| 563 |
* @param [object] $product |
| 564 |
* @return [html] |
| 565 |
*/ |
| 566 |
public function availability_html( $product ) { |
| 567 |
$html = ''; |
| 568 |
$availability = $product->get_availability(); |
| 569 |
|
| 570 |
if( empty( $availability['availability'] ) ) { |
| 571 |
$availability['availability'] = __( 'In stock', 'woocommerce' ); |
| 572 |
} |
| 573 |
|
| 574 |
if ( ! empty( $availability['availability'] ) ) { |
| 575 |
ob_start(); |
| 576 |
|
| 577 |
wc_get_template( 'single-product/stock.php', array( |
| 578 |
'product' => $product, |
| 579 |
'class' => $availability['class'], |
| 580 |
'availability' => $availability['availability'], |
| 581 |
) ); |
| 582 |
|
| 583 |
$html = ob_get_clean(); |
| 584 |
} |
| 585 |
|
| 586 |
return apply_filters( 'woocommerce_get_stock_html', $html, $product ); |
| 587 |
} |
| 588 |
|
| 589 |
/** |
| 590 |
* [compare_cookie_name] Get Compare cookie name |
| 591 |
* @return [string] |
| 592 |
*/ |
| 593 |
public function get_cookie_name() { |
| 594 |
$name = 'ever_compare_compare_list'; |
| 595 |
if ( is_multisite() ){ |
| 596 |
$name .= '_' . get_current_blog_id(); |
| 597 |
} |
| 598 |
return $name; |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* [is_product_in_compare] |
| 603 |
* @param [int] $id product id |
| 604 |
* @return [array] product id list |
| 605 |
*/ |
| 606 |
public function is_product_in_compare( $id ) { |
| 607 |
$list = $this->get_compared_products(); |
| 608 |
return in_array( strval($id), $list, true ); |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* [get_compared_products] |
| 613 |
* @return [json] |
| 614 |
*/ |
| 615 |
public function get_compared_products() { |
| 616 |
$cookie_name = $this->get_cookie_name(); |
| 617 |
return isset( $_COOKIE[ $cookie_name ] ) ? json_decode( wp_unslash( $_COOKIE[ $cookie_name ] ), true ) : array(); |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* [compare_json_response] |
| 622 |
* @return [json] product json |
| 623 |
*/ |
| 624 |
public function compare_json_response() { |
| 625 |
$count = 0; |
| 626 |
$products = $this->get_compared_products(); |
| 627 |
|
| 628 |
ob_start(); |
| 629 |
|
| 630 |
$this->get_response_html(); |
| 631 |
|
| 632 |
$table_html = ob_get_clean(); |
| 633 |
|
| 634 |
if ( is_array( $products ) ) { |
| 635 |
$count = count( $products ); |
| 636 |
} |
| 637 |
|
| 638 |
$res = [ |
| 639 |
'count' => $count, |
| 640 |
'table' => $table_html, |
| 641 |
'products' => $products, |
| 642 |
'limitReached' => '<div class="ever-compare-message-error"><p>'. wp_kses_post( esc_html__( 'You have already added the maximum number of products.', 'ever-compare' ) ).'</p></div>' |
| 643 |
]; |
| 644 |
wp_send_json( $res ); |
| 645 |
|
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* [get_compare_page_url] Compare Page Link |
| 650 |
* @return [URL] |
| 651 |
*/ |
| 652 |
public function get_compare_page_url() { |
| 653 |
$page_id = ever_compare_get_option( 'compare_page', 'ever_compare_table_settings_tabs' ); |
| 654 |
return get_permalink( $page_id ); |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* [field_name] |
| 659 |
* @param [string] $field |
| 660 |
* @return [string] |
| 661 |
*/ |
| 662 |
public function field_name( $field = '', $custom = false ){ |
| 663 |
|
| 664 |
if( empty( $field ) ){ |
| 665 |
return; |
| 666 |
} |
| 667 |
|
| 668 |
if( $custom === true ){ |
| 669 |
return $field; |
| 670 |
} |
| 671 |
|
| 672 |
$default = ever_compare_get_default_fields(); |
| 673 |
|
| 674 |
$str = substr( $field, 0, 3 ); |
| 675 |
if( 'pa_' === $str ){ |
| 676 |
$field_name = wc_attribute_label( $field ); |
| 677 |
}else{ |
| 678 |
$field_name = $default[$field]; |
| 679 |
} |
| 680 |
return $field_name; |
| 681 |
|
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
} |