YITH_Woocompare_Frontend

YITH Custom Login Frontend.

Defined (1)

The class is defined in the following location(s).

/includes/class.yith-woocompare-frontend.php  
  1. class YITH_Woocompare_Frontend { 
  2. /** 
  3. * Plugin version 
  4. * @var string 
  5. * @since 1.0.0 
  6. */ 
  7. public $version = YITH_WOOCOMPARE_VERSION; 
  8.  
  9. /** 
  10. * The list of products inside the comparison table 
  11. * @var array 
  12. * @since 1.0.0 
  13. */ 
  14. public $products_list = array(); 
  15.  
  16. /** 
  17. * Plugin version 
  18. * @var string 
  19. * @since 1.0.0 
  20. */ 
  21. public $template_file = 'compare.php'; 
  22.  
  23. /** 
  24. * Stylesheet file 
  25. *  
  26. * @var string 
  27. * @since 2.1.0 
  28. */ 
  29. public $stylesheet_file = 'compare.css'; 
  30.  
  31. /** 
  32. * The name of cookie name 
  33. * @var string 
  34. * @since 1.0.0 
  35. */ 
  36. public $cookie_name = 'yith_woocompare_list'; 
  37.  
  38. /** 
  39. * The action used to view the table comparison 
  40. * @var string 
  41. * @since 1.0.0 
  42. */ 
  43. public $action_view = 'yith-woocompare-view-table'; 
  44.  
  45. /** 
  46. * The action used to add the product to compare list 
  47. * @var string 
  48. * @since 1.0.0 
  49. */ 
  50. public $action_add = 'yith-woocompare-add-product'; 
  51.  
  52. /** 
  53. * The action used to add the product to compare list 
  54. * @var string 
  55. * @since 1.0.0 
  56. */ 
  57. public $action_remove = 'yith-woocompare-remove-product'; 
  58.  
  59. /** 
  60. * The action used to reload the compare list widget 
  61. * @var string 
  62. * @since 1.0.0 
  63. */ 
  64. public $action_reload = 'yith-woocompare-reload-product'; 
  65.  
  66. /** 
  67. * The standard fields 
  68. * @var array 
  69. * @since 1.0.0 
  70. */ 
  71. public $default_fields = array(); 
  72.  
  73. /** 
  74. * Constructor 
  75. * @return YITH_Woocompare_Frontend 
  76. * @since 1.0.0 
  77. */ 
  78. public function __construct() { 
  79.  
  80. // set coookiename 
  81. if ( is_multisite() ) $this->cookie_name .= '_' . get_current_blog_id(); 
  82.  
  83. add_action( 'init', array( $this, 'populate_products_list' ), 10 ); 
  84.  
  85. // populate default fields for the comparison table 
  86. $this->default_fields = YITH_Woocompare_Helper::standard_fields(); 
  87.  
  88. // Add link or button in the products list or 
  89. if ( get_option('yith_woocompare_compare_button_in_product_page') == 'yes' ) add_action( 'woocommerce_single_product_summary', array( $this, 'add_compare_link' ), 35 ); 
  90. if ( get_option('yith_woocompare_compare_button_in_products_list') == 'yes' ) add_action( 'woocommerce_after_shop_loop_item', array( $this, 'add_compare_link' ), 20 ); 
  91. add_action( 'init', array( $this, 'add_product_to_compare_action' ), 15 ); 
  92. add_action( 'init', array( $this, 'remove_product_from_compare_action' ), 15 ); 
  93. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  94. add_action( 'template_redirect', array( $this, 'compare_table_html' ) ); 
  95.  
  96. // add the shortcode 
  97. add_shortcode( 'yith_compare_button', array( $this, 'compare_button_sc' ) ); 
  98.  
  99. // AJAX 
  100. if( version_compare( WC()->version, '2.4', '>=' ) ) { 
  101. add_action( 'wc_ajax_' . $this->action_add, array( $this, 'add_product_to_compare_ajax' ) ); 
  102. add_action( 'wc_ajax_' . $this->action_remove, array( $this, 'remove_product_from_compare_ajax' ) ); 
  103. add_action( 'wc_ajax_' . $this->action_reload, array( $this, 'reload_widget_list_ajax' ) ); 
  104. else { 
  105. add_action( 'wp_ajax_' . $this->action_add, array( $this, 'add_product_to_compare_ajax' ) ); 
  106. add_action( 'wp_ajax_' . $this->action_remove, array( $this, 'remove_product_from_compare_ajax' ) ); 
  107. add_action( 'wp_ajax_' . $this->action_reload, array( $this, 'reload_widget_list_ajax' ) ); 
  108. // no priv 
  109. add_action( 'wp_ajax_nopriv_' . $this->action_add, array( $this, 'add_product_to_compare_ajax' ) ); 
  110. add_action( 'wp_ajax_nopriv_' . $this->action_remove, array( $this, 'remove_product_from_compare_ajax' ) ); 
  111. add_action( 'wp_ajax_nopriv_' . $this->action_reload, array( $this, 'reload_widget_list_ajax' ) ); 
  112.  
  113. return $this; 
  114.  
  115. /** 
  116. * Populate the compare product list 
  117. */ 
  118. public function populate_products_list() { 
  119.  
  120. global $sitepress; 
  121.  
  122. /** 
  123. * WPML Support 
  124. */ 
  125. $lang = isset( $_REQUEST['lang'] ) ? $_REQUEST['lang'] : false; 
  126.  
  127. // get cookie val 
  128. $the_list = isset( $_COOKIE[ $this->cookie_name ] ) ? json_decode( $_COOKIE[ $this->cookie_name ] ) : array(); 
  129.  
  130. // switch lang for WPML 
  131. if( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) { 
  132. $sitepress->switch_lang( $lang, true ); 
  133.  
  134. foreach( $the_list as $product_id ) { 
  135. if( function_exists( 'wpml_object_id_filter' ) ) { 
  136. $product_id_translated = wpml_object_id_filter( $product_id, 'product', false ); 
  137. // get all product of current lang 
  138. if ( $product_id_translated !== $product_id ) { 
  139. continue; 
  140.  
  141. // check for deleted|private products 
  142. $product = wc_get_product( $product_id ); 
  143. if( ! $product || yit_get_prop( $product, 'post_status' ) !== 'publish' ) { 
  144. continue; 
  145.  
  146. $this->products_list[] = $product_id; 
  147.  
  148. do_action( 'yith_woocompare_after_populate_product_list', $this->products_list ); 
  149.  
  150. /** 
  151. * Enqueue the scripts and styles in the page 
  152. */ 
  153. public function enqueue_scripts() { 
  154.  
  155. // scripts 
  156. $min = ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '.min' : ''; 
  157. wp_register_script( 'yith-woocompare-main', YITH_WOOCOMPARE_ASSETS_URL . '/js/woocompare' . $min .'.js', array('jquery'), $this->version, true ); 
  158.  
  159. // enqueue and add localize 
  160. wp_enqueue_script( 'yith-woocompare-main' ); 
  161.  
  162. // localize script args 
  163. $args = apply_filters( 'yith_woocompare_main_script_localize_array', array( 
  164. 'ajaxurl' => version_compare( WC()->version, '2.4', '>=' ) ? WC_AJAX::get_endpoint( "%%endpoint%%" ) : admin_url( 'admin-ajax.php', 'relative' ),  
  165. 'actionadd' => $this->action_add,  
  166. 'actionremove' => $this->action_remove,  
  167. 'actionview' => $this->action_view,  
  168. 'actionreload' => $this->action_reload,  
  169. 'added_label' => apply_filters( 'yith_woocompare_compare_added_label', __( 'Added', 'yith-woocommerce-compare' ) ),  
  170. 'table_title' => apply_filters( 'yith_woocompare_compare_table_title', __( 'Product Comparison', 'yith-woocommerce-compare' ) ),  
  171. 'auto_open' => get_option( 'yith_woocompare_auto_open', 'yes' ),  
  172. 'loader' => YITH_WOOCOMPARE_ASSETS_URL . '/images/loader.gif',  
  173. 'button_text' => get_option('yith_woocompare_button_text'),  
  174. 'cookie_name' => $this->cookie_name 
  175. )); 
  176.  
  177. wp_localize_script( 'yith-woocompare-main', 'yith_woocompare', $args ); 
  178.  
  179. // colorbox 
  180. wp_enqueue_style( 'jquery-colorbox', YITH_WOOCOMPARE_ASSETS_URL . '/css/colorbox.css' ); 
  181. wp_enqueue_script( 'jquery-colorbox', YITH_WOOCOMPARE_ASSETS_URL . '/js/jquery.colorbox-min.js', array('jquery'), '1.4.21', true ); 
  182.  
  183. // widget 
  184. if ( is_active_widget( false, false, 'yith-woocompare-widget', true ) && ! is_admin() ) { 
  185. wp_enqueue_style( 'yith-woocompare-widget', YITH_WOOCOMPARE_ASSETS_URL . '/css/widget.css' ); 
  186.  
  187. /** 
  188. * The fields to show in the table 
  189. * @param array $products 
  190. * @return mixed|void 
  191. * @since 1.0.0 
  192. */ 
  193. public function fields( $products = array() ) { 
  194.  
  195. $fields = get_option( 'yith_woocompare_fields', array() ); 
  196.  
  197. foreach ( $fields as $field => $show ) { 
  198. if ( $show ) { 
  199. if ( isset( $this->default_fields[$field] ) ) { 
  200. $fields[$field] = $this->default_fields[$field]; 
  201. else { 
  202. if ( taxonomy_exists( $field ) ) { 
  203. $fields[$field] = wc_attribute_label( $field ); 
  204. else { 
  205. unset( $fields[$field] ); 
  206.  
  207. return apply_filters( 'yith_woocompare_filter_table_fields', $fields, $products ); 
  208.  
  209. /** 
  210. * Render the maintenance page 
  211. */ 
  212. public function compare_table_html() { 
  213.  
  214. if ( ( ! defined('DOING_AJAX') || ! DOING_AJAX ) && ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_view ) ) return; 
  215.  
  216. // check if is add to cart 
  217. if( isset( $_REQUEST['add-to-cart'] ) ) { 
  218. $product_id = intval( $_REQUEST['add-to-cart'] ); 
  219.  
  220. wp_redirect( get_permalink( $product_id ) ); 
  221. exit; 
  222.  
  223. global $woocommerce, $sitepress; 
  224.  
  225. /** 
  226. * WPML Suppot: Localize Ajax Call 
  227. */ 
  228. $lang = isset( $_REQUEST['lang'] ) ? $_REQUEST['lang'] : false; 
  229.  
  230. if( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress ) ) { 
  231. $sitepress->switch_lang( $lang, true ); 
  232.  
  233. extract( $this->_vars() ); 
  234.  
  235. // remove all styles from compare template 
  236. add_action('wp_print_styles', array( $this, 'remove_all_styles' ), 100); 
  237.  
  238. // remove admin bar 
  239. remove_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); 
  240. remove_action( 'wp_head', '_admin_bar_bump_cb' ); 
  241.  
  242. $plugin_path = YITH_WOOCOMPARE_TEMPLATE_PATH . '/' . $this->template_file; 
  243.  
  244. if ( defined('WC_TEMPLATE_PATH') ) { 
  245.  
  246. $template_path = get_template_directory() . '/' . WC_TEMPLATE_PATH . $this->template_file; 
  247. $child_path = get_stylesheet_directory() . '/' . WC_TEMPLATE_PATH . $this->template_file; 
  248. else { 
  249. $template_path = get_template_directory() . '/' . $woocommerce->template_url . $this->template_file; 
  250. $child_path = get_stylesheet_directory() . '/' . $woocommerce->template_url . $this->template_file; 
  251.  
  252. foreach ( array( 'child_path', 'template_path', 'plugin_path' ) as $var ) { 
  253. if ( file_exists( ${$var} ) ) { 
  254. include ${$var}; 
  255. exit(); 
  256.  
  257. /** 
  258. * Return the array with all products and all attributes values 
  259. * @param mixed $products 
  260. * @return array The complete list of products with all attributes value 
  261. */ 
  262. public function get_products_list( $products = array() ) { 
  263. $list = array(); 
  264.  
  265. empty( $products ) && $products = $this->products_list; 
  266. $products = apply_filters( 'yith_woocompare_exclude_products_from_list', $products ); 
  267.  
  268. $fields = $this->fields( $products ); 
  269.  
  270. foreach ( $products as $product_id ) { 
  271.  
  272. /** 
  273. * @type object $product /WC_Product 
  274. */ 
  275. $product = $this->wc_get_product( $product_id ); 
  276.  
  277. if ( ! $product ) 
  278. continue; 
  279.  
  280. $product->fields = array(); 
  281.  
  282. // custom attributes 
  283. foreach ( $fields as $field => $name ) { 
  284.  
  285. switch( $field ) { 
  286. case 'title': 
  287. $product->fields[$field] = $product->get_title(); 
  288. break; 
  289. case 'price': 
  290. $product->fields[$field] = $product->get_price_html(); 
  291. break; 
  292. case 'image': 
  293. $product->fields[$field] = intval( get_post_thumbnail_id( $product_id ) ); 
  294. break; 
  295. case 'description': 
  296. $description = apply_filters( 'woocommerce_short_description', yit_get_prop( $product, 'post_excerpt' ) ); 
  297. $product->fields[$field] = apply_filters( 'yith_woocompare_products_description', $description ); 
  298. break; 
  299. case 'stock': 
  300. $availability = $product->get_availability(); 
  301. if ( empty( $availability['availability'] ) ) { 
  302. $availability['availability'] = __( 'In stock', 'yith-woocommerce-compare' ); 
  303. $product->fields[$field] = sprintf( '<span>%s</span>', esc_html( $availability['availability'] ) ); 
  304. break; 
  305. case 'weight': 
  306. if( $weight = $product->get_weight() ) { 
  307. $weight = wc_format_localized_decimal( $weight ) . ' ' . esc_attr( get_option( 'woocommerce_weight_unit' ) ); 
  308. else { 
  309. $weight = '-'; 
  310. $product->fields[$field] = sprintf( '<span>%s</span>', esc_html( $weight ) ); 
  311. break; 
  312. case 'dimensions': 
  313. $dimensions = function_exists( 'wc_format_dimensions' ) ? wc_format_dimensions( $product->get_dimensions(false) ) : $product->get_dimensions(); 
  314. ! $dimensions && $dimensions = '-'; 
  315.  
  316. $product->fields[$field] = sprintf( '<span>%s</span>', esc_html( $dimensions ) ); 
  317. break; 
  318. default: 
  319. if ( taxonomy_exists( $field ) ) { 
  320. $product->fields[$field] = array(); 
  321. $terms = get_the_terms( $product_id, $field ); 
  322. if ( ! empty( $terms ) ) { 
  323. foreach ( $terms as $term ) { 
  324. $term = sanitize_term( $term, $field ); 
  325. $product->fields[$field][] = $term->name; 
  326. $product->fields[$field] = implode( ', ', $product->fields[$field] ); 
  327. else { 
  328. do_action_ref_array( 'yith_woocompare_field_' . $field, array( $product, &$product->fields ) ); 
  329. break; 
  330.  
  331. $list[ $product_id ] = $product; 
  332.  
  333. return $list; 
  334.  
  335. /** 
  336. * The URL of product comparison table 
  337. * @param bool | int $product_id 
  338. * @return string The url to add the product in the comparison table 
  339. */ 
  340. public function view_table_url( $product_id = false ) { 
  341. $url_args = array( 
  342. 'action' => $this->action_view,  
  343. 'iframe' => true 
  344. ); 
  345.  
  346. $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : false; 
  347. if( $lang ) { 
  348. $url_args['lang'] = $lang; 
  349.  
  350. return apply_filters( 'yith_woocompare_view_table_url', esc_url_raw( add_query_arg( $url_args, site_url() ) ), $product_id ); 
  351.  
  352. /** 
  353. * The URL to add the product into the comparison table 
  354. * @param int $product_id ID of the product to add 
  355. * @return string The url to add the product in the comparison table 
  356. */ 
  357. public function add_product_url( $product_id ) { 
  358. $url_args = array( 
  359. 'action' => $this->action_add,  
  360. 'id' => $product_id 
  361. ); 
  362.  
  363. $lang = defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : false; 
  364. if( $lang ) { 
  365. $url_args['lang'] = $lang; 
  366.  
  367. return apply_filters( 'yith_woocompare_add_product_url', esc_url_raw( add_query_arg( $url_args, site_url() ) ), $this->action_add ); 
  368.  
  369. /** 
  370. * The URL to remove the product into the comparison table 
  371. * @param string $product_id The ID of the product to remove 
  372. * @return string The url to remove the product in the comparison table 
  373. */ 
  374. public function remove_product_url( $product_id ) { 
  375. $url_args = array( 
  376. 'action' => $this->action_remove,  
  377. 'id' => $product_id 
  378. ); 
  379. return apply_filters( 'yith_woocompare_remove_product_url', esc_url_raw( add_query_arg( $url_args, site_url() ) ), $this->action_remove ); 
  380.  
  381. /** 
  382. * Add the link to compare 
  383. */ 
  384. public function add_compare_link( $product_id = false, $args = array() ) { 
  385. extract( $args ); 
  386.  
  387. if ( ! $product_id ) { 
  388. global $product; 
  389. $product_id = ! is_null( $product ) ? yit_get_prop( $product, 'id', true ) : 0; 
  390.  
  391. // return if product doesn't exist 
  392. if ( empty( $product_id ) || apply_filters( 'yith_woocompare_remove_compare_link_by_cat', false, $product_id ) ) 
  393. return; 
  394.  
  395. $is_button = ! isset( $button_or_link ) || ! $button_or_link ? get_option( 'yith_woocompare_is_button' ) : $button_or_link; 
  396.  
  397. if ( ! isset( $button_text ) || $button_text == 'default' ) { 
  398. $button_text = get_option( 'yith_woocompare_button_text', __( 'Compare', 'yith-woocommerce-compare' ) ); 
  399. do_action ( 'wpml_register_single_string', 'Plugins', 'plugin_yit_compare_button_text', $button_text ); 
  400. $button_text = apply_filters( 'wpml_translate_single_string', $button_text, 'Plugins', 'plugin_yit_compare_button_text' ); 
  401.  
  402. printf( '<a href="%s" class="%s" data-product_id="%d" rel="nofollow">%s</a>', $this->add_product_url( $product_id ), 'compare' . ( $is_button == 'button' ? ' button' : '' ), $product_id, $button_text ); 
  403.  
  404. /** 
  405. * Return the url of stylesheet position 
  406. */ 
  407. public function stylesheet_url() { 
  408. global $woocommerce; 
  409.  
  410. $filename = $this->stylesheet_file; 
  411.  
  412. $plugin_path = array( 'path' => YITH_WOOCOMPARE_DIR . '/assets/css/style.css', 'url' => YITH_WOOCOMPARE_ASSETS_URL . '/css/style.css' ); 
  413.  
  414. if ( defined('WC_TEMPLATE_PATH') ) { 
  415. $template_path = array( 'path' => get_template_directory() . '/' . WC_TEMPLATE_PATH . $filename, 'url' => get_template_directory_uri() . '/' . WC_TEMPLATE_PATH . $filename ); 
  416. $child_path = array( 'path' => get_stylesheet_directory() . '/' . WC_TEMPLATE_PATH . $filename, 'url' => get_stylesheet_directory_uri() . '/' . WC_TEMPLATE_PATH . $filename ); 
  417. else { 
  418. $template_path = array( 'path' => get_template_directory() . '/' . $woocommerce->template_url . $filename, 'url' => get_template_directory_uri() . '/' . $woocommerce->template_url . $filename ); 
  419. $child_path = array( 'path' => get_stylesheet_directory() . '/' . $woocommerce->template_url . $filename, 'url' => get_stylesheet_directory_uri() . '/' . $woocommerce->template_url . $filename ); 
  420.  
  421. foreach ( array( 'child_path', 'template_path', 'plugin_path' ) as $var ) { 
  422. if ( file_exists( ${$var}['path'] ) ) { 
  423. return ${$var}['url']; 
  424.  
  425.  
  426. /** 
  427. * Generate template vars 
  428. * @return array 
  429. * @since 1.0.0 
  430. * @access protected 
  431. */ 
  432. protected function _vars() { 
  433. $vars = array( 
  434. 'products' => $this->get_products_list(),  
  435. 'fields' => $this->fields(),  
  436. 'repeat_price' => get_option( 'yith_woocompare_price_end' ),  
  437. 'repeat_add_to_cart' => get_option( 'yith_woocompare_add_to_cart_end' ),  
  438. ); 
  439.  
  440. return $vars; 
  441.  
  442. /** 
  443. * The action called by the query string 
  444. */ 
  445. public function add_product_to_compare_action() { 
  446. if ( defined( 'DOING_AJAX' ) && DOING_AJAX || ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_add ) { 
  447. return; 
  448.  
  449. $product_id = intval( $_REQUEST['id'] ); 
  450. $product = $this->wc_get_product( $product_id ); 
  451.  
  452. // don't add the product if doesn't exist 
  453. if ( isset( $product->id ) && ! in_array( $product_id, $this->products_list ) ) { 
  454. $this->add_product_to_compare( $product_id ); 
  455.  
  456. wp_redirect( esc_url( remove_query_arg( array( 'id', 'action' ) ) ) ); 
  457. exit(); 
  458.  
  459. /** 
  460. * The action called by AJAX 
  461. */ 
  462. public function add_product_to_compare_ajax() { 
  463.  
  464. if( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_add ) { 
  465. die(); 
  466.  
  467. $product_id = intval( $_REQUEST['id'] ); 
  468. $product = $this->wc_get_product( $product_id ); 
  469.  
  470. // don't add the product if doesn't exist 
  471. if ( isset( $product->id ) && ! in_array( $product_id, $this->products_list ) ) { 
  472. $this->add_product_to_compare( $product_id ); 
  473.  
  474. do_action( 'yith_woocompare_add_product_action_ajax' ); 
  475.  
  476. $json = apply_filters( 'yith_woocompare_add_product_action_json', array( 
  477. 'table_url' => $this->view_table_url( $product_id ),  
  478. 'widget_table' => $this->list_products_html(),  
  479. ) ); 
  480.  
  481. echo json_encode( $json ); 
  482. die(); 
  483.  
  484. /** 
  485. * Add a product in the products comparison table 
  486. * @param int $product_id product ID to add in the comparison table 
  487. */ 
  488. public function add_product_to_compare( $product_id ) { 
  489.  
  490. $this->products_list[] = $product_id; 
  491. setcookie( $this->cookie_name, json_encode( $this->products_list ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); 
  492.  
  493. do_action( 'yith_woocompare_after_add_product', $product_id ); 
  494.  
  495. /** 
  496. * The action called by the query string 
  497. */ 
  498. public function remove_product_from_compare_action() { 
  499. if ( defined( 'DOING_AJAX' ) && DOING_AJAX || ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_remove ) { 
  500. return; 
  501.  
  502. $this->remove_product_from_compare( $_REQUEST['id'] ); 
  503.  
  504. // redirect 
  505. $redirect = esc_url( remove_query_arg( array( 'id', 'action' ) ) ); 
  506.  
  507. if ( isset( $_REQUEST['redirect'] ) && $_REQUEST['redirect'] == 'view' ) 
  508. $redirect = esc_url( remove_query_arg( 'redirect', add_query_arg( 'action', $this->action_view, $redirect ) ) ); 
  509.  
  510. wp_redirect( $redirect ); 
  511. exit(); 
  512.  
  513. /** 
  514. * The action called by AJAX 
  515. */ 
  516. public function remove_product_from_compare_ajax() { 
  517.  
  518. if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_remove ) { 
  519. die(); 
  520.  
  521. $lang = isset( $_REQUEST['lang'] ) ? $_REQUEST['lang'] : false; 
  522.  
  523. $this->remove_product_from_compare( $_REQUEST['id'] ); 
  524.  
  525. do_action( 'yith_woocompare_remove_product_action_ajax' ); 
  526.  
  527. header('Content-Type: text/html; charset=utf-8'); 
  528.  
  529. if ( isset( $_REQUEST['responseType'] ) && $_REQUEST['responseType'] == 'product_list' ) { 
  530. echo $this->list_products_html( $lang ); 
  531. } else { 
  532. $this->compare_table_html(); 
  533.  
  534. die(); 
  535.  
  536. /** 
  537. * Return the list of widget table, used in AJAX 
  538. */ 
  539. public function reload_widget_list_ajax() { 
  540.  
  541. if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != $this->action_reload ) { 
  542. die(); 
  543.  
  544. $lang = isset( $_REQUEST['lang'] ) ? $_REQUEST['lang'] : false; 
  545.  
  546. echo $this->list_products_html( $lang ); 
  547. die(); 
  548.  
  549. /** 
  550. * The list of products as HTML list 
  551. */ 
  552. public function list_products_html( $lang = false ) { 
  553. ob_start(); 
  554.  
  555. /** 
  556. * WPML Suppot: Localize Ajax Call 
  557. */ 
  558. global $sitepress; 
  559.  
  560. if( defined( 'ICL_LANGUAGE_CODE' ) && $lang && isset( $sitepress )) { 
  561. $sitepress->switch_lang( $lang, true ); 
  562.  
  563. if ( empty( $this->products_list ) ) { 
  564. echo '<li class="list_empty">' . __( 'No products to compare', 'yith-woocommerce-compare' ) . '</li>'; 
  565. return ob_get_clean(); 
  566.  
  567. foreach ( $this->products_list as $product_id ) { 
  568. /** 
  569. * @type object $product /WC_Product 
  570. */ 
  571. $product = $this->wc_get_product( $product_id ); 
  572. if ( ! $product ) 
  573. continue; 
  574. ?> 
  575. <li> 
  576. <a href="<?php echo $this->remove_product_url( $product_id ) ?>" data-product_id="<?php echo $product_id; ?>" class="remove" title="<?php _e( 'Remove', 'yith-woocommerce-compare' ) ?>">x</a> 
  577. <a class="title" href="<?php echo get_permalink( $product_id ) ?>"><?php echo $product->get_title() ?></a> 
  578. </li> 
  579. <?php 
  580.  
  581. $return = ob_get_clean(); 
  582.  
  583. return apply_filters( 'yith_woocompare_widget_products_html', $return, $this->products_list, $this ); 
  584.  
  585. /** 
  586. * Remove a product from the comparison table 
  587. * @param $product_id The product ID to remove from the comparison table 
  588. */ 
  589. public function remove_product_from_compare( $product_id ) { 
  590.  
  591. if( $product_id == 'all' ) { 
  592. $this->products_list = array(); 
  593. else { 
  594. foreach ( $this->products_list as $k => $id ) { 
  595. if ( intval( $product_id ) == $id ) { 
  596. unset( $this->products_list[ $k ] ); 
  597.  
  598. setcookie( $this->cookie_name, json_encode( array_values( $this->products_list ) ), 0, COOKIEPATH, COOKIE_DOMAIN, false, false ); 
  599.  
  600. do_action( 'yith_woocompare_after_remove_product', $product_id ); 
  601.  
  602. /** 
  603. * Remove all styles from the compare template 
  604. */ 
  605. public function remove_all_styles() { 
  606. global $wp_styles; 
  607. $wp_styles->queue = array(); 
  608.  
  609. /** 
  610. * Show the html for the shortcode 
  611. */ 
  612. public function compare_button_sc( $atts, $content = null ) { 
  613. $atts = shortcode_atts(array( 
  614. 'product' => false,  
  615. 'type' => 'default',  
  616. 'container' => 'yes' 
  617. ), $atts); 
  618.  
  619. $product_id = 0; 
  620.  
  621. /** 
  622. * Retrieve the product ID in these steps: 
  623. * - If "product" attribute is not set, get the product ID of current product loop 
  624. * - If "product" contains ID, post slug or post title 
  625. */ 
  626. if ( ! $atts['product'] ) { 
  627. global $product; 
  628. $product_id = ! is_null( $product ) ? yit_get_prop( $product, 'id', true ) : 0; 
  629. } else { 
  630. global $wpdb; 
  631. $product = $wpdb->get_row( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d OR post_name = %s OR post_title = %s LIMIT 1", $atts['product'], $atts['product'], $atts['product'] ) ); 
  632. if ( ! empty( $product ) ) { 
  633. $product_id = $product->ID; 
  634.  
  635. // if product ID is 0, maybe the product doesn't exists or is wrong.. in this case, doesn't show the button 
  636. if ( empty( $product_id ) ) 
  637. return ''; 
  638.  
  639. ob_start(); 
  640. if ( $atts['container'] == 'yes' ) echo '<div class="woocommerce product compare-button">'; 
  641. $this->add_compare_link( $product_id, array( 
  642. 'button_or_link' => ( $atts['type'] == 'default' ? false : $atts['type'] ),  
  643. 'button_text' => empty( $content ) ? 'default' : $content 
  644. ) ); 
  645. if ( $atts['container'] == 'yes' ) echo '</div>'; 
  646. return ob_get_clean(); 
  647.  
  648. public function wc_get_product( $product_id ) { 
  649. $wc_get_product = function_exists( 'wc_get_product' ) ? 'wc_get_product' : 'get_product'; 
  650. return $wc_get_product( $product_id );