/classes/class-wc-compare-filter.php

  1. <?php 
  2. /** "Copyright 2012 A3 Revolution Web Design" This software is distributed under the terms of GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 */ 
  3. /** 
  4. * WooCommerce Compare Hook Filter 
  5. * 
  6. * Hook anf Filter into woocommerce plugin 
  7. * 
  8. * Table Of Contents 
  9. * 
  10. * register_admin_screen() 
  11. * template_loader() 
  12. * add_google_fonts() 
  13. * include_customized_style() 
  14. * woocp_shop_add_compare_button() 
  15. * woocp_shop_add_compare_button_below_cart() 
  16. * woocp_details_add_compare_button() 
  17. * woocp_details_add_compare_button_below_cart() 
  18. * add_compare_button() 
  19. * show_compare_fields() 
  20. * woocp_variable_ajax_add_to_cart() 
  21. * woocp_add_to_compare() 
  22. * woocp_remove_from_popup_compare() 
  23. * woocp_update_compare_popup() 
  24. * woocp_update_compare_widget() 
  25. * woocp_update_total_compare() 
  26. * woocp_remove_from_compare() 
  27. * woocp_clear_compare() 
  28. * woocp_footer_script() 
  29. * woocp_variable_add_to_cart_script() 
  30. * woocp_product_featured_tab() 
  31. * woocp_product_featured_tab_woo_2_0() 
  32. * woocp_product_featured_panel() 
  33. * woocp_product_featured_panel_woo_2_0() 
  34. * woocp_set_selected_attributes() 
  35. * auto_create_compare_category() 
  36. * auto_create_compare_feature() 
  37. * a3_wp_admin() 
  38. * admin_sidebar_menu_css() 
  39. * plugin_extra_links() 
  40. */ 
  41. class WC_Compare_Hook_Filter 
  42. public static function register_admin_screen () { 
  43.  
  44. $product_comparison = add_menu_page( __('Product Comparison', 'woocommerce-compare-products' ), __('WC Compare', 'woocommerce-compare-products' ), 'manage_options', 'woo-compare-features', array( 'WC_Compare_Features_Panel', 'admin_screen' ), null, '55.222'); 
  45.  
  46. $compare_features = add_submenu_page('woo-compare-features', __( 'Compare Category & Feature', 'woocommerce-compare-products' ), __( 'Category & Feature', 'woocommerce-compare-products' ), 'manage_options', 'woo-compare-features', array( 'WC_Compare_Features_Panel', 'admin_screen' ) ); 
  47.  
  48. $compare_products = add_submenu_page('woo-compare-features', __( 'Compare Products Manager', 'woocommerce-compare-products' ), __( 'Product Manager', 'woocommerce-compare-products' ), 'manage_options', 'woo-compare-products', array( 'WC_Compare_Products_Class', 'woocp_products_manager' ) ); 
  49.  
  50. } // End register_admin_screen() 
  51.  
  52. public static function template_loader( $template ) { 
  53. global $product_compare_id; 
  54. global $post; 
  55. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  56.  
  57. if ( is_object( $post ) && $product_compare_id == $post->ID ) { 
  58.  
  59. if ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) { 
  60. $file = 'product-compare-old.php'; 
  61. } else { 
  62. $file = 'product-compare.php'; 
  63. $find[] = $file; 
  64. $find[] = apply_filters( 'woocommerce_template_url', 'woocommerce/' ) . $file; 
  65.  
  66. $template = locate_template( $find ); 
  67. if ( ! $template ) $template = WOOCP_FILE_PATH . '/templates/' . $file; 
  68.  
  69.  
  70. return $template; 
  71.  
  72. public static function nocache_ours_page() { 
  73. global $product_compare_id; 
  74.  
  75. $woocp_page_uris = array(); 
  76. // Exclude querystring when using page ID 
  77. $woocp_page_uris[] = 'p=' . $product_compare_id; 
  78. $woocp_page_uris[] = 'page_id=' . $product_compare_id; 
  79.  
  80. // Exclude permalinks 
  81. $comparision_page = get_post( $product_compare_id ); 
  82.  
  83. if ( ! is_null( $comparision_page ) ) 
  84. $woocp_page_uris[] = '/' . $comparision_page->post_name; 
  85.  
  86. if ( is_array( $woocp_page_uris ) ) { 
  87. foreach( $woocp_page_uris as $uri ) { 
  88. if ( strstr( $_SERVER['REQUEST_URI'], $uri ) ) { 
  89. if ( ! defined( 'DONOTCACHEPAGE' ) ) 
  90. define( "DONOTCACHEPAGE", "true" ); 
  91.  
  92. if ( ! defined( 'DONOTCACHEOBJECT' ) ) 
  93. define( "DONOTCACHEOBJECT", "true" ); 
  94.  
  95. if ( ! defined( 'DONOTCACHEDB' ) ) 
  96. define( "DONOTCACHEDB", "true" ); 
  97.  
  98. nocache_headers(); 
  99.  
  100. public static function add_google_fonts() { 
  101. global $wc_compare_fonts_face; 
  102. global $woo_compare_product_page_settings; 
  103. $google_fonts = array(  
  104. $woo_compare_product_page_settings['product_compare_link_font']['face'],  
  105. $woo_compare_product_page_settings['product_compare_button_font']['face'],  
  106. $woo_compare_product_page_settings['product_view_compare_link_font']['face'],  
  107. $woo_compare_product_page_settings['product_view_button_font']['face'],  
  108. ); 
  109.  
  110. $google_fonts = apply_filters( 'wc_compare_google_fonts', $google_fonts ); 
  111.  
  112. $wc_compare_fonts_face->generate_google_webfonts( $google_fonts ); 
  113.  
  114. public static function add_google_fonts_comparison_page() { 
  115. global $wc_compare_fonts_face; 
  116. global $woo_compare_comparison_page_global_settings; 
  117. $google_fonts = array(  
  118. $woo_compare_comparison_page_global_settings['no_product_message_font']['face'],  
  119. ); 
  120.  
  121. $google_fonts = apply_filters( 'wc_comparison_page_google_fonts', $google_fonts ); 
  122.  
  123. $wc_compare_fonts_face->generate_google_webfonts( $google_fonts ); 
  124.  
  125. public static function include_customized_style() { 
  126. include( WOOCP_DIR. '/templates/customized_style.php' ); 
  127.  
  128. public static function woocp_shop_add_compare_button($template_name, $template_path, $located) { 
  129. global $post; 
  130. global $product; 
  131. global $woo_compare_grid_view_settings; 
  132. global $woo_compare_comparison_page_global_settings; 
  133. global $product_compare_id; 
  134. extract($woo_compare_grid_view_settings); 
  135. if ($template_name == 'loop/add-to-cart.php') { 
  136. $product_id = $product->get_id(); 
  137. if (($post->post_type == 'product' || $post->post_type == 'product_variation') && WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  138. $compare_grid_view_custom_class = ''; 
  139. $compare_grid_view_text = __('Compare This', 'woocommerce-compare-products' ); 
  140. $compare_grid_view_class = 'woo_bt_compare_this_button'; 
  141.  
  142. $view_compare_html = ''; 
  143.  
  144. $compare_html = '<div class="woo_grid_compare_button_container"><a class="woo_bt_compare_this '.$compare_grid_view_class.' '.$compare_grid_view_custom_class.'" id="woo_bt_compare_this_'.$product_id.'">'.$compare_grid_view_text.'</a>' . $view_compare_html . '<input type="hidden" id="input_woo_bt_compare_this_'.$product_id.'" name="product_compare_'.$product_id.'" value="'.$product_id.'" /></div>'; 
  145. echo $compare_html; 
  146.  
  147. public static function woocp_shop_add_compare_button_below_cart() { 
  148. global $post; 
  149. global $product; 
  150. global $woo_compare_grid_view_settings; 
  151. global $woo_compare_comparison_page_global_settings; 
  152. global $product_compare_id; 
  153.  
  154. if ( $woo_compare_grid_view_settings['disable_grid_view_compare'] == 1 || $woo_compare_grid_view_settings['grid_view_button_position'] == 'above' ) return; 
  155.  
  156. extract($woo_compare_grid_view_settings); 
  157. $product_id = $product->get_id(); 
  158. if (($post->post_type == 'product' || $post->post_type == 'product_variation') && WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  159. $compare_grid_view_custom_class = ''; 
  160. $compare_grid_view_text = __('Compare This', 'woocommerce-compare-products' ); 
  161. $compare_grid_view_class = 'woo_bt_compare_this_button'; 
  162.  
  163. $view_compare_html = ''; 
  164.  
  165. $compare_html = '<div class="woo_grid_compare_button_container"><a class="woo_bt_compare_this '.$compare_grid_view_class.' '.$compare_grid_view_custom_class.'" id="woo_bt_compare_this_'.$product_id.'">'.$compare_grid_view_text.'</a>' . $view_compare_html . '<input type="hidden" id="input_woo_bt_compare_this_'.$product_id.'" name="product_compare_'.$product_id.'" value="'.$product_id.'" /></div>'; 
  166. echo $compare_html; 
  167.  
  168. public static function woocp_details_add_compare_button() { 
  169. global $post; 
  170. global $product; 
  171. global $woo_compare_product_page_settings; 
  172. global $woo_compare_comparison_page_global_settings; 
  173. global $product_compare_id; 
  174. $product_id = $product->get_id(); 
  175. if (($post->post_type == 'product' || $post->post_type == 'product_variation') && WC_Compare_Functions::check_product_activate_compare($product_id) && $woo_compare_product_page_settings['auto_add'] == 'yes' && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  176.  
  177. $widget_compare_popup_view_button = ''; 
  178. if ( $woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page' ) $widget_compare_popup_view_button = 'woo_bt_view_compare_popup'; 
  179.  
  180. $product_compare_custom_class = ''; 
  181. $product_compare_text = $woo_compare_product_page_settings['product_compare_button_text']; 
  182. $product_compare_class = 'woo_bt_compare_this_button'; 
  183. if ($woo_compare_product_page_settings['product_compare_button_type'] == 'link') { 
  184. $product_compare_custom_class = ''; 
  185. $product_compare_text = $woo_compare_product_page_settings['product_compare_link_text']; 
  186. $product_compare_class = 'woo_bt_compare_this_link'; 
  187.  
  188. $view_compare_html = ''; 
  189. if ($woo_compare_product_page_settings['disable_product_view_compare'] == 0) { 
  190. $product_view_compare_custom_class = ''; 
  191. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_link_text']; 
  192. $product_view_compare_class = 'woo_bt_view_compare_link'; 
  193. if ($woo_compare_product_page_settings['product_view_compare_button_type'] == 'button') { 
  194. $product_view_compare_custom_class = ''; 
  195. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_button_text']; 
  196. $product_view_compare_class = 'woo_bt_view_compare_button'; 
  197. $product_compare_page = get_permalink($product_compare_id); 
  198. if ($woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page') { 
  199. $product_compare_page = '#'; 
  200. $view_compare_html = '<div style="clear:both;"></div><a class="woo_bt_view_compare '.$widget_compare_popup_view_button.' '.$product_view_compare_class.' '.$product_view_compare_custom_class.'" href="'.$product_compare_page.'" target="_blank" alt="" title="" style="display:none;">'.$product_view_compare_text.'</a>'; 
  201. $compare_html = '<div class="woo_compare_button_container"><a class="woo_bt_compare_this '.$product_compare_class.' '.$product_compare_custom_class.'" id="woo_bt_compare_this_'.$product_id.'">'.$product_compare_text.'</a>' . $view_compare_html . '<input type="hidden" id="input_woo_bt_compare_this_'.$product_id.'" name="product_compare_'.$product_id.'" value="'.$product_id.'" /></div>'; 
  202. echo $compare_html; 
  203.  
  204. public static function woocp_details_add_compare_button_below_cart($template_name, $template_path, $located) { 
  205. global $post; 
  206. global $product; 
  207. global $woo_compare_product_page_settings; 
  208. global $woo_compare_comparison_page_global_settings; 
  209. global $product_compare_id; 
  210. if (in_array($template_name, array('single-product/add-to-cart/simple.php', 'single-product/add-to-cart/grouped.php', 'single-product/add-to-cart/external.php', 'single-product/add-to-cart/variable.php'))) { 
  211. $product_id = $product->get_id(); 
  212. if (($post->post_type == 'product' || $post->post_type == 'product_variation') && WC_Compare_Functions::check_product_activate_compare($product_id) && $woo_compare_product_page_settings['auto_add'] == 'yes' && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  213.  
  214. $widget_compare_popup_view_button = ''; 
  215. if ( $woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page' ) $widget_compare_popup_view_button = 'woo_bt_view_compare_popup'; 
  216.  
  217. $product_compare_custom_class = ''; 
  218. $product_compare_text = $woo_compare_product_page_settings['product_compare_button_text']; 
  219. $product_compare_class = 'woo_bt_compare_this_button'; 
  220. if ($woo_compare_product_page_settings['product_compare_button_type'] == 'link') { 
  221. $product_compare_custom_class = ''; 
  222. $product_compare_text = $woo_compare_product_page_settings['product_compare_link_text']; 
  223. $product_compare_class = 'woo_bt_compare_this_link'; 
  224.  
  225. $view_compare_html = ''; 
  226. if ($woo_compare_product_page_settings['disable_product_view_compare'] == 0) { 
  227. $product_view_compare_custom_class = ''; 
  228. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_link_text']; 
  229. $product_view_compare_class = 'woo_bt_view_compare_link'; 
  230. if ($woo_compare_product_page_settings['product_view_compare_button_type'] == 'button') { 
  231. $product_view_compare_custom_class = ''; 
  232. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_button_text']; 
  233. $product_view_compare_class = 'woo_bt_view_compare_button'; 
  234. $product_compare_page = get_permalink($product_compare_id); 
  235. if ($woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page') { 
  236. $product_compare_page = '#'; 
  237. $view_compare_html = '<div style="clear:both;"></div><a class="woo_bt_view_compare '.$widget_compare_popup_view_button.' '.$product_view_compare_class.' '.$product_view_compare_custom_class.'" href="'.$product_compare_page.'" target="_blank" alt="" title="" style="display:none;">'.$product_view_compare_text.'</a>'; 
  238.  
  239. $compare_html = '<div class="woo_compare_button_container"><a class="woo_bt_compare_this '.$product_compare_class.' '.$product_compare_custom_class.'" id="woo_bt_compare_this_'.$product_id.'">'.$product_compare_text.'</a>' . $view_compare_html . '<input type="hidden" id="input_woo_bt_compare_this_'.$product_id.'" name="product_compare_'.$product_id.'" value="'.$product_id.'" /></div>'; 
  240. echo $compare_html; 
  241.  
  242. public static function add_compare_button($product_id='') { 
  243. global $post; 
  244. global $woo_compare_product_page_settings; 
  245. global $woo_compare_comparison_page_global_settings; 
  246. global $product_compare_id; 
  247. if (trim($product_id) == '') $product_id = $post->ID; 
  248. $post_type = get_post_type($product_id); 
  249. $html = ''; 
  250. if (($post_type == 'product' || $post_type == 'product_variation') && WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  251.  
  252. $widget_compare_popup_view_button = ''; 
  253. if ( $woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page' ) $widget_compare_popup_view_button = 'woo_bt_view_compare_popup'; 
  254.  
  255. $product_compare_custom_class = ''; 
  256. $product_compare_text = $woo_compare_product_page_settings['product_compare_button_text']; 
  257. $product_compare_class = 'woo_bt_compare_this_button'; 
  258. if ($woo_compare_product_page_settings['product_compare_button_type'] == 'link') { 
  259. $product_compare_custom_class = ''; 
  260. $product_compare_text = $woo_compare_product_page_settings['product_compare_link_text']; 
  261. $product_compare_class = 'woo_bt_compare_this_link'; 
  262.  
  263. $view_compare_html = ''; 
  264. if ($woo_compare_product_page_settings['disable_product_view_compare'] == 0) { 
  265. $product_view_compare_custom_class = ''; 
  266. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_link_text']; 
  267. $product_view_compare_class = 'woo_bt_view_compare_link'; 
  268. if ($woo_compare_product_page_settings['product_view_compare_button_type'] == 'button') { 
  269. $product_view_compare_custom_class = ''; 
  270. $product_view_compare_text = $woo_compare_product_page_settings['product_view_compare_button_text']; 
  271. $product_view_compare_class = 'woo_bt_view_compare_button'; 
  272. $product_compare_page = get_permalink($product_compare_id); 
  273. if ($woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page') { 
  274. $product_compare_page = '#'; 
  275. $view_compare_html = '<div style="clear:both;"></div><a class="woo_bt_view_compare '.$widget_compare_popup_view_button.' '.$product_view_compare_class.' '.$product_view_compare_custom_class.'" href="'.$product_compare_page.'" target="_blank" alt="" title="" style="display:none;">'.$product_view_compare_text.'</a>'; 
  276.  
  277. $html .= '<div class="woo_compare_button_container"><a class="woo_bt_compare_this '.$product_compare_class.' '.$product_compare_custom_class.'" id="woo_bt_compare_this_'.$product_id.'">'.$product_compare_text.'</a>' . $view_compare_html . '<input type="hidden" id="input_woo_bt_compare_this_'.$product_id.'" name="product_compare_'.$product_id.'" value="'.$product_id.'" /></div>'; 
  278.  
  279. return $html; 
  280.  
  281. public static function show_compare_fields($product_id='', $use_wootheme_style=true) { 
  282. global $post, $woo_compare_comparison_page_global_settings; 
  283. if (trim($product_id) == '') $product_id = $post->ID; 
  284. $html = ''; 
  285. $variations_list = WC_Compare_Functions::get_variations($product_id); 
  286. if (is_array($variations_list) && count($variations_list) > 0) { 
  287. foreach ($variations_list as $variation_id) { 
  288. if (WC_Compare_Functions::check_product_activate_compare($variation_id) && WC_Compare_Functions::check_product_have_cat($variation_id)) { 
  289. $compare_category = get_post_meta( $variation_id, '_woo_compare_category', true ); 
  290. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  291. if (is_array($compare_fields) && count($compare_fields)>0) { 
  292. $html .= '<div class="compare_product_variation"><h2>'.WC_Compare_Functions::get_variation_name($variation_id).'</h2></div>'; 
  293. if ($use_wootheme_style)  
  294. $html .= '<table class="compare_featured_fields shop_attributes">';  
  295. else  
  296. $html .= '<ul class="compare_featured_fields">'; 
  297. $fixed_width = ' width="60%"'; 
  298. foreach ($compare_fields as $field_data) { 
  299. $field_value = get_post_meta( $variation_id, '_woo_compare_'.$field_data->field_key, true ); 
  300. if (is_serialized($field_value)) $field_value = maybe_unserialize($field_value); 
  301. if (is_array($field_value) && count($field_value) > 0) $field_value = implode(', ', $field_value); 
  302. elseif (is_array($field_value) && count($field_value) < 0) $field_value = $woo_compare_comparison_page_global_settings['empty_text']; 
  303. if (trim($field_value) == '') $field_value = $woo_compare_comparison_page_global_settings['empty_text']; 
  304. $field_unit = ''; 
  305. if (trim($field_data->field_unit) != '') $field_unit = ' <span class="compare_featured_unit">('.trim(stripslashes($field_data->field_unit)).')</span>'; 
  306. if ($use_wootheme_style)  
  307. $html .= '<tr><th><span class="compare_featured_name">'.stripslashes($field_data->field_name).'</span>'.$field_unit.'</th><td '.$fixed_width.'><span class="compare_featured_value">'.$field_value.'</span></td></tr>'; 
  308. else 
  309. $html .= '<li class="compare_featured_item"><span class="compare_featured_name"><strong>'.stripslashes($field_data->field_name).'</strong>'.$field_unit.'</span> : <span class="compare_featured_value">'.$field_value.'</span></li>'; 
  310. $fixed_width = ''; 
  311. if ($use_wootheme_style)  
  312. $html .= '</table>'; 
  313. else  
  314. $html .= '</ul>'; 
  315. }elseif (WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  316. $compare_category = get_post_meta( $product_id, '_woo_compare_category', true ); 
  317. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  318. if (is_array($compare_fields) && count($compare_fields)>0) { 
  319. if ($use_wootheme_style)  
  320. $html .= '<table class="compare_featured_fields shop_attributes">';  
  321. else  
  322. $html .= '<ul class="compare_featured_fields">'; 
  323. $fixed_width = ' width="60%"'; 
  324. foreach ($compare_fields as $field_data) { 
  325. $field_value = get_post_meta( $product_id, '_woo_compare_'.$field_data->field_key, true ); 
  326. if (is_serialized($field_value)) $field_value = maybe_unserialize($field_value); 
  327. if (is_array($field_value) && count($field_value) > 0) $field_value = implode(', ', $field_value); 
  328. elseif (is_array($field_value) && count($field_value) < 0) $field_value = $woo_compare_comparison_page_global_settings['empty_text']; 
  329. if (trim($field_value) == '') $field_value = $woo_compare_comparison_page_global_settings['empty_text']; 
  330. $field_unit = ''; 
  331. if (trim($field_data->field_unit) != '') $field_unit = ' <span class="compare_featured_unit">('.trim(stripslashes($field_data->field_unit)).')</span>'; 
  332. if ($use_wootheme_style)  
  333. $html .= '<tr><th><span class="compare_featured_name">'.stripslashes($field_data->field_name).'</span>'.$field_unit.'</th><td '.$fixed_width.'><span class="compare_featured_value">'.$field_value.'</span></td></tr>'; 
  334. else 
  335. $html .= '<li class="compare_featured_item"><span class="compare_featured_name"><strong>'.stripslashes($field_data->field_name).'</strong>'.$field_unit.'</span> : <span class="compare_featured_value">'.$field_value.'</span></li>'; 
  336. $fixed_width = ''; 
  337. if ($use_wootheme_style)  
  338. $html .= '</table>'; 
  339. else  
  340. $html .= '</ul>'; 
  341. return $html; 
  342.  
  343. public static function woocp_variable_ajax_add_to_cart() { 
  344.  
  345. // Get product ID to add and quantity 
  346. $variation_id = (int) $_REQUEST['product_id']; 
  347. $mypost = get_post($variation_id); 
  348. $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $mypost->post_parent); 
  349. $quantity = (isset($_REQUEST['quantity'])) ? (int) $_REQUEST['quantity'] : 1; 
  350. $attributes = (array) maybe_unserialize(get_post_meta($product_id, '_product_attributes', true)); 
  351. $variations = array(); 
  352.  
  353. $my_variation = new WC_Product_Variation($variation_id, $product_id); 
  354. $variation_data = $my_variation->get_variation_attributes(); 
  355.  
  356. // Add to cart validation 
  357. $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); 
  358.  
  359. if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation_data)) { 
  360. // Return html fragments 
  361. $data = apply_filters('add_to_cart_fragments', array()); 
  362.  
  363. // For WC 2.3 
  364. $data = apply_filters('woocommerce_add_to_cart_fragments', $data); 
  365. } else { 
  366. $data = array( 
  367. 'error' => true,  
  368. 'product_url' => get_permalink( $product_id ) 
  369. ); 
  370.  
  371. echo json_encode( $data ); 
  372. die(); 
  373.  
  374. public static function woocp_add_to_compare() { 
  375.  
  376. $product_id = $_REQUEST['product_id']; 
  377. WC_Compare_Functions::add_product_to_compare_list($product_id); 
  378.  
  379. die(); 
  380.  
  381. public static function woocp_remove_from_popup_compare() { 
  382.  
  383. $product_id = $_REQUEST['product_id']; 
  384. WC_Compare_Functions::delete_product_on_compare_list($product_id); 
  385.  
  386. die(); 
  387.  
  388. public static function woocp_update_compare_popup() { 
  389. $result = WC_Compare_Functions::get_compare_list_html_popup(); 
  390. $result .= '<script src="'. WOOCP_JS_URL.'/fixedcolumntable/fixedcolumntable.js"></script>'; 
  391. echo json_encode( $result ); 
  392. die(); 
  393.  
  394. public static function woocp_update_compare_widget() { 
  395. $result = WC_Compare_Functions::get_compare_list_html_widget(); 
  396. echo json_encode( $result ); 
  397. die(); 
  398.  
  399. public static function woocp_update_total_compare() { 
  400. $result = WC_Compare_Functions::get_total_compare_list(); 
  401. echo json_encode( $result ); 
  402. die(); 
  403.  
  404. public static function woocp_remove_from_compare() { 
  405. $product_id = $_REQUEST['product_id']; 
  406. WC_Compare_Functions::delete_product_on_compare_list($product_id); 
  407. die(); 
  408.  
  409. public static function woocp_clear_compare() { 
  410. WC_Compare_Functions::clear_compare_list(); 
  411. die(); 
  412.  
  413. public static function woocp_footer_script() { 
  414. global $product_compare_id; 
  415. global $woo_compare_comparison_page_global_settings; 
  416. $woocp_compare_events = wp_create_nonce("woocp-compare-events"); 
  417. $woocp_compare_popup = wp_create_nonce("woocp-compare-popup"); 
  418.  
  419. $script_add_on = ''; 
  420. $script_add_on .= '<script type="text/javascript"> 
  421. jQuery(document).ready(function($) { 
  422. var ajax_url = "'.admin_url( 'admin-ajax.php', 'relative' ).'"; 
  423. woo_compare_widget_load();'; 
  424.  
  425. $script_add_on .= ' 
  426. $(document).on("click", ".woo_compare_popup_button_go, .woo_bt_view_compare_popup", function (event) { 
  427. var compare_url = "'.get_permalink($product_compare_id).'"; 
  428. window.open(compare_url, "'.__('Product_Comparison', 'woocommerce-compare-products' ).'", "scrollbars=1, width=980, height=650"); 
  429. event.preventDefault(); 
  430. return false; 
  431.  
  432. });'; 
  433.  
  434. $script_add_on .= ' 
  435. $(document).on("click", ".woo_bt_compare_this", function() { 
  436. var woo_bt_compare_current = $(this); 
  437. var product_id = $("#input_"+$(this).attr("id")).val(); 
  438. $(".woo_compare_widget_loader").show(); 
  439. $(".woo_compare_widget_container").html(""); 
  440. var data = { 
  441. action: "woocp_add_to_compare",  
  442. product_id: product_id,  
  443. security: "'.$woocp_compare_events.'" 
  444. }; 
  445. $.post( ajax_url, data, function(response) { 
  446. //woo_bt_compare_current.siblings(".woo_add_compare_success").show(); 
  447. woo_bt_compare_current.addClass("compared"); 
  448. woo_bt_compare_current.siblings(".woo_bt_view_compare").show(); 
  449. //setTimeout(function() { 
  450. // woo_bt_compare_current.siblings(".woo_add_compare_success").hide(); 
  451. //}, 3000); 
  452. data = { 
  453. action: "woocp_update_compare_widget",  
  454. security: "'.$woocp_compare_events.'" 
  455. }; 
  456. $.post( ajax_url, data, function(response) { 
  457. result = $.parseJSON( response ); 
  458. $(".woo_compare_widget_loader").hide(); 
  459. $(".woo_compare_widget_container").html(result); 
  460. }); 
  461. woo_update_total_compare_list(); 
  462. }); 
  463. }); 
  464.  
  465. $(document).on("click", ".woo_compare_remove_product", function() { 
  466. var remove_product_id = $(this).attr("rel"); 
  467. $(".woo_compare_widget_loader").show(); 
  468. $(".woo_compare_widget_container").html(""); 
  469. var data = { 
  470. action: "woocp_remove_from_compare",  
  471. product_id: remove_product_id,  
  472. security: "'.$woocp_compare_events.'" 
  473. }; 
  474. $.post( ajax_url, data, function(response) { 
  475. data = { 
  476. action: "woocp_update_compare_widget",  
  477. security: "'.$woocp_compare_events.'" 
  478. }; 
  479. $.post( ajax_url, data, function(response) { 
  480. result = $.parseJSON( response ); 
  481. $(".woo_compare_widget_loader").hide(); 
  482. $(".woo_compare_widget_container").html(result); 
  483. }); 
  484. woo_update_total_compare_list(); 
  485. }); 
  486. }); 
  487. $(document).on("click", ".woo_compare_clear_all", function() { 
  488. $(".woo_compare_widget_loader").show(); 
  489. $(".woo_compare_widget_container").html(""); 
  490. var data = { 
  491. action: "woocp_clear_compare",  
  492. security: "'.$woocp_compare_events.'" 
  493. }; 
  494. $.post( ajax_url, data, function(response) { 
  495. data = { 
  496. action: "woocp_update_compare_widget",  
  497. security: "'.$woocp_compare_events.'" 
  498. }; 
  499. $.post( ajax_url, data, function(response) { 
  500. result = $.parseJSON( response ); 
  501. $(".woo_compare_widget_loader").hide(); 
  502. $(".woo_compare_widget_container").html(result); 
  503. }); 
  504. woo_update_total_compare_list(); 
  505. }); 
  506. }); 
  507.  
  508. function woo_update_total_compare_list() { 
  509. var data = { 
  510. action: "woocp_update_total_compare",  
  511. security: "'.$woocp_compare_events.'" 
  512. }; 
  513. $.post( ajax_url, data, function(response) { 
  514. total_compare = $.parseJSON( response ); 
  515. $("#total_compare_product").html(total_compare); 
  516. }); 
  517.  
  518. function woo_compare_widget_load() { 
  519. $(".woo_compare_widget_loader").show(); 
  520. $(".woo_compare_widget_container").html(""); 
  521. var data = { 
  522. action: "woocp_update_compare_widget",  
  523. security: "'.$woocp_compare_events.'" 
  524. }; 
  525. $.post( ajax_url, data, function(response) { 
  526. result = $.parseJSON( response ); 
  527. $(".woo_compare_widget_loader").hide(); 
  528. $(".woo_compare_widget_container").html(result); 
  529. }); 
  530. woo_update_total_compare_list(); 
  531.  
  532. }); 
  533. </script>'; 
  534. echo $script_add_on; 
  535.  
  536. public static function woocp_variable_add_to_cart_script() { 
  537. $woocp_add_to_cart_nonce = wp_create_nonce("woocp-add-to-cart"); 
  538. $script_add_on = ''; 
  539. $script_add_on .= '<script type="text/javascript"> 
  540. (function($) { 
  541. $(function() { 
  542. if ( typeof wc_add_to_cart_params !== "undefined" ) { 
  543.  
  544. // Ajax add to cart 
  545. $(document).on("click", ".add_to_cart_button", function() { 
  546.  
  547. // AJAX add to cart request 
  548. var $thisbutton = $(this); 
  549.  
  550. if ($thisbutton.is(".product_type_variation")) { 
  551. if (!$thisbutton.attr("data-product_id")) return true; 
  552.  
  553. $thisbutton.removeClass("added"); 
  554. $thisbutton.addClass("loading"); 
  555.  
  556. var data = { 
  557. action: "woocp_variable_add_to_cart",  
  558. product_id: $thisbutton.attr("data-product_id"),  
  559. security: "'.$woocp_add_to_cart_nonce.'" 
  560. }; 
  561.  
  562. // Trigger event 
  563. $("body").trigger("adding_to_cart"); 
  564.  
  565. // Ajax action 
  566. $.post( wc_add_to_cart_params.ajax_url, data, function(response) { 
  567.  
  568. $thisbutton.removeClass("loading"); 
  569.  
  570. // Get response 
  571. data = $.parseJSON( response ); 
  572.  
  573. if (data.error && data.product_url) { 
  574. window.location = data.product_url; 
  575. return; 
  576.  
  577. fragments = data; 
  578.  
  579. // Block fragments class 
  580. if (fragments) { 
  581. $.each(fragments, function(key, value) { 
  582. $(key).addClass("updating"); 
  583. }); 
  584.  
  585. // Block widgets and fragments 
  586. $(".widget_shopping_cart, .shop_table.cart, .updating, .cart_totals").fadeTo("400", "0.6").block({message: null, overlayCSS: {background: "transparent url('.str_replace( array( 'http:', 'https:' ), '', WC()->plugin_url() ).'/assets/images/ajax-loader.gif) no-repeat center", opacity: 0.6}}); 
  587.  
  588. // Changes button classes 
  589. $thisbutton.addClass("added"); 
  590.  
  591. // Cart widget load 
  592. if ($(".widget_shopping_cart").size()>0) { 
  593. $(".widget_shopping_cart:eq(0)").load( window.location + " .widget_shopping_cart:eq(0) > *", function() { 
  594.  
  595. // Replace fragments 
  596. if (fragments) { 
  597. $.each(fragments, function(key, value) { 
  598. $(key).replaceWith(value); 
  599. }); 
  600.  
  601. // Unblock 
  602. $(".widget_shopping_cart, .updating").css("opacity", "1").unblock(); 
  603.  
  604. $("body").trigger("cart_widget_refreshed"); 
  605. } ); 
  606. } else { 
  607. // Replace fragments 
  608. if (fragments) { 
  609. $.each(fragments, function(key, value) { 
  610. $(key).replaceWith(value); 
  611. }); 
  612.  
  613. // Unblock 
  614. $(".widget_shopping_cart, .updating").css("opacity", "1").unblock(); 
  615.  
  616. // Cart page elements 
  617. $(".shop_table.cart").load( window.location + " .shop_table.cart:eq(0) > *", function() { 
  618.  
  619. $("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass("buttons_added").append("<input type=\"button\" value=\"+\" id=\"add1\" class=\"plus\" />").prepend("<input type=\"button\" value=\"-\" id=\"minus1\" class=\"minus\" />"); 
  620.  
  621. $(".shop_table.cart").css("opacity", "1").unblock(); 
  622.  
  623. $("body").trigger("cart_page_refreshed"); 
  624. }); 
  625.  
  626. $(".cart_totals").load( window.location + " .cart_totals:eq(0) > *", function() { 
  627. $(".cart_totals").css("opacity", "1").unblock(); 
  628. }); 
  629.  
  630. // Trigger event so themes can refresh other areas 
  631. $("body").trigger("added_to_cart"); 
  632.  
  633. }); 
  634.  
  635. return false; 
  636.  
  637. } else { 
  638. return true; 
  639.  
  640. }); 
  641. }); 
  642. })(jQuery); 
  643. </script>'; 
  644. echo $script_add_on; 
  645.  
  646. public static function woocp_product_featured_tab() { 
  647. global $post; 
  648. global $woo_compare_product_page_settings; 
  649. $compare_featured_tab = trim($woo_compare_product_page_settings['compare_featured_tab']); 
  650. if ($compare_featured_tab == '') $compare_featured_tab = __('Technical Details', 'woocommerce-compare-products' ); 
  651.  
  652. $show_compare_featured_tab = false; 
  653. $product_id = $post->ID; 
  654. $variations_list = WC_Compare_Functions::get_variations($product_id); 
  655. if (is_array($variations_list) && count($variations_list) > 0) { 
  656. foreach ($variations_list as $variation_id) { 
  657. if (WC_Compare_Functions::check_product_activate_compare($variation_id) && WC_Compare_Functions::check_product_have_cat($variation_id)) { 
  658. $compare_category = get_post_meta( $variation_id, '_woo_compare_category', true ); 
  659. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  660. if (is_array($compare_fields) && count($compare_fields)>0) { 
  661. $show_compare_featured_tab = true; 
  662. break; 
  663. }elseif (WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  664. $compare_category = get_post_meta( $product_id, '_woo_compare_category', true ); 
  665. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  666. if (is_array($compare_fields) && count($compare_fields)>0) { 
  667. $show_compare_featured_tab = true; 
  668.  
  669. if ($show_compare_featured_tab) echo '<li><a href="#tab-compare-featured">'.esc_attr( stripslashes( $compare_featured_tab ) ).'</a></li>'; 
  670.  
  671. public static function woocp_product_featured_tab_woo_2_0( $tabs = array() ) { 
  672. global $product, $post; 
  673. global $woo_compare_product_page_settings; 
  674.  
  675. $compare_featured_tab = trim($woo_compare_product_page_settings['compare_featured_tab']); 
  676. if ($compare_featured_tab == '') $compare_featured_tab = __('Technical Details', 'woocommerce-compare-products' ); 
  677.  
  678. $show_compare_featured_tab = false; 
  679. $product_id = $post->ID; 
  680. $variations_list = WC_Compare_Functions::get_variations($product_id); 
  681. if (is_array($variations_list) && count($variations_list) > 0) { 
  682. foreach ($variations_list as $variation_id) { 
  683. if (WC_Compare_Functions::check_product_activate_compare($variation_id) && WC_Compare_Functions::check_product_have_cat($variation_id)) { 
  684. $compare_category = get_post_meta( $variation_id, '_woo_compare_category', true ); 
  685. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  686. if (is_array($compare_fields) && count($compare_fields)>0) { 
  687. $show_compare_featured_tab = true; 
  688. break; 
  689. }elseif (WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) { 
  690. $compare_category = get_post_meta( $product_id, '_woo_compare_category', true ); 
  691. $compare_fields = WC_Compare_Categories_Fields_Data::get_results("cat_id='".$compare_category."'", 'cf.field_order ASC'); 
  692. if (is_array($compare_fields) && count($compare_fields)>0) { 
  693. $show_compare_featured_tab = true; 
  694.  
  695. if ($show_compare_featured_tab) { 
  696.  
  697. $tabs['compare-featured'] = array( 
  698. 'title' => esc_attr( stripslashes( $compare_featured_tab ) ),  
  699. 'priority' => $woo_compare_product_page_settings['auto_compare_featured_tab'],  
  700. 'callback' => array('WC_Compare_Hook_Filter', 'woocp_product_featured_panel_woo_2_0') 
  701. ); 
  702.  
  703. return $tabs; 
  704.  
  705. public static function woocp_product_featured_panel() { 
  706. global $post; 
  707. ?> 
  708. <div class="panel entry-content" id="tab-compare-featured"> 
  709. <?php echo WC_Compare_Hook_Filter::show_compare_fields($post->ID); ?> 
  710. </div> 
  711. <?php 
  712.  
  713. public static function woocp_product_featured_panel_woo_2_0() { 
  714. global $post; 
  715. echo WC_Compare_Hook_Filter::show_compare_fields($post->ID); 
  716.  
  717. public static function woocp_set_selected_attributes($default_attributes) { 
  718. if (isset($_REQUEST['variation_selected']) && $_REQUEST['variation_selected'] > 0) { 
  719. $variation_id = $_REQUEST['variation_selected']; 
  720. $mypost = get_post($variation_id); 
  721. if ($mypost != NULL && $mypost->post_type == 'product_variation') { 
  722. $attributes = (array) maybe_unserialize(get_post_meta($mypost->post_parent, '_product_attributes', true)); 
  723. $my_variation = new WC_Product_Variation($variation_id, $mypost->post_parent); 
  724. $variation_data = $my_variation->get_variation_attributes(); 
  725. if (is_array($attributes) && count($attributes) > 0) { 
  726. foreach ($attributes as $attribute) { 
  727. if ( !$attribute['is_variation'] ) continue; 
  728. $taxonomy = 'attribute_' . sanitize_title($attribute['name']); 
  729. if (isset($variation_data[$taxonomy])) { 
  730. $default_attributes[sanitize_title($attribute['name'])] = $variation_data[$taxonomy];  
  731. return $default_attributes; 
  732.  
  733. public static function auto_create_compare_category($term_id) { 
  734. $term = get_term( $term_id, 'product_cat' ); 
  735. $check_existed = WC_Compare_Categories_Data::get_count("category_name='".trim($term->name)."'"); 
  736. if ($check_existed < 1 ) { 
  737. WC_Compare_Categories_Data::insert_row(array('category_name' => trim(addslashes($term->name)))); 
  738.  
  739. public static function auto_create_compare_feature() { 
  740. if (isset($_POST['add_new_attribute']) && $_POST['add_new_attribute']) { 
  741. //check_admin_referer( 'woocommerce-add-new_attribute' ); 
  742. $attribute_name = (string) sanitize_title($_POST['attribute_name']); 
  743. $attribute_type = (string) $_POST['attribute_type']; 
  744. $attribute_label = (string) $_POST['attribute_label']; 
  745.  
  746. if (!$attribute_label) $attribute_label = ucwords($attribute_name); 
  747.  
  748. if (!$attribute_name) $attribute_name = sanitize_title($attribute_label); 
  749.  
  750. if ($attribute_name && strlen($attribute_name)<30 && $attribute_type && !taxonomy_exists( 'pa_'.$attribute_name )) { 
  751.  
  752. $check_existed = WC_Compare_Data::get_count("field_name='".$attribute_label."'"); 
  753. if ($check_existed < 1 ) { 
  754. $feature_id = WC_Compare_Data::insert_row(array('field_name' => $attribute_label, 'field_type' => 'input-text', 'field_unit' => '', 'default_value' => '' ) ); 
  755.  
  756. public static function a3_wp_admin() { 
  757. wp_enqueue_style( 'a3rev-wp-admin-style', WOOCP_CSS_URL . '/a3_wp_admin.css' ); 
  758.  
  759. public static function admin_sidebar_menu_css() { 
  760. wp_enqueue_style( 'a3rev-wc-cp-admin-sidebar-menu-style', WOOCP_CSS_URL . '/admin_sidebar_menu.css' ); 
  761.  
  762. public static function plugin_extension_box( $boxes = array() ) { 
  763. global $wc_compare_admin_init; 
  764.  
  765. $support_box = '<a href="'.$wc_compare_admin_init->support_url.'" target="_blank" alt="'.__('Go to Support Forum', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/go-to-support-forum.png" /></a>'; 
  766.  
  767. $boxes[] = array( 
  768. 'content' => $support_box,  
  769. 'css' => 'border: none; padding: 0; background: none;' 
  770. ); 
  771.  
  772. $review_box = '<div style="margin-bottom: 5px; font-size: 12px;"><strong>' . __('Is this plugin is just what you needed? If so', 'woocommerce-compare-products' ) . '</strong></div>'; 
  773. $review_box .= '<a href="https://wordpress.org/support/view/plugin-reviews/woocommerce-compare-products#postform" target="_blank" alt="'.__('Submit Review for Plugin on WordPress', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/a-5-star-rating-would-be-appreciated.png" /></a>'; 
  774.  
  775. $boxes[] = array( 
  776. 'content' => $review_box,  
  777. 'css' => 'border: none; padding: 0; background: none;' 
  778. ); 
  779.  
  780. $pro_box = '<a href="'.$wc_compare_admin_init->pro_plugin_page_url.'" target="_blank" alt="'.__('WooCommerce Compare Product', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/pro-version.jpg" /></a>'; 
  781.  
  782. $boxes[] = array( 
  783. 'content' => $pro_box,  
  784. 'css' => 'border: none; padding: 0; background: none;' 
  785. ); 
  786.  
  787. $free_woocommerce_box = '<a href="https://profiles.wordpress.org/a3rev/#content-plugins" target="_blank" alt="'.__('Free WooCommerce Plugins', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/free-woocommerce-plugins.png" /></a>'; 
  788.  
  789. $boxes[] = array( 
  790. 'content' => $free_woocommerce_box,  
  791. 'css' => 'border: none; padding: 0; background: none;' 
  792. ); 
  793.  
  794. $free_wordpress_box = '<a href="https://profiles.wordpress.org/a3rev/#content-plugins" target="_blank" alt="'.__('Free WordPress Plugins', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/free-wordpress-plugins.png" /></a>'; 
  795.  
  796. $boxes[] = array( 
  797. 'content' => $free_wordpress_box,  
  798. 'css' => 'border: none; padding: 0; background: none;' 
  799. ); 
  800.  
  801. $connect_box = '<div style="margin-bottom: 5px;">' . __('Connect with us via', 'woocommerce-compare-products' ) . '</div>'; 
  802. $connect_box .= '<a href="https://www.facebook.com/a3rev" target="_blank" alt="'.__('a3rev Facebook', 'woocommerce-compare-products' ).'" style="margin-right: 5px;"><img src="'.WOOCP_IMAGES_URL.'/follow-facebook.png" /></a> '; 
  803. $connect_box .= '<a href="https://twitter.com/a3rev" target="_blank" alt="'.__('a3rev Twitter', 'woocommerce-compare-products' ).'"><img src="'.WOOCP_IMAGES_URL.'/follow-twitter.png" /></a>'; 
  804.  
  805. $boxes[] = array( 
  806. 'content' => $connect_box,  
  807. 'css' => 'border-color: #3a5795;' 
  808. ); 
  809.  
  810. return $boxes; 
  811.  
  812. public static function plugin_extra_links($links, $plugin_name) { 
  813. if ( $plugin_name != WOOCP_NAME) { 
  814. return $links; 
  815.  
  816. global $wc_compare_admin_init; 
  817. $links[] = '<a href="http://docs.a3rev.com/user-guides/woocommerce/compare-products/" target="_blank">'.__('Documentation', 'woocommerce-compare-products' ).'</a>'; 
  818. $links[] = '<a href="'.$wc_compare_admin_init->support_url.'" target="_blank">'.__('Support', 'woocommerce-compare-products' ).'</a>'; 
  819. return $links; 
  820.  
  821. public static function settings_plugin_links($actions) { 
  822. $actions = array_merge( array( 'settings' => '<a href="admin.php?page=woo-compare-settings">' . __( 'Settings', 'woocommerce-compare-products' ) . '</a>' ), $actions ); 
  823.  
  824. return $actions; 
  825. ?> 
.