WC_Predictive_Search_Functions

WooCommerce Predictive Search Functions.

Defined (1)

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

/classes/class-wc-predictive-search-functions.php  
  1. class WC_Predictive_Search_Functions 
  2.  
  3. public static function symbol_entities() { 
  4. $symbol_entities = array( 
  5. "_" => "_",  
  6. "(" => "(",  
  7. ")" => ")",  
  8. "{" => "{",  
  9. "}" => "}",  
  10. "<" => "<",  
  11. ">" => ">",  
  12. "" => "«",  
  13. "" => "»",  
  14. "*" => "‘",  
  15. "*" => "’",  
  16. "*" => "“",  
  17. "*" => "”",  
  18. "*" => "‐",  
  19. "-" => "-",  
  20. "*" => "–",  
  21. "*" => "—",  
  22. "*" => "←",  
  23. "*" => "→",  
  24. "*" => "↑",  
  25. "*" => "↓",  
  26. "" => "©",  
  27. "" => "®",  
  28. "*" => "™",  
  29. "*" => "€",  
  30. "" => "£",  
  31. "" => "¥",  
  32. "" => "¢",  
  33. "" => "§",  
  34. "*" => "∑",  
  35. "" => "µ",  
  36. "" => "¶",  
  37. "" => "¿",  
  38. "" => "¡",  
  39.  
  40. ); 
  41.  
  42. return apply_filters( 'wc_ps_symbol_entities', $symbol_entities ); 
  43.  
  44. public static function get_argument_vars() { 
  45. $argument_vars = array( 'keyword' , 'search-in', 'cat-in', 'search-other' ); 
  46. return $argument_vars; 
  47.  
  48. public static function special_characters_list() { 
  49. $special_characters = array(); 
  50. foreach ( self::symbol_entities() as $symbol => $entity ) { 
  51. $special_characters[$symbol] = $symbol; 
  52.  
  53. return apply_filters( 'wc_ps_special_characters', $special_characters ); 
  54.  
  55. public static function is_enable_special_characters () { 
  56. $enable_special_characters = true; 
  57.  
  58. $woocommerce_search_remove_special_character = get_option( 'woocommerce_search_remove_special_character', 'no' ); 
  59. if ( 'no' == $woocommerce_search_remove_special_character ) { 
  60. $enable_special_characters = false; 
  61.  
  62. $woocommerce_search_special_characters = get_option( 'woocommerce_search_special_characters', array() ); 
  63. if ( !is_array( $woocommerce_search_special_characters ) || count( $woocommerce_search_special_characters ) < 1 ) { 
  64. $enable_special_characters = false; 
  65.  
  66. return $enable_special_characters; 
  67.  
  68. public static function replace_mysql_command( $field_name, $special_symbol, $replace_special_character = 'ignore' ) { 
  69. if ( 'ignore' == $replace_special_character ) { 
  70. $field_name = 'REPLACE( '.$field_name.', " '.$special_symbol.' ", "")'; 
  71. $field_name = 'REPLACE( '.$field_name.', " '.$special_symbol.'", "")'; 
  72. $field_name = 'REPLACE( '.$field_name.', "'.$special_symbol.' ", "")'; 
  73. $field_name = 'REPLACE( '.$field_name.', "'.$special_symbol.'", "")'; 
  74. } else { 
  75. $field_name = 'REPLACE( '.$field_name.', " '.$special_symbol.' ", " ")'; 
  76. $field_name = 'REPLACE( '.$field_name.', " '.$special_symbol.'", " ")'; 
  77. $field_name = 'REPLACE( '.$field_name.', "'.$special_symbol.' ", " ")'; 
  78. $field_name = 'REPLACE( '.$field_name.', "'.$special_symbol.'", " ")'; 
  79.  
  80. return $field_name; 
  81.  
  82. public static function remove_special_characters_in_mysql( $field_name, $search_keyword = '' ) { 
  83. global $wpdb; 
  84.  
  85. $sql_after = ''; 
  86.  
  87. if ( '' == trim( $field_name ) || '' == trim( $search_keyword ) ) { 
  88. return $sql_after; 
  89.  
  90. global $predictive_search_mode; 
  91.  
  92. $multi_keywords = explode( ' ', trim( $search_keyword ) ); 
  93.  
  94. // This is original query 
  95. if ( 'broad' != $predictive_search_mode ) { 
  96.  
  97. $sql_after .= " ( "; 
  98. $combine = ''; 
  99. foreach ( $multi_keywords as $single_keyword ) { 
  100. $sql_after .= $combine . " ( " . $wpdb->prepare( $field_name . " LIKE %s OR " . $field_name . " LIKE %s ", $single_keyword.'%', '% '.$single_keyword.'%' ) . " ) "; 
  101. $combine = " AND "; 
  102. $sql_after .= " ) "; 
  103.  
  104. } else { 
  105.  
  106. $sql_after .= " ( "; 
  107. $combine = ''; 
  108. foreach ( $multi_keywords as $single_keyword ) { 
  109. $sql_after .= $combine . $wpdb->prepare( $field_name . " LIKE %s ", '%'.$single_keyword.'%' ); 
  110. $combine = " AND "; 
  111. $sql_after .= " ) "; 
  112.  
  113.  
  114. if ( ! self::is_enable_special_characters() ) { 
  115. return $sql_after; 
  116.  
  117. $replace_special_character = get_option( 'woocommerce_search_replace_special_character', 'remove' ); 
  118. $woocommerce_search_special_characters = get_option( 'woocommerce_search_special_characters', array() ); 
  119.  
  120. foreach ( $woocommerce_search_special_characters as $special_symbol ) { 
  121.  
  122. if ( 'both' == $replace_special_character ) { 
  123. if ( 'broad' != $predictive_search_mode ) { 
  124.  
  125. $sql_after .= " OR ( "; 
  126. $combine = ''; 
  127. foreach ( $multi_keywords as $single_keyword ) { 
  128. $sql_after .= $combine . " ( " . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, 'ignore' ) . " LIKE %s OR " . self::replace_mysql_command( $field_name, $special_symbol, 'ignore' ) . " LIKE %s ", $single_keyword.'%', '% '.$single_keyword.'%' ) . " ) "; 
  129.  
  130. $combine = " AND "; 
  131. $sql_after .= " ) "; 
  132.  
  133. $sql_after .= " OR ( "; 
  134. $combine = ''; 
  135. foreach ( $multi_keywords as $single_keyword ) { 
  136. $sql_after .= $combine . " ( " . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, 'remove' ) . " LIKE %s OR " . self::replace_mysql_command( $field_name, $special_symbol, 'remove' ) . " LIKE %s ", $single_keyword.'%', '% '.$single_keyword.'%' ) . " ) "; 
  137.  
  138. $combine = " AND "; 
  139. $sql_after .= " ) "; 
  140.  
  141. } else { 
  142.  
  143. $sql_after .= " OR ( "; 
  144. $combine = ''; 
  145. foreach ( $multi_keywords as $single_keyword ) { 
  146. $sql_after .= $combine . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, 'ignore' ) . " LIKE %s ", '%'.$single_keyword.'%' ); 
  147.  
  148. $combine = " AND "; 
  149. $sql_after .= " ) "; 
  150.  
  151. $sql_after .= " OR ( "; 
  152. $combine = ''; 
  153. foreach ( $multi_keywords as $single_keyword ) { 
  154. $sql_after .= $combine . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, 'remove' ) . " LIKE %s ", '%'.$single_keyword.'%' ); 
  155.  
  156. $combine = " AND "; 
  157. $sql_after .= " ) "; 
  158. } else { 
  159. if ( 'broad' != $predictive_search_mode ) { 
  160.  
  161. $sql_after .= " OR ( "; 
  162. $combine = ''; 
  163. foreach ( $multi_keywords as $single_keyword ) { 
  164. $sql_after .= $combine . " ( " . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, $replace_special_character ) . " LIKE %s OR " . self::replace_mysql_command( $field_name, $special_symbol, $replace_special_character ) . " LIKE %s ", $single_keyword.'%', '% '.$single_keyword.'%' ). " ) "; 
  165.  
  166. $combine = " AND "; 
  167. $sql_after .= " ) "; 
  168.  
  169. } else { 
  170.  
  171. $sql_after .= " OR ( "; 
  172. $combine = ''; 
  173. foreach ( $multi_keywords as $single_keyword ) { 
  174. $sql_after .= $combine . $wpdb->prepare( self::replace_mysql_command( $field_name, $special_symbol, $replace_special_character ) . " LIKE %s ", '%'.$single_keyword.'%' ); 
  175.  
  176. $combine = " AND "; 
  177. $sql_after .= " ) "; 
  178.  
  179.  
  180. return $sql_after; 
  181.  
  182. public static function remove_s_letter_at_end_word( $search_keyword ) { 
  183. $search_keyword_new = ''; 
  184. $search_keyword_new_a = array(); 
  185. $search_keyword_split = explode( " ", trim( $search_keyword ) ); 
  186. if ( is_array( $search_keyword_split ) && count( $search_keyword_split ) > 0 ) { 
  187. foreach ( $search_keyword_split as $search_keyword_element ) { 
  188. if ( strlen( $search_keyword_element ) > 2 ) { 
  189. $search_keyword_new_a[] = rtrim( $search_keyword_element, 's' ); 
  190. } else { 
  191. $search_keyword_new_a[] = $search_keyword_element; 
  192. $search_keyword_new = implode(" ", $search_keyword_new_a); 
  193.  
  194. if ( '' != $search_keyword && $search_keyword_new != $search_keyword ) { 
  195. return $search_keyword_new; 
  196. } else { 
  197. return false; 
  198.  
  199. public static function woops_limit_words($str='', $len=100, $more) { 
  200. if (trim($len) == '' || $len < 0) $len = 100; 
  201. if ( $str=="" || $str==NULL ) return $str; 
  202. if ( is_array($str) ) return $str; 
  203. $str = trim($str); 
  204. $str = strip_tags(str_replace("\r\n", "", $str)); 
  205. if ( strlen($str) <= $len ) return $str; 
  206. $str = substr($str, 0, $len); 
  207. if ( $str != "" ) { 
  208. if ( !substr_count($str, " ") ) { 
  209. if ( $more ) $str .= " ..."; 
  210. return $str; 
  211. while( strlen($str) && ($str[strlen($str)-1] != " ") ) { 
  212. $str = substr($str, 0, -1); 
  213. $str = substr($str, 0, -1); 
  214. if ( $more ) $str .= " ..."; 
  215. return $str; 
  216.  
  217. public static function create_page( $slug, $option, $page_title = '', $page_content = '', $post_parent = 0 ) { 
  218. global $wpdb; 
  219.  
  220. $option_value = get_option($option); 
  221.  
  222. if ( $option_value > 0 && get_post( $option_value ) ) 
  223. return $option_value; 
  224.  
  225. $page_id = $wpdb->get_var( "SELECT ID FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%$page_content%' AND `post_type` = 'page' AND post_status = 'publish' ORDER BY ID ASC LIMIT 1" ); 
  226.  
  227. if ( $page_id != NULL ) : 
  228. if ( ! $option_value ) 
  229. update_option( $option, $page_id ); 
  230. return $page_id; 
  231. endif; 
  232.  
  233. $page_data = array( 
  234. 'post_status' => 'publish',  
  235. 'post_type' => 'page',  
  236. 'post_author' => 1,  
  237. 'post_name' => $slug,  
  238. 'post_title' => $page_title,  
  239. 'post_content' => $page_content,  
  240. 'post_parent' => $post_parent,  
  241. 'comment_status' => 'closed' 
  242. ); 
  243. $page_id = wp_insert_post( $page_data ); 
  244.  
  245. if ( class_exists('SitePress') ) { 
  246. global $sitepress; 
  247. $source_lang_code = $sitepress->get_default_language(); 
  248. $trid = $sitepress->get_element_trid( $page_id, 'post_page' ); 
  249. if ( ! $trid ) { 
  250. $wpdb->query( "UPDATE ".$wpdb->prefix . "icl_translations SET trid=".$page_id." WHERE element_id=".$page_id." AND language_code='".$source_lang_code."' AND element_type='post_page' " ); 
  251.  
  252. update_option( $option, $page_id ); 
  253.  
  254. return $page_id; 
  255.  
  256. public static function create_page_wpml( $trid, $lang_code, $source_lang_code, $slug, $page_title = '', $page_content = '' ) { 
  257. global $wpdb; 
  258.  
  259. $element_id = $wpdb->get_var( "SELECT ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->prefix . "icl_translations AS ic ON p.ID = ic.element_id WHERE p.post_content LIKE '%$page_content%' AND p.post_type = 'page' AND p.post_status = 'publish' AND ic.trid=".$trid." AND ic.language_code = '".$lang_code."' AND ic.element_type = 'post_page' ORDER BY p.ID ASC LIMIT 1" ); 
  260.  
  261. if ( $element_id != NULL ) : 
  262. return $element_id; 
  263. endif; 
  264.  
  265. $page_data = array( 
  266. 'post_date' => gmdate( 'Y-m-d H:i:s' ),  
  267. 'post_modified' => gmdate( 'Y-m-d H:i:s' ),  
  268. 'post_status' => 'publish',  
  269. 'post_type' => 'page',  
  270. 'post_author' => 1,  
  271. 'post_name' => $slug,  
  272. 'post_title' => $page_title,  
  273. 'post_content' => $page_content,  
  274. 'comment_status' => 'closed' 
  275. ); 
  276. $wpdb->insert( $wpdb->posts , $page_data); 
  277. $element_id = $wpdb->insert_id; 
  278.  
  279. //$element_id = wp_insert_post( $page_data ); 
  280.  
  281. $wpdb->insert( $wpdb->prefix . "icl_translations", array( 
  282. 'element_type' => 'post_page',  
  283. 'element_id' => $element_id,  
  284. 'trid' => $trid,  
  285. 'language_code' => $lang_code,  
  286. 'source_language_code' => $source_lang_code,  
  287. ) ); 
  288.  
  289. return $element_id; 
  290.  
  291. public static function auto_create_page_for_wpml( $original_id, $slug, $page_title = '', $page_content = '' ) { 
  292. if ( class_exists('SitePress') ) { 
  293. global $sitepress; 
  294. $active_languages = $sitepress->get_active_languages(); 
  295. if ( is_array($active_languages) && count($active_languages) > 0 ) { 
  296. $source_lang_code = $sitepress->get_default_language(); 
  297. $trid = $sitepress->get_element_trid( $original_id, 'post_page' ); 
  298. foreach ( $active_languages as $language ) { 
  299. if ( $language['code'] == $source_lang_code ) continue; 
  300. WC_Predictive_Search_Functions::create_page_wpml( $trid, $language['code'], $source_lang_code, $slug.'-'.$language['code'], $page_title.' '.$language['display_name'], $page_content ); 
  301.  
  302. public static function get_page_id_from_option( $shortcode, $option ) { 
  303. global $wpdb; 
  304. global $wp_version; 
  305. $page_id = get_option($option); 
  306.  
  307. if ( version_compare( $wp_version, '4.0', '<' ) ) { 
  308. $shortcode = esc_sql( like_escape( $shortcode ) ); 
  309. } else { 
  310. $shortcode = esc_sql( $wpdb->esc_like( $shortcode ) ); 
  311.  
  312. $page_data = null; 
  313. if ( $page_id ) { 
  314. $page_data = $wpdb->get_row( "SELECT ID FROM " . $wpdb->posts . " WHERE post_content LIKE '%[{$shortcode}]%' AND ID = '".$page_id."' AND post_type = 'page' LIMIT 1" ); 
  315. if ( $page_data == null ) { 
  316. $page_data = $wpdb->get_row( "SELECT ID FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[{$shortcode}]%' AND `post_type` = 'page' ORDER BY post_date DESC LIMIT 1" ); 
  317.  
  318. $page_id = $page_data->ID; 
  319.  
  320. return $page_id; 
  321.  
  322. public static function get_page_id_from_shortcode( $shortcode, $option ) { 
  323. global $wpdb; 
  324.  
  325. $page_id = self::get_page_id_from_option( $shortcode, $option ); 
  326.  
  327. // For WPML 
  328. if ( class_exists('SitePress') ) { 
  329. global $sitepress; 
  330. $translation_page_data = null; 
  331. $trid = $sitepress->get_element_trid( $page_id, 'post_page' ); 
  332. if ( $trid ) { 
  333. $translation_page_data = $wpdb->get_row( $wpdb->prepare( "SELECT element_id FROM " . $wpdb->prefix . "icl_translations WHERE trid = %d AND element_type='post_page' AND language_code = %s LIMIT 1", $trid , $sitepress->get_current_language() ) ); 
  334. if ( $translation_page_data != null ) 
  335. $page_id = $translation_page_data->element_id; 
  336.  
  337. return $page_id; 
  338.  
  339. public static function add_query_vars( $aVars ) { 
  340. $argument_vars = self::get_argument_vars(); 
  341. foreach ( $argument_vars as $avar ) { 
  342. $aVars[] = $avar; 
  343.  
  344. return $aVars; 
  345.  
  346. public static function add_page_rewrite_rules( $aRules, $page_id ) { 
  347. $search_page = get_page( $page_id ); 
  348.  
  349. if ( ! empty( $search_page ) ) { 
  350.  
  351. $search_page_slug = $search_page->post_name; 
  352. $argument_vars = self::get_argument_vars(); 
  353.  
  354. $rewrite_rule = ''; 
  355. $original_url = ''; 
  356. $number_matches = 0; 
  357. foreach ( $argument_vars as $avar ) { 
  358. $number_matches++; 
  359. $rewrite_rule .= $avar.'/([^/]*)/'; 
  360. $original_url .= '&'.$avar.'=$matches['.$number_matches.']'; 
  361.  
  362. $aNewRules = array($search_page_slug.'/'.$rewrite_rule.'?$' => 'index.php?pagename='.$search_page_slug.$original_url); 
  363. $aRules = $aNewRules + $aRules; 
  364.  
  365.  
  366. return $aRules; 
  367.  
  368. public static function add_rewrite_rules( $aRules ) { 
  369. global $wpdb; 
  370. global $woocommerce_search_page_id; 
  371.  
  372. $shortcode = 'woocommerce_search'; 
  373. $option_name = 'woocommerce_search_page_id'; 
  374.  
  375. $page_id = $woocommerce_search_page_id; 
  376. if ( empty( $page_id ) ) { 
  377. $page_id = self::get_page_id_from_option( $shortcode, $option_name ); 
  378.  
  379. $aRules = self::add_page_rewrite_rules( $aRules, $page_id ); 
  380.  
  381. // For WPML 
  382. if ( class_exists('SitePress') ) { 
  383. global $sitepress; 
  384. $translation_page_data = null; 
  385. $trid = $sitepress->get_element_trid( $page_id, 'post_page' ); 
  386. if ( $trid ) { 
  387. $translation_page_data = $wpdb->get_results( $wpdb->prepare( "SELECT element_id FROM " . $wpdb->prefix . "icl_translations WHERE trid = %d AND element_type='post_page' AND element_id != %d", $trid , $page_id ) ); 
  388. if ( is_array( $translation_page_data ) && count( $translation_page_data ) > 0 ) { 
  389. foreach( $translation_page_data as $translation_page ) { 
  390. $aRules = self::add_page_rewrite_rules( $aRules, $translation_page->element_id ); 
  391.  
  392. return $aRules; 
  393.  
  394. public static function strip_shortcodes ($content='') { 
  395. $content = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $content); 
  396.  
  397. return $content; 
  398.  
  399. /** 
  400. * Get product price 
  401. */ 
  402. public static function get_product_price( $product ) { 
  403.  
  404. if ( ! is_object( $product ) ) { 
  405. $product_id = absint( $product ); 
  406.  
  407. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  408. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  409. $product = new WC_Product( $product_id ); 
  410. } elseif ( version_compare( WC()->version, '2.2.0', '<' ) ) { 
  411. $product = get_product( $product_id ); 
  412. } else { 
  413. $product = wc_get_product( $product_id ); 
  414.  
  415. $product_price_output = ''; 
  416.  
  417. $product_price_output = $product->get_price_html(); 
  418.  
  419. return $product_price_output; 
  420.  
  421. /** 
  422. * Get product add to cart 
  423. */ 
  424. public static function get_product_addtocart( $product ) { 
  425.  
  426. if ( ! is_object( $product ) ) { 
  427. $product_id = absint( $product ); 
  428.  
  429. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  430. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  431. $product = new WC_Product( $product_id ); 
  432. } elseif ( version_compare( WC()->version, '2.2.0', '<' ) ) { 
  433. $product = get_product( $product_id ); 
  434. } else { 
  435. $product = wc_get_product( $product_id ); 
  436.  
  437. $product_addtocart_output = ''; 
  438.  
  439. ob_start(); 
  440. if (function_exists('woocommerce_template_loop_add_to_cart') ) { 
  441. add_filter( 'woocommerce_product_add_to_cart_url', array( 'WC_Predictive_Search_Functions', 'change_add_to_cart_url' ), 10, 2 ); 
  442. woocommerce_template_loop_add_to_cart(); 
  443. remove_filter( 'woocommerce_product_add_to_cart_url', array( 'WC_Predictive_Search_Functions', 'change_add_to_cart_url' ), 10, 2 ); 
  444. $product_addtocart_output = ob_get_clean(); 
  445.  
  446. return $product_addtocart_output; 
  447.  
  448. /** 
  449. * Get product variation name 
  450. */ 
  451. public static function get_product_variation_name( $variation ) { 
  452.  
  453. if ( ! is_object( $variation ) ) { 
  454. $variation_id = absint( $variation ); 
  455.  
  456. $current_db_version = get_option( 'woocommerce_db_version', null ); 
  457. if ( version_compare( $current_db_version, '2.0', '<' ) && null !== $current_db_version ) { 
  458. $variation = new WC_Product_Variation( $variation_id ); 
  459. } elseif ( version_compare( WC()->version, '2.2.0', '<' ) ) { 
  460. $variation = get_product( $variation_id ); 
  461. } else { 
  462. $variation = wc_get_product( $variation_id ); 
  463.  
  464. if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) { 
  465. $variation_data = $variation->get_variation_attributes(); 
  466. $description = array(); 
  467.  
  468. $attributes = $variation->parent->get_attributes(); 
  469. $return = $variation->parent->get_title(); 
  470.  
  471. if ( is_array( $variation_data ) ) { 
  472.  
  473. foreach ( $attributes as $attribute ) { 
  474.  
  475. // Only deal with attributes that are variations 
  476. if ( ! $attribute[ 'is_variation' ] ) { 
  477. continue; 
  478.  
  479. $variation_selected_value = isset( $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] ) ? $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] : ''; 
  480. $description_name = esc_html( wc_attribute_label( $attribute[ 'name' ] ) ); 
  481. $description_value = ''; 
  482.  
  483. // Get terms for attribute taxonomy or value if its a custom attribute 
  484. if ( $attribute[ 'is_taxonomy' ] ) { 
  485.  
  486. $variation_id = $variation->id; 
  487.  
  488. $post_terms = wp_get_post_terms( $variation_id, $attribute[ 'name' ] ); 
  489.  
  490. foreach ( $post_terms as $term ) { 
  491. if ( $variation_selected_value === $term->slug ) { 
  492. $description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) ); 
  493.  
  494. } else { 
  495.  
  496. $options = wc_get_text_attributes( $attribute[ 'value' ] ); 
  497.  
  498. foreach ( $options as $option ) { 
  499.  
  500. if ( sanitize_title( $variation_selected_value ) === $variation_selected_value ) { 
  501. if ( $variation_selected_value !== sanitize_title( $option ) ) { 
  502. continue; 
  503. } else { 
  504. if ( $variation_selected_value !== $option ) { 
  505. continue; 
  506.  
  507. $description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ); 
  508.  
  509. if ( '' != $description_value ) { 
  510. $description[] = rawurldecode( $description_value ); 
  511.  
  512. if ( count( $description ) > 0 ) { 
  513. $return .= ' ' . implode( ' ', $description ); 
  514. } else { 
  515. $return = $variation->get_name(); 
  516.  
  517. return $return; 
  518.  
  519. /** 
  520. * Get product add to cart 
  521. */ 
  522. public static function get_terms_object( $object_id, $taxonomy = 'product_cat', $post_parent = 0 ) { 
  523. $terms_list = array(); 
  524.  
  525. if ( (int) $post_parent > 0 ) { 
  526. $object_id = (int) $post_parent; 
  527.  
  528. $terms = get_the_terms( $object_id, $taxonomy ); 
  529.  
  530. if ( $terms && ! is_wp_error( $terms ) ) { 
  531. foreach ( $terms as $terms ) { 
  532. $terms_list[] = array( 
  533. 'name' => $terms->name,  
  534. 'url' => get_term_link($terms->slug, $taxonomy ) 
  535. ); 
  536.  
  537. return $terms_list; 
  538.  
  539. /** 
  540. * Get product thumbnail url 
  541. */ 
  542. public static function get_product_thumbnail_url( $post_id, $post_parent = 0, $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) { 
  543. global $woocommerce; 
  544. $woocommerce_db_version = get_option( 'woocommerce_db_version', null ); 
  545. $shop_catalog = ( ( version_compare( $woocommerce_db_version, '2.1', '<' ) ) ? $woocommerce->get_image_size( 'shop_catalog' ) : wc_get_image_size( 'shop_catalog' ) ); 
  546. if ( is_array( $shop_catalog ) && isset( $shop_catalog['width'] ) && $placeholder_width == 0 ) { 
  547. $placeholder_width = $shop_catalog['width']; 
  548. if ( is_array( $shop_catalog ) && isset( $shop_catalog['height'] ) && $placeholder_height == 0 ) { 
  549. $placeholder_height = $shop_catalog['height']; 
  550.  
  551. $mediumSRC = ''; 
  552.  
  553. // Return Feature Image URL 
  554. if ( has_post_thumbnail( $post_id ) ) { 
  555. $thumbid = get_post_thumbnail_id( $post_id ); 
  556. $attachmentArray = wp_get_attachment_image_src( $thumbid, $size, false ); 
  557. if ( $attachmentArray ) { 
  558. $mediumSRC = $attachmentArray[0]; 
  559. if ( trim( $mediumSRC ) != '' ) { 
  560. return $mediumSRC; 
  561.  
  562. // Return First Image URL in gallery of this product 
  563. if ( $post_parent == 0 && trim( $mediumSRC ) == '' ) { 
  564. $args = array( 'post_parent' => $post_id , 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'ID', 'post_status' => null ); 
  565. $attachments = get_posts( $args ); 
  566. if ( $attachments ) { 
  567. foreach ( $attachments as $attachment ) { 
  568. $attachmentArray = wp_get_attachment_image_src( $attachment->ID, $size, false ); 
  569. if ( $attachmentArray ) { 
  570. $mediumSRC = $attachmentArray[0]; 
  571. if ( trim( $mediumSRC ) != '' ) { 
  572. return $mediumSRC; 
  573.  
  574. // Ger Image URL of parent product 
  575. if ( $post_parent > 0 && trim( $mediumSRC ) == '' ) { 
  576.  
  577. // Set ID of parent product if one exists 
  578. $post_id = $post_parent; 
  579.  
  580. if ( has_post_thumbnail( $post_id ) ) { 
  581. $thumbid = get_post_thumbnail_id( $post_id ); 
  582. $attachmentArray = wp_get_attachment_image_src( $thumbid, $size, false ); 
  583. if ( $attachmentArray ) { 
  584. $mediumSRC = $attachmentArray[0]; 
  585. if ( trim( $mediumSRC ) != '' ) { 
  586. return $mediumSRC; 
  587.  
  588. if ( trim( $mediumSRC ) == '' ) { 
  589. $args = array( 'post_parent' => $post_id , 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'DESC', 'orderby' => 'ID', 'post_status' => null ); 
  590. $attachments = get_posts( $args ); 
  591. if ( $attachments ) { 
  592. foreach ( $attachments as $attachment ) { 
  593. $attachmentArray = wp_get_attachment_image_src( $attachment->ID, $size, false ); 
  594. if ( $attachmentArray ) { 
  595. $mediumSRC = $attachmentArray[0]; 
  596. if ( trim( $mediumSRC ) != '' ) { 
  597. return $mediumSRC; 
  598.  
  599. // Use place holder image of Woo 
  600. if ( trim( $mediumSRC ) == '' ) { 
  601. $mediumSRC = ( ( version_compare( $woocommerce_db_version, '2.1', '<' ) && null !== $woocommerce_db_version ) ? woocommerce_placeholder_img_src() : wc_placeholder_img_src() ); 
  602.  
  603. return $mediumSRC; 
  604.  
  605. public static function change_add_to_cart_url( $url, $product ) { 
  606.  
  607. if ( $product->is_type( 'simple' ) || $product->is_type( 'subscription' ) ) { 
  608. $url = $product->is_purchasable() && $product->is_in_stock() ? remove_query_arg( 'added-to-cart', add_query_arg( 'add-to-cart', $product->get_id(), get_permalink( $product->get_id() ) ) ) : get_permalink( $product->get_id() ); 
  609. } elseif ( $product->is_type( 'variation' ) ) { 
  610. $variation_id = $product->get_id(); 
  611. if ( version_compare( WC_VERSION, '3.0.0', '<' ) ) { 
  612. $parent_id = $product->id; 
  613. } else { 
  614. $parent_id = $product->get_parent_id(); 
  615. $variation_data = array_map( 'urlencode', $product->get_variation_attributes() ); 
  616. $url = $product->is_purchasable() && $product->is_in_stock() ? remove_query_arg( 'added-to-cart', add_query_arg( array_merge( array( 'variation_id' => $variation_id, 'add-to-cart' => $parent_id ), $variation_data ), get_permalink( $parent_id ) ) ) : get_permalink( $parent_id ); 
  617.  
  618. return $url;