WC_Compare_Functions

WooCommerce Compare Functions.

Defined (1)

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

/classes/class-wc-compare-functions.php  
  1. class WC_Compare_Functions  
  2.  
  3. /**  
  4. * Set global variable when plugin loaded 
  5. */ 
  6. public static function plugins_loaded() { 
  7. global $product_compare_id; 
  8. global $wpdb; 
  9. $product_compare_id = get_option('product_compare_id'); 
  10.  
  11. $page_data = null; 
  12. if ($product_compare_id) 
  13. $page_data = $wpdb->get_row( "SELECT ID, post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[product_comparison_page]%' AND `ID` = '".$product_compare_id."' AND `post_type` = 'page' LIMIT 1" ); 
  14.  
  15. if ( $page_data == null ) 
  16. $page_data = $wpdb->get_row( "SELECT ID, post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[product_comparison_page]%' AND `post_type` = 'page' ORDER BY ID DESC LIMIT 1" ); 
  17.  
  18. $product_compare_id = $page_data->ID; 
  19.  
  20. /** 
  21. * Get all compare cats 
  22. */ 
  23. public static function get_all_compare_cats( $parent = 0, $append_str = '' ) { 
  24.  
  25. $compare_cats = array(); 
  26. $all_product_cats = get_terms( 'product_cat', array( 'hide_empty' => false, 'parent' => $parent ) ); 
  27.  
  28. if ( ! empty( $all_product_cats ) && !is_wp_error( $all_product_cats ) ) { 
  29. foreach ( $all_product_cats as $cat ) { 
  30. $cat->name = $append_str . $cat->name; 
  31. $compare_cats[] = $cat; 
  32.  
  33. $compare_cats = array_merge( $compare_cats, WC_Compare_Functions::get_all_compare_cats( $cat->term_id, $append_str . '– ' ) ); 
  34.  
  35. return $compare_cats; 
  36.  
  37. /** 
  38. * Get variations or child product from variable product and grouped product 
  39. */ 
  40. public static function get_variations($product_id) { 
  41. $product_avalibale = array(); 
  42. $terms = wp_get_object_terms( $product_id, 'product_type', array('fields' => 'names') ); 
  43.  
  44. // If it is variable product 
  45. if (sanitize_title($terms[0]) == 'variable') { 
  46. $attributes = (array) maybe_unserialize( get_post_meta($product_id, '_product_attributes', true) ); 
  47.  
  48. // See if any are set 
  49. $variation_attribute_found = false; 
  50. if ($attributes) foreach ($attributes as $attribute) { 
  51. if (isset($attribute['is_variation'])) : 
  52. $variation_attribute_found = true; 
  53. break; 
  54. endif; 
  55. if ($variation_attribute_found) { 
  56. $args = array( 
  57. 'post_type' => 'product_variation',  
  58. 'post_status' => array('publish'),  
  59. 'numberposts' => -1,  
  60. 'orderby' => 'id',  
  61. 'order' => 'asc',  
  62. 'post_parent' => $product_id 
  63. ); 
  64. $variations = get_posts($args); 
  65. if ($variations) { 
  66. foreach ($variations as $variation) { 
  67. if (WC_Compare_Functions::check_product_activate_compare($variation->ID) && WC_Compare_Functions::check_product_have_cat($variation->ID)) { 
  68. $product_avalibale[] = $variation->ID; 
  69. // If it is grouped product 
  70. elseif (sanitize_title($terms[0]) == 'grouped') { 
  71. $args = array( 
  72. 'post_type' => 'product',  
  73. 'post_status' => array('publish'),  
  74. 'numberposts' => -1,  
  75. 'orderby' => 'id',  
  76. 'order' => 'asc',  
  77. 'post_parent' => $product_id 
  78. ); 
  79. $variations = get_posts($args); 
  80. if ($variations) { 
  81. foreach ($variations as $variation) { 
  82. if (WC_Compare_Functions::check_product_activate_compare($variation->ID) && WC_Compare_Functions::check_product_have_cat($variation->ID)) { 
  83. $product_avalibale[] = $variation->ID; 
  84.  
  85. return $product_avalibale; 
  86.  
  87. /** 
  88. * Get variation name from variation id 
  89. */ 
  90. public static function get_variation_name($variation_id) { 
  91. $mypost = get_post($variation_id); 
  92. $product_name = ''; 
  93. if ($mypost != NULL) { 
  94. if ($mypost->post_type == 'product_variation') { 
  95. $attributes = (array) maybe_unserialize(get_post_meta($mypost->post_parent, '_product_attributes', true)); 
  96. $my_variation = new WC_Product_Variation($variation_id, $mypost->post_parent); 
  97.  
  98. $variation_data = $my_variation->get_variation_attributes(); 
  99. $variation_name = ''; 
  100. if (is_array($attributes) && count($attributes) > 0) { 
  101. foreach ($attributes as $attribute) { 
  102. if ( !$attribute['is_variation'] ) continue; 
  103. $taxonomy = 'attribute_' . sanitize_title($attribute['name']); 
  104. if (isset($variation_data[$taxonomy])) { 
  105. if (taxonomy_exists(sanitize_title($attribute['name']))) { 
  106. $term = get_term_by('slug', $variation_data[$taxonomy], sanitize_title($attribute['name'])); 
  107. if (!is_wp_error($term) && isset($term->name) && $term->name != '') { 
  108. $value = $term->name; 
  109. $variation_name .= ' '.$value; 
  110. }else { 
  111. $variation_name .= ' '. wc_clean( $variation_data[$taxonomy] ); 
  112.  
  113.  
  114. $product_name = get_the_title($mypost->post_parent).' -'.$variation_name; 
  115. }else { 
  116. $product_name = get_the_title($variation_id); 
  117.  
  118. return $product_name; 
  119.  
  120. /** 
  121. * Get product url 
  122. */ 
  123. public static function get_product_url($product_id) { 
  124. $product = wc_get_product( $product_id ); 
  125.  
  126. $product_url = $product->get_permalink(); 
  127.  
  128. return $product_url; 
  129.  
  130. /** 
  131. * check product or variation is deactivated or activated 
  132. */ 
  133. public static function check_product_activate_compare($product_id) { 
  134. if (get_post_meta( $product_id, '_woo_deactivate_compare_feature', true ) != 'yes') { 
  135. return true; 
  136. }else { 
  137. return false; 
  138.  
  139. /** 
  140. * Check product that is assigned the compare category for it 
  141. */ 
  142. public static function check_product_have_cat( $product_id ) { 
  143. $compare_category = get_post_meta( $product_id, '_woo_compare_category', true ); 
  144. if ($compare_category > 0 && WC_Compare_Categories_Data::get_count("id='".$compare_category."'") > 0) { 
  145. $compare_fields = WC_Compare_Categories_Fields_Data::get_fieldid_results($compare_category); 
  146. if (is_array($compare_fields) && count($compare_fields)>0) { 
  147. return true; 
  148. }else { 
  149. return false; 
  150. }else { 
  151. return false; 
  152.  
  153. /** 
  154. * Add a product or variations of product into compare widget list 
  155. */ 
  156. public static function add_product_to_compare_list($product_id) { 
  157. $product_list = WC_Compare_Functions::get_variations($product_id); 
  158. if (count($product_list) < 1 && WC_Compare_Functions::check_product_activate_compare($product_id) && WC_Compare_Functions::check_product_have_cat($product_id)) $product_list = array($product_id); 
  159. if (is_array($product_list) && count($product_list) > 0) { 
  160. if (isset($_COOKIE['woo_compare_list'])) 
  161. $current_compare_list = (array) unserialize($_COOKIE['woo_compare_list']); 
  162. else 
  163. $current_compare_list = array(); 
  164. foreach ($product_list as $product_add) { 
  165. if (!in_array($product_add, $current_compare_list)) { 
  166. $current_compare_list[] = (int)$product_add; 
  167.  
  168. setcookie( "woo_compare_list", serialize($current_compare_list), 0, COOKIEPATH, COOKIE_DOMAIN, false, true ); 
  169.  
  170. /** 
  171. * Get list product ids , variation ids 
  172. */ 
  173. public static function get_compare_list() { 
  174. if (isset($_COOKIE['woo_compare_list'])) 
  175. $current_compare_list = (array) unserialize($_COOKIE['woo_compare_list']); 
  176. else 
  177. $current_compare_list = array(); 
  178. $return_compare_list = array(); 
  179. if (is_array($current_compare_list) && count($current_compare_list) > 0) { 
  180. foreach ($current_compare_list as $product_id) { 
  181. if (WC_Compare_Functions::check_product_activate_compare($product_id)) { 
  182. $return_compare_list[] = (int)$product_id; 
  183. return $return_compare_list; 
  184.  
  185. /** 
  186. * Get total products in complare list 
  187. */ 
  188. public static function get_total_compare_list() { 
  189. if (isset($_COOKIE['woo_compare_list'])) 
  190. $current_compare_list = (array) unserialize($_COOKIE['woo_compare_list']); 
  191. else 
  192. $current_compare_list = array(); 
  193. $return_compare_list = array(); 
  194. if (is_array($current_compare_list) && count($current_compare_list) > 0) { 
  195. foreach ($current_compare_list as $product_id) { 
  196. if (WC_Compare_Functions::check_product_activate_compare($product_id)) { 
  197. $return_compare_list[] = (int)$product_id; 
  198. return count($return_compare_list); 
  199.  
  200. /** 
  201. * Remove a product out compare list 
  202. */ 
  203. public static function delete_product_on_compare_list($product_id) { 
  204. if (isset($_COOKIE['woo_compare_list'])) 
  205. $current_compare_list = (array) unserialize($_COOKIE['woo_compare_list']); 
  206. else 
  207. $current_compare_list = array(); 
  208. $key = array_search($product_id, $current_compare_list); 
  209. unset($current_compare_list[$key]); 
  210. setcookie( "woo_compare_list", serialize($current_compare_list), 0, COOKIEPATH, COOKIE_DOMAIN, false, true ); 
  211.  
  212. /** 
  213. * Clear compare list 
  214. */ 
  215. public static function clear_compare_list() { 
  216. setcookie( "woo_compare_list", serialize(array()), 0, COOKIEPATH, COOKIE_DOMAIN, false, true ); 
  217.  
  218. /** 
  219. * Get price of product, variation to show on popup compare 
  220. */ 
  221. public static function woocp_the_product_price( $product_id, $no_decimals = false, $only_normal_price = false ) { 
  222. global $woo_query, $woo_variations, $wpdb; 
  223. $price = $full_price = get_post_meta( $product_id, '_woo_price', true ); 
  224.  
  225. if ( ! $only_normal_price ) { 
  226. $special_price = get_post_meta( $product_id, '_woo_special_price', true ); 
  227.  
  228. if ( ( $full_price > $special_price ) && ( $special_price > 0 ) ) 
  229. $price = $special_price; 
  230.  
  231. if ( $no_decimals == true ) 
  232. $price = array_shift( explode( ".", $price ) ); 
  233.  
  234. $price = apply_filters( 'woo_do_convert_price', $price ); 
  235. $args = array( 
  236. 'display_as_html' => false,  
  237. 'display_decimal_point' => ! $no_decimals 
  238. ); 
  239. if ($price > 0) { 
  240. $output = woo_currency_display( $price, $args ); 
  241. return $output; 
  242.  
  243. /** 
  244. * Get compare widget on sidebar 
  245. */ 
  246. public static function get_compare_list_html_widget() { 
  247. global $product_compare_id; 
  248. global $woo_compare_comparison_page_global_settings; 
  249. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  250. $woo_compare_basket_icon = WOOCP_IMAGES_URL.'/compare_remove.png'; 
  251. $compare_list = WC_Compare_Functions::get_compare_list(); 
  252. $html = ''; 
  253. if (is_array($compare_list) && count($compare_list)>0) { 
  254. $html .= '<ul class="compare_widget_ul">'; 
  255. foreach ($compare_list as $product_id) { 
  256. $thumbnail_html = ''; 
  257. $thumbnail_html = WC_Compare_Functions::get_post_thumbnail($product_id, 64, 9999, 'woo_compare_widget_thumbnail'); 
  258. if (trim($thumbnail_html) == '') { 
  259. $thumbnail_html = '<img class="woo_compare_widget_thumbnail" alt="" src="'. ( ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) ? woocommerce_placeholder_img_src() : wc_placeholder_img_src() ).'" />'; 
  260. $html .= '<li class="compare_widget_item">'; 
  261. $html .= '<div class="compare_remove_column"><a class="woo_compare_remove_product" rel="'.$product_id.'"><img class="woo_compare_remove_icon" src="'.$woo_compare_basket_icon.'" /></a></div>'; 
  262. $html .= '<div class="compare_title_column"><a class="woo_compare_widget_item" href="'.WC_Compare_Functions::get_product_url($product_id).'">'.$thumbnail_html.WC_Compare_Functions::get_variation_name($product_id).'</a></div>'; 
  263. $html .= '<div style="clear:both;"></div></li>'; 
  264. $html .= '</ul>'; 
  265. $html .= '<div class="compare_widget_action">'; 
  266.  
  267. $widget_clear_all_custom_class = ''; 
  268. $widget_clear_all_text = __('Clear All', 'woocommerce-compare-products' ); 
  269. $widget_clear_all_class = 'woo_compare_clear_all_link'; 
  270.  
  271. $clear_html = '<div style="clear:both"></div><div class="woo_compare_clear_all_container"><a class="woo_compare_clear_all '.$widget_clear_all_class.' '.$widget_clear_all_custom_class.'">'.$widget_clear_all_text.'</a></div><div style="clear:both"></div>'; 
  272.  
  273. $widget_button_custom_class = ''; 
  274. $widget_button_text = __('Compare', 'woocommerce-compare-products' ); 
  275. $widget_button_class = 'woo_compare_widget_button_go'; 
  276.  
  277. $product_compare_page = get_permalink($product_compare_id); 
  278. if ($woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page') { 
  279. $product_compare_page = '#'; 
  280.  
  281. $widget_compare_popup_button = ''; 
  282. if ( $woo_compare_comparison_page_global_settings['open_compare_type'] != 'new_page' ) $widget_compare_popup_button = 'woo_compare_popup_button_go'; 
  283.  
  284. $html .= '<div class="woo_compare_widget_button_container"><a class="woo_compare_button_go '.$widget_compare_popup_button.' '.$widget_button_class.' '.$widget_button_custom_class.'" href="'.$product_compare_page.'" target="_blank" alt="" title="">'.$widget_button_text.'</a></div>'; 
  285.  
  286. $html .= '<div style="clear:both"></div></div>'; 
  287. }else { 
  288. $html .= '<div class="no_compare_list">'.__( 'Nothing to Compare Text', 'woocommerce-compare-products' ).'</div>'; 
  289. return $html; 
  290.  
  291. /** 
  292. * Get compare list on popup 
  293. */ 
  294. public static function get_compare_list_html_popup() { 
  295. global $woo_compare_comparison_page_global_settings; 
  296. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  297. $compare_list = WC_Compare_Functions::get_compare_list(); 
  298. $woo_compare_basket_icon = WOOCP_IMAGES_URL.'/compare_remove.png'; 
  299. $html = ''; 
  300. $product_cats = array(); 
  301. $products_fields = array(); 
  302. $products_prices = array(); 
  303. $custom_class = ''; 
  304. $add_to_cart_text = __('Add to cart', 'woocommerce-compare-products' ); 
  305. $add_to_cart_button_class = 'add_to_cart_link_type'; 
  306.  
  307. if (is_array($compare_list) && count($compare_list)>0) { 
  308. $html .= '<div id="compare-wrapper"><div class="compare-products">'; 
  309. $html .= '<table id="product_comparison" class="compare_popup_table" border="1" bordercolor="#D6D6D6" cellpadding="5" cellspacing="0" width="">'; 
  310. $html .= '<tbody><tr class="row_1 row_product_detail"><th class="column_first first_row"><div class="column_first_wide"> '; 
  311. $html .= '</div></th>'; 
  312. $i = 0; 
  313. foreach ($compare_list as $product_id) { 
  314. $product_cat = get_post_meta( $product_id, '_woo_compare_category', true ); 
  315. $products_fields[$product_id] = WC_Compare_Categories_Fields_Data::get_fieldid_results($product_cat); 
  316. if ($product_cat > 0) { 
  317. $product_cats[] = $product_cat; 
  318. $i++; 
  319.  
  320. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  321. $current_product = new WC_Product($product_id); 
  322. } elseif ( version_compare( WC()->version, '2.2.0', '<' ) ) { 
  323. $current_product = get_product($product_id); 
  324. } else { 
  325. $current_product = wc_get_product($product_id); 
  326.  
  327. $product_name = WC_Compare_Functions::get_variation_name($product_id); 
  328.  
  329. $product_price = $current_product->get_price_html(); 
  330.  
  331. /** 
  332. * Add code check show or hide price and add to cart button support for Woo Catalog Visibility Options plugin 
  333. */ 
  334. $show_add_to_cart = true; 
  335. if (class_exists('WC_CVO_Visibility_Options')) { 
  336. global $wc_cvo; 
  337. /** 
  338. * Check show or hide price 
  339. */ 
  340. if (($wc_cvo->setting('wc_cvo_prices') == 'secured' && !catalog_visibility_user_has_access()) || $wc_cvo->setting('wc_cvo_prices') == 'disabled') { 
  341. $product_price = ''; 
  342.  
  343. /** 
  344. * Check show or hide add to cart button 
  345. */ 
  346. if (($wc_cvo->setting('wc_cvo_atc') == 'secured' && !catalog_visibility_user_has_access()) || $wc_cvo->setting('wc_cvo_atc') == 'disabled') { 
  347. $show_add_to_cart = false; 
  348. $products_prices[$product_id] = $product_price; 
  349. $image_src = WC_Compare_Functions::get_post_thumbnail($product_id, 220, 180); 
  350. if (trim($image_src) == '') { 
  351. $image_src = '<img alt="'.$product_name.'" src="'. ( ( version_compare( $current_db_version, '2.1.0', '<' ) && null !== $current_db_version ) ? woocommerce_placeholder_img_src() : wc_placeholder_img_src() ) .'" />'; 
  352. $html .= '<td class="first_row column_'.$i.'"><div class="td-spacer"><div class="woo_compare_popup_remove_product_container"><a class="woo_compare_popup_remove_product" rel="'.$product_id.'" style="cursor:pointer;">Remove <img src="'.$woo_compare_basket_icon.'" border=0 /></a></div>'; 
  353. $html .= '<div class="compare_image_container">'.$image_src.'</div>'; 
  354. $html .= '<div class="compare_product_name">'.$product_name.'</div>'; 
  355. $html .= '<div class="compare_price">'.$products_prices[$product_id].'</div>'; 
  356. if ($show_add_to_cart && $current_product->is_in_stock() && trim($products_prices[$product_id]) != '') { 
  357. if ( ! $current_product->is_type( 'external' ) ) { 
  358. $cart_url = add_query_arg( array( 'post_type' => 'product', 'add-to-cart' => $product_id ), get_permalink( $product_id ) ); 
  359. } else if ( $current_product->is_type( 'external' ) ) { 
  360. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  361. $cart_url = get_post_meta( $product_id, '_product_url', true ); 
  362. $add_to_cart_text_external = get_post_meta( $product_id, '_button_text', true ); 
  363. ( $add_to_cart_text_external ) ? $add_to_cart_text_external : __( 'Buy product', 'woocommerce-compare-products' ); 
  364. } else { 
  365. $cart_url = $current_product->get_product_url(); 
  366. $add_to_cart_text_external = $current_product->get_button_text(); 
  367. switch (get_post_type($product_id)) : 
  368. case "product_variation" : 
  369. $class = 'is_variation'; 
  370. $cart_url = WC_Compare_Functions::get_product_url($product_id); 
  371. break; 
  372. default : 
  373. $class = 'simple ajax_add_to_cart'; 
  374. break; 
  375. endswitch; 
  376. $html .= '<div class="compare_add_cart">'; 
  377. if ( $current_product->is_type( 'external' ) ) { 
  378. $html .= sprintf('<a href="%s" rel="nofollow" class="button add_to_cart_button %s product_type_%s %s" target="_blank">%s</a>', $cart_url, $add_to_cart_button_class, $class, $custom_class, $add_to_cart_text_external); 
  379. } else { 
  380. $html .= sprintf('<a href="%s" data-product_id="%s" class="button add_to_cart_button %s product_type_%s %s" target="_blank">%s</a>', $cart_url, $product_id, $add_to_cart_button_class, $class, $custom_class, $add_to_cart_text); 
  381. $html .= '<a class="virtual_added_to_cart" href="#"> </a>'; 
  382. $html .= '</div>'; 
  383. $html .= '</div></td>'; 
  384. $html .= '</tr>'; 
  385. $product_cats = implode(", ", $product_cats); 
  386. $compare_fields = WC_Compare_Categories_Fields_Data::get_results('cat_id IN('.$product_cats.')', 'cf.cat_id ASC, cf.field_order ASC'); 
  387. if (is_array($compare_fields) && count($compare_fields)>0) { 
  388. $j = 1; 
  389. foreach ($compare_fields as $field_data) { 
  390. $j++; 
  391. $html .= '<tr class="row_'.$j.'">'; 
  392. if (trim($field_data->field_unit) != '') 
  393. $html .= '<th class="column_first"><div class="compare_value">'.stripslashes($field_data->field_name).' ('.trim(stripslashes($field_data->field_unit)).')</div></th>'; 
  394. else 
  395. $html .= '<th class="column_first"><div class="compare_value">'.stripslashes($field_data->field_name).'</div></th>'; 
  396. $i = 0; 
  397. foreach ($compare_list as $product_id) { 
  398. $i++; 
  399. $empty_cell_class = ''; 
  400. $empty_text_class = ''; 
  401. if (in_array($field_data->id, $products_fields[$product_id])) { 
  402. $field_value = get_post_meta( $product_id, '_woo_compare_'.$field_data->field_key, true ); 
  403. if (is_serialized($field_value)) $field_value = maybe_unserialize($field_value); 
  404. if (is_array($field_value) && count($field_value) > 0) $field_value = implode(', ', $field_value); 
  405. elseif (is_array($field_value) && count($field_value) < 0) $field_value = ''; 
  406. }else { 
  407. $field_value = ''; 
  408. if ($field_value == '') { 
  409. $empty_cell_class = 'empty_cell'; 
  410. $empty_text_class = 'empty_text'; 
  411. $html .= '<td class="column_'.$i.' '.$empty_cell_class.'"><div class="td-spacer '.$empty_text_class.' compare_'.$field_data->field_key.'">'.$field_value.'</div></td>'; 
  412. $html .= '</tr>'; 
  413. if ($j==2) $j=0; 
  414.  
  415. $j++; 
  416. if ($j>2) $j=1; 
  417. $html .= '<tr class="row_'.$j.' row_end"><th class="column_first"> </th>'; 
  418. $i = 0; 
  419. foreach ($compare_list as $product_id) { 
  420. $i++; 
  421. $html .= '<td class="column_'.$i.'">'; 
  422. $html .= '<div class="td-spacer compare_price">'.$products_prices[$product_id].'</div>'; 
  423. $html .= '</td>'; 
  424. $html .= '</tbody></table>'; 
  425. $html .= '</div></div>'; 
  426. }else { 
  427. $html .= '<div class="no_compare_list">'.$woo_compare_comparison_page_global_settings['no_product_message_text'].'</div>'; 
  428. return $html; 
  429.  
  430. public static function get_post_thumbnail($postid=0, $width=220, $height=180, $class='') { 
  431. $mediumSRC = ''; 
  432. // Get the product ID if none was passed 
  433. if ( empty( $postid ) ) 
  434. $postid = get_the_ID(); 
  435.  
  436. // Load the product 
  437. $product = get_post( $postid ); 
  438.  
  439. if (has_post_thumbnail($postid)) { 
  440. $thumbid = get_post_thumbnail_id($postid); 
  441. $attachmentArray = wp_get_attachment_image_src($thumbid, array(0 => $width, 1 => $height), false); 
  442. $mediumSRC = $attachmentArray[0]; 
  443. if (trim($mediumSRC != '')) { 
  444. return '<img class="'.$class.'" src="'.$mediumSRC.'" />'; 
  445. if (trim($mediumSRC == '')) { 
  446. $args = array( 'post_parent' => $postid , 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'ID', 'post_status' => null); 
  447. $attachments = get_posts($args); 
  448. if ($attachments) { 
  449. foreach ( $attachments as $attachment ) { 
  450. $mediumSRC = wp_get_attachment_image( $attachment->ID, array(0 => $width, 1 => $height), true, array('class' => $class) ); 
  451. break; 
  452.  
  453. if (trim($mediumSRC == '')) { 
  454. // Get ID of parent product if one exists 
  455. if ( !empty( $product->post_parent ) ) 
  456. $postid = $product->post_parent; 
  457.  
  458. if (has_post_thumbnail($postid)) { 
  459. $thumbid = get_post_thumbnail_id($postid); 
  460. $attachmentArray = wp_get_attachment_image_src($thumbid, array(0 => $width, 1 => $height), false); 
  461. $mediumSRC = $attachmentArray[0]; 
  462. if (trim($mediumSRC != '')) { 
  463. return '<img class="'.$class.'" src="'.$mediumSRC.'" />'; 
  464. if (trim($mediumSRC == '')) { 
  465. $args = array( 'post_parent' => $postid , 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'ID', 'post_status' => null); 
  466. $attachments = get_posts($args); 
  467. if ($attachments) { 
  468. foreach ( $attachments as $attachment ) { 
  469. $mediumSRC = wp_get_attachment_image( $attachment->ID, array(0 => $width, 1 => $height), true, array('class' => $class) ); 
  470. break; 
  471. return $mediumSRC; 
  472.  
  473. public static function printPage($link, $total = 0, $currentPage = 1, $div = 3, $rows = 5, $li = false, $a_class= '') { 
  474. if(!$total || !$rows || !$div || $total<=$rows) return false; 
  475. $nPage = floor($total/$rows) + (($total%$rows)?1:0); 
  476. $nDiv = floor($nPage/$div) + (($nPage%$div)?1:0);  
  477. $currentDiv = floor(($currentPage-1)/$div) ;  
  478. $sPage = '';  
  479. if($currentDiv) {  
  480. if($li) { 
  481. $sPage .= '<li><span class="pagenav"><a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', 1, $link).'">«</a></span></li>';  
  482. $sPage .= '<li><span class="pagenav"><a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', $currentDiv*$div, $link).'">'.__("Back", 'woocommerce-compare-products' ).'</a></span></li>';  
  483. }else{ 
  484. $sPage .= '<a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', 1, $link).'">«</a> ';  
  485. $sPage .= '<a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', $currentDiv*$div, $link).'">'.__("Back", 'woocommerce-compare-products' ).'</a> ';  
  486. $count =($nPage<=($currentDiv+1)*$div)?($nPage-$currentDiv*$div):$div;  
  487. for($i=1;$i<=$count;$i++) {  
  488. $page = ($currentDiv*$div + $i);  
  489. if($li) { 
  490. $sPage .= '<li '.(($page==$currentPage)? 'class="current"':'class="page-numbers"').'><span class="pagenav"><a title="" href="'.add_query_arg('pp', ($currentDiv*$div + $i ), $link).'" '.(($page==$currentPage)? 'class="current '.$a_class.'"':'class="page-numbers '.$a_class.'"').'>'.$page.'</a></span></li>'; 
  491. }else{ 
  492. $sPage .= '<a title="" href="'.add_query_arg('pp', ($currentDiv*$div + $i ), $link).'" '.(($page==$currentPage)? 'class="current '.$a_class.'"':'class="page-numbers '.$a_class.'"').'>'.$page.'</a> '; 
  493. }  
  494. if($currentDiv < $nDiv - 1) {  
  495. if($li) {  
  496. $sPage .= '<li><span class="pagenav"><a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', ((($currentDiv+1)*$div)+1), $link).'">'.__("Next", 'woocommerce-compare-products' ).'</a></span></li>';  
  497. $sPage .= '<li><span class="pagenav"><a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', (($nDiv*$div )-2), $link).'">»</a></span></li>';  
  498. }else{ 
  499. $sPage .= '<a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', ((($currentDiv+1)*$div)+1), $link).'">'.__("Next", 'woocommerce-compare-products' ).'</a> ';  
  500. $sPage .= '<a title="" class="page-numbers '.$a_class.'" href="'.add_query_arg('pp', (($nDiv*$div )-2), $link).'">»</a>';  
  501. }  
  502. return $sPage;  
  503.  
  504. /** 
  505. * Create Page 
  506. */ 
  507. public static function create_page( $slug, $option, $page_title = '', $page_content = '', $post_parent = 0 ) { 
  508. global $wpdb; 
  509.  
  510. $page_id = $wpdb->get_var( "SELECT ID FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%$page_content%' AND `post_type` = 'page' ORDER BY ID DESC LIMIT 1" ); 
  511.  
  512. if ( $page_id != NULL )  
  513. return $page_id; 
  514.  
  515. $page_data = array( 
  516. 'post_status' => 'publish',  
  517. 'post_type' => 'page',  
  518. 'post_author' => 1,  
  519. 'post_name' => $slug,  
  520. 'post_title' => $page_title,  
  521. 'post_content' => $page_content,  
  522. 'post_parent' => $post_parent,  
  523. 'comment_status' => 'closed' 
  524. ); 
  525. $page_id = wp_insert_post( $page_data ); 
  526.  
  527. return $page_id;