PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/wc-term-functions.php

https://gitlab.com/webkod3r/tripolis
PHP | 665 lines | 361 code | 104 blank | 200 comment | 81 complexity | 2076b36cd787bae9888d87917bd3d957 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Terms
  4. *
  5. * Functions for handling terms/term meta.
  6. *
  7. * @author WooThemes
  8. * @category Core
  9. * @package WooCommerce/Functions
  10. * @version 2.1.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit; // Exit if accessed directly
  14. }
  15. /**
  16. * Wrapper for wp_get_post_terms which supports ordering by parent.
  17. *
  18. * NOTE: At this point in time, ordering by menu_order for example isn't possible with this function. wp_get_post_terms has no.
  19. * filters which we can utilise to modify it's query. https://core.trac.wordpress.org/ticket/19094.
  20. *
  21. * @param int $product_id
  22. * @param string $taxonomy
  23. * @param array $args
  24. * @return array
  25. */
  26. function wc_get_product_terms( $product_id, $taxonomy, $args = array() ) {
  27. if ( ! taxonomy_exists( $taxonomy ) ) {
  28. return array();
  29. }
  30. if ( empty( $args['orderby'] ) && taxonomy_is_product_attribute( $taxonomy ) ) {
  31. $args['orderby'] = wc_attribute_orderby( $taxonomy );
  32. }
  33. // Support ordering by parent
  34. if ( ! empty( $args['orderby'] ) && in_array( $args['orderby'], array( 'name_num', 'parent' ) ) ) {
  35. $fields = isset( $args['fields'] ) ? $args['fields'] : 'all';
  36. $orderby = $args['orderby'];
  37. // Unset for wp_get_post_terms
  38. unset( $args['orderby'] );
  39. unset( $args['fields'] );
  40. $terms = wp_get_post_terms( $product_id, $taxonomy, $args );
  41. switch ( $orderby ) {
  42. case 'name_num' :
  43. usort( $terms, '_wc_get_product_terms_name_num_usort_callback' );
  44. break;
  45. case 'parent' :
  46. usort( $terms, '_wc_get_product_terms_parent_usort_callback' );
  47. break;
  48. }
  49. switch ( $fields ) {
  50. case 'names' :
  51. $terms = wp_list_pluck( $terms, 'name' );
  52. break;
  53. case 'ids' :
  54. $terms = wp_list_pluck( $terms, 'term_id' );
  55. break;
  56. case 'slugs' :
  57. $terms = wp_list_pluck( $terms, 'slug' );
  58. break;
  59. }
  60. } elseif ( ! empty( $args['orderby'] ) && $args['orderby'] === 'menu_order' ) {
  61. // wp_get_post_terms doesn't let us use custom sort order
  62. $args['include'] = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'ids' ) );
  63. if ( empty( $args['include'] ) ) {
  64. $terms = array();
  65. } else {
  66. // This isn't needed for get_terms
  67. unset( $args['orderby'] );
  68. // Set args for get_terms
  69. $args['menu_order'] = isset( $args['order'] ) ? $args['order'] : 'ASC';
  70. $args['hide_empty'] = isset( $args['hide_empty'] ) ? $args['hide_empty'] : 0;
  71. $args['fields'] = isset( $args['fields'] ) ? $args['fields'] : 'names';
  72. // Ensure slugs is valid for get_terms - slugs isn't supported
  73. $args['fields'] = $args['fields'] === 'slugs' ? 'id=>slug' : $args['fields'];
  74. $terms = get_terms( $taxonomy, $args );
  75. }
  76. } else {
  77. $terms = wp_get_post_terms( $product_id, $taxonomy, $args );
  78. }
  79. return apply_filters( 'woocommerce_get_product_terms' , $terms, $product_id, $taxonomy, $args );
  80. }
  81. /**
  82. * Sort by name (numeric).
  83. * @param WP_POST object $a
  84. * @param WP_POST object $b
  85. * @return int
  86. */
  87. function _wc_get_product_terms_name_num_usort_callback( $a, $b ) {
  88. if ( $a->name + 0 === $b->name + 0 ) {
  89. return 0;
  90. }
  91. return ( $a->name + 0 < $b->name + 0 ) ? -1 : 1;
  92. }
  93. /**
  94. * Sort by parent.
  95. * @param WP_POST object $a
  96. * @param WP_POST object $b
  97. * @return int
  98. */
  99. function _wc_get_product_terms_parent_usort_callback( $a, $b ) {
  100. if ( $a->parent === $b->parent ) {
  101. return 0;
  102. }
  103. return ( $a->parent < $b->parent ) ? 1 : -1;
  104. }
  105. /**
  106. * WooCommerce Dropdown categories.
  107. *
  108. * Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258.
  109. * We use a custom walker, just like WordPress does.
  110. *
  111. * @param int $deprecated_show_uncategorized (default: 1)
  112. * @return string
  113. */
  114. function wc_product_dropdown_categories( $args = array(), $deprecated_hierarchical = 1, $deprecated_show_uncategorized = 1, $deprecated_orderby = '' ) {
  115. global $wp_query;
  116. if ( ! is_array( $args ) ) {
  117. _deprecated_argument( 'wc_product_dropdown_categories()', '2.1', 'show_counts, hierarchical, show_uncategorized and orderby arguments are invalid - pass a single array of values instead.' );
  118. $args['show_count'] = $args;
  119. $args['hierarchical'] = $deprecated_hierarchical;
  120. $args['show_uncategorized'] = $deprecated_show_uncategorized;
  121. $args['orderby'] = $deprecated_orderby;
  122. }
  123. $current_product_cat = isset( $wp_query->query_vars['product_cat'] ) ? $wp_query->query_vars['product_cat'] : '';
  124. $defaults = array(
  125. 'pad_counts' => 1,
  126. 'show_count' => 1,
  127. 'hierarchical' => 1,
  128. 'hide_empty' => 1,
  129. 'show_uncategorized' => 1,
  130. 'orderby' => 'name',
  131. 'selected' => $current_product_cat,
  132. 'menu_order' => false
  133. );
  134. $args = wp_parse_args( $args, $defaults );
  135. if ( $args['orderby'] == 'order' ) {
  136. $args['menu_order'] = 'asc';
  137. $args['orderby'] = 'name';
  138. }
  139. $terms = get_terms( 'product_cat', apply_filters( 'wc_product_dropdown_categories_get_terms_args', $args ) );
  140. if ( ! $terms ) {
  141. return;
  142. }
  143. $output = "<select name='product_cat' class='dropdown_product_cat'>";
  144. $output .= '<option value="" ' . selected( $current_product_cat, '', false ) . '>' . __( 'Select a category', 'woocommerce' ) . '</option>';
  145. $output .= wc_walk_category_dropdown_tree( $terms, 0, $args );
  146. if ( $args['show_uncategorized'] ) {
  147. $output .= '<option value="0" ' . selected( $current_product_cat, '0', false ) . '>' . __( 'Uncategorized', 'woocommerce' ) . '</option>';
  148. }
  149. $output .= "</select>";
  150. echo $output;
  151. }
  152. /**
  153. * Walk the Product Categories.
  154. *
  155. * @return mixed
  156. */
  157. function wc_walk_category_dropdown_tree() {
  158. if ( ! class_exists( 'WC_Product_Cat_Dropdown_Walker' ) ) {
  159. include_once( WC()->plugin_path() . '/includes/walkers/class-product-cat-dropdown-walker.php' );
  160. }
  161. $args = func_get_args();
  162. // the user's options are the third parameter
  163. if ( empty( $args[2]['walker']) || !is_a($args[2]['walker'], 'Walker' ) ) {
  164. $walker = new WC_Product_Cat_Dropdown_Walker;
  165. } else {
  166. $walker = $args[2]['walker'];
  167. }
  168. return call_user_func_array( array( &$walker, 'walk' ), $args );
  169. }
  170. /**
  171. * WooCommerce Term/Order item Meta API - set table name.
  172. */
  173. function wc_taxonomy_metadata_wpdbfix() {
  174. global $wpdb;
  175. $termmeta_name = 'woocommerce_termmeta';
  176. $itemmeta_name = 'woocommerce_order_itemmeta';
  177. $wpdb->woocommerce_termmeta = $wpdb->prefix . $termmeta_name;
  178. $wpdb->order_itemmeta = $wpdb->prefix . $itemmeta_name;
  179. $wpdb->tables[] = 'woocommerce_termmeta';
  180. $wpdb->tables[] = 'woocommerce_order_itemmeta';
  181. }
  182. add_action( 'init', 'wc_taxonomy_metadata_wpdbfix', 0 );
  183. add_action( 'switch_blog', 'wc_taxonomy_metadata_wpdbfix', 0 );
  184. /**
  185. * When a term is split, ensure meta data maintained.
  186. * @param int $old_term_id
  187. * @param int $new_term_id
  188. * @param string $term_taxonomy_id
  189. * @param string $taxonomy
  190. */
  191. function wc_taxonomy_metadata_update_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
  192. global $wpdb;
  193. if ( 'product_cat' === $taxonomy || taxonomy_is_product_attribute( $taxonomy ) ) {
  194. $old_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_termmeta WHERE woocommerce_term_id = %d;", $old_term_id ) );
  195. // Copy across to split term
  196. if ( $old_meta_data ) {
  197. foreach ( $old_meta_data as $meta_data ) {
  198. $wpdb->insert(
  199. "{$wpdb->prefix}woocommerce_termmeta",
  200. array(
  201. 'woocommerce_term_id' => $new_term_id,
  202. 'meta_key' => $meta_data->meta_key,
  203. 'meta_value' => $meta_data->meta_value
  204. )
  205. );
  206. }
  207. }
  208. }
  209. }
  210. add_action( 'split_shared_term', 'wc_taxonomy_metadata_update_content_for_split_terms', 10, 4 );
  211. /**
  212. * WooCommerce Term Meta API - Update term meta.
  213. *
  214. * @param mixed $term_id
  215. * @param string $meta_key
  216. * @param mixed $meta_value
  217. * @param string $prev_value (default: '')
  218. * @return bool
  219. */
  220. function update_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $prev_value = '' ) {
  221. return update_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $prev_value );
  222. }
  223. /**
  224. * WooCommerce Term Meta API - Add term meta.
  225. *
  226. * @param mixed $term_id
  227. * @param mixed $meta_key
  228. * @param mixed $meta_value
  229. * @param bool $unique (default: false)
  230. * @return bool
  231. */
  232. function add_woocommerce_term_meta( $term_id, $meta_key, $meta_value, $unique = false ){
  233. return add_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $unique );
  234. }
  235. /**
  236. * WooCommerce Term Meta API - Delete term meta.
  237. *
  238. * @param mixed $term_id
  239. * @param mixed $meta_key
  240. * @param string $meta_value (default: '')
  241. * @param bool $delete_all (default: false)
  242. * @return bool
  243. */
  244. function delete_woocommerce_term_meta( $term_id, $meta_key, $meta_value = '', $delete_all = false ) {
  245. return delete_metadata( 'woocommerce_term', $term_id, $meta_key, $meta_value, $delete_all );
  246. }
  247. /**
  248. * WooCommerce Term Meta API - Get term meta.
  249. *
  250. * @param mixed $term_id
  251. * @param string $key
  252. * @param bool $single (default: true)
  253. * @return mixed
  254. */
  255. function get_woocommerce_term_meta( $term_id, $key, $single = true ) {
  256. return get_metadata( 'woocommerce_term', $term_id, $key, $single );
  257. }
  258. /**
  259. * Move a term before the a given element of its hierarchy level.
  260. *
  261. * @param int $the_term
  262. * @param int $next_id the id of the next sibling element in save hierarchy level
  263. * @param string $taxonomy
  264. * @param int $index (default: 0)
  265. * @param mixed $terms (default: null)
  266. * @return int
  267. */
  268. function wc_reorder_terms( $the_term, $next_id, $taxonomy, $index = 0, $terms = null ) {
  269. if( ! $terms ) $terms = get_terms($taxonomy, 'menu_order=ASC&hide_empty=0&parent=0' );
  270. if( empty( $terms ) ) return $index;
  271. $id = $the_term->term_id;
  272. $term_in_level = false; // flag: is our term to order in this level of terms
  273. foreach ($terms as $term) {
  274. if( $term->term_id == $id ) { // our term to order, we skip
  275. $term_in_level = true;
  276. continue; // our term to order, we skip
  277. }
  278. // the nextid of our term to order, lets move our term here
  279. if(null !== $next_id && $term->term_id == $next_id) {
  280. $index++;
  281. $index = wc_set_term_order($id, $index, $taxonomy, true);
  282. }
  283. // set order
  284. $index++;
  285. $index = wc_set_term_order($term->term_id, $index, $taxonomy);
  286. // if that term has children we walk through them
  287. $children = get_terms($taxonomy, "parent={$term->term_id}&menu_order=ASC&hide_empty=0");
  288. if( !empty($children) ) {
  289. $index = wc_reorder_terms( $the_term, $next_id, $taxonomy, $index, $children );
  290. }
  291. }
  292. // no nextid meaning our term is in last position
  293. if( $term_in_level && null === $next_id )
  294. $index = wc_set_term_order($id, $index+1, $taxonomy, true);
  295. return $index;
  296. }
  297. /**
  298. * Set the sort order of a term.
  299. *
  300. * @param int $term_id
  301. * @param int $index
  302. * @param string $taxonomy
  303. * @param bool $recursive (default: false)
  304. * @return int
  305. */
  306. function wc_set_term_order( $term_id, $index, $taxonomy, $recursive = false ) {
  307. $term_id = (int) $term_id;
  308. $index = (int) $index;
  309. // Meta name
  310. if ( taxonomy_is_product_attribute( $taxonomy ) )
  311. $meta_name = 'order_' . esc_attr( $taxonomy );
  312. else
  313. $meta_name = 'order';
  314. update_woocommerce_term_meta( $term_id, $meta_name, $index );
  315. if( ! $recursive ) return $index;
  316. $children = get_terms($taxonomy, "parent=$term_id&menu_order=ASC&hide_empty=0");
  317. foreach ( $children as $term ) {
  318. $index ++;
  319. $index = wc_set_term_order($term->term_id, $index, $taxonomy, true);
  320. }
  321. clean_term_cache( $term_id, $taxonomy );
  322. return $index;
  323. }
  324. /**
  325. * Add term ordering to get_terms.
  326. *
  327. * It enables the support a 'menu_order' parameter to get_terms for the product_cat taxonomy.
  328. * By default it is 'ASC'. It accepts 'DESC' too.
  329. *
  330. * To disable it, set it ot false (or 0).
  331. *
  332. * @param array $clauses
  333. * @param array $taxonomies
  334. * @param array $args
  335. * @return array
  336. */
  337. function wc_terms_clauses( $clauses, $taxonomies, $args ) {
  338. global $wpdb;
  339. // No sorting when menu_order is false
  340. if ( isset( $args['menu_order'] ) && $args['menu_order'] == false ) {
  341. return $clauses;
  342. }
  343. // No sorting when orderby is non default
  344. if ( isset( $args['orderby'] ) && $args['orderby'] != 'name' ) {
  345. return $clauses;
  346. }
  347. // No sorting in admin when sorting by a column
  348. if ( is_admin() && isset( $_GET['orderby'] ) ) {
  349. return $clauses;
  350. }
  351. // wordpress should give us the taxonomies asked when calling the get_terms function. Only apply to categories and pa_ attributes
  352. $found = false;
  353. foreach ( (array) $taxonomies as $taxonomy ) {
  354. if ( taxonomy_is_product_attribute( $taxonomy ) || in_array( $taxonomy, apply_filters( 'woocommerce_sortable_taxonomies', array( 'product_cat' ) ) ) ) {
  355. $found = true;
  356. break;
  357. }
  358. }
  359. if ( ! $found ) {
  360. return $clauses;
  361. }
  362. // Meta name
  363. if ( ! empty( $taxonomies[0] ) && taxonomy_is_product_attribute( $taxonomies[0] ) ) {
  364. $meta_name = 'order_' . esc_attr( $taxonomies[0] );
  365. } else {
  366. $meta_name = 'order';
  367. }
  368. // query fields
  369. if ( strpos( 'COUNT(*)', $clauses['fields'] ) === false ) {
  370. $clauses['fields'] .= ', tm.* ';
  371. }
  372. //query join
  373. $clauses['join'] .= " LEFT JOIN {$wpdb->woocommerce_termmeta} AS tm ON (t.term_id = tm.woocommerce_term_id AND tm.meta_key = '". $meta_name ."') ";
  374. // default to ASC
  375. if ( ! isset( $args['menu_order'] ) || ! in_array( strtoupper($args['menu_order']), array('ASC', 'DESC')) ) {
  376. $args['menu_order'] = 'ASC';
  377. }
  378. $order = "ORDER BY tm.meta_value+0 " . $args['menu_order'];
  379. if ( $clauses['orderby'] ):
  380. $clauses['orderby'] = str_replace('ORDER BY', $order . ',', $clauses['orderby'] );
  381. else:
  382. $clauses['orderby'] = $order;
  383. endif;
  384. return $clauses;
  385. }
  386. add_filter( 'terms_clauses', 'wc_terms_clauses', 10, 3 );
  387. /**
  388. * Function for recounting product terms, ignoring hidden products.
  389. *
  390. * @param array $terms
  391. * @param string $taxonomy
  392. * @param bool $callback
  393. * @param bool $terms_are_term_taxonomy_ids
  394. */
  395. function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_taxonomy_ids = true ) {
  396. global $wpdb;
  397. // Standard callback
  398. if ( $callback ) {
  399. _update_post_term_count( $terms, $taxonomy );
  400. }
  401. // Stock query
  402. if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
  403. $stock_join = "LEFT JOIN {$wpdb->postmeta} AS meta_stock ON posts.ID = meta_stock.post_id";
  404. $stock_query = "
  405. AND meta_stock.meta_key = '_stock_status'
  406. AND meta_stock.meta_value = 'instock'
  407. ";
  408. } else {
  409. $stock_query = $stock_join = '';
  410. }
  411. // Main query
  412. $count_query = "
  413. SELECT COUNT( DISTINCT posts.ID ) FROM {$wpdb->posts} as posts
  414. LEFT JOIN {$wpdb->postmeta} AS meta_visibility ON posts.ID = meta_visibility.post_id
  415. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
  416. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  417. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  418. LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id
  419. $stock_join
  420. WHERE post_status = 'publish'
  421. AND post_type = 'product'
  422. AND meta_visibility.meta_key = '_visibility'
  423. AND meta_visibility.meta_value IN ( 'visible', 'catalog' )
  424. $stock_query
  425. ";
  426. // Pre-process term taxonomy ids
  427. if ( ! $terms_are_term_taxonomy_ids ) {
  428. // We passed in an array of TERMS in format id=>parent
  429. $terms = array_filter( (array) array_keys( $terms ) );
  430. } else {
  431. // If we have term taxonomy IDs we need to get the term ID
  432. $term_taxonomy_ids = $terms;
  433. $terms = array();
  434. foreach ( $term_taxonomy_ids as $term_taxonomy_id ) {
  435. $term = get_term_by( 'term_taxonomy_id', $term_taxonomy_id, $taxonomy->name );
  436. $terms[] = $term->term_id;
  437. }
  438. }
  439. // Exit if we have no terms to count
  440. if ( ! $terms ) {
  441. return;
  442. }
  443. // Ancestors need counting
  444. if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
  445. foreach ( $terms as $term_id ) {
  446. $terms = array_merge( $terms, get_ancestors( $term_id, $taxonomy->name ) );
  447. }
  448. }
  449. // Unique terms only
  450. $terms = array_unique( $terms );
  451. // Count the terms
  452. foreach ( $terms as $term_id ) {
  453. $terms_to_count = array( absint( $term_id ) );
  454. if ( is_taxonomy_hierarchical( $taxonomy->name ) ) {
  455. // We need to get the $term's hierarchy so we can count its children too
  456. if ( ( $children = get_term_children( $term_id, $taxonomy->name ) ) && ! is_wp_error( $children ) ) {
  457. $terms_to_count = array_unique( array_map( 'absint', array_merge( $terms_to_count, $children ) ) );
  458. }
  459. }
  460. // Generate term query
  461. $term_query = 'AND term_id IN ( ' . implode( ',', $terms_to_count ) . ' )';
  462. // Get the count
  463. $count = $wpdb->get_var( $count_query . $term_query );
  464. // Update the count
  465. update_woocommerce_term_meta( $term_id, 'product_count_' . $taxonomy->name, absint( $count ) );
  466. }
  467. delete_transient( 'wc_term_counts' );
  468. }
  469. /**
  470. * Recount terms after the stock amount changes.
  471. *
  472. * @param int $product_id
  473. */
  474. function wc_recount_after_stock_change( $product_id ) {
  475. if ( get_option( 'woocommerce_hide_out_of_stock_items' ) != 'yes' )
  476. return;
  477. $product_terms = get_the_terms( $product_id, 'product_cat' );
  478. if ( $product_terms ) {
  479. $product_cats = array();
  480. foreach ( $product_terms as $term ) {
  481. $product_cats[ $term->term_id ] = $term->parent;
  482. }
  483. _wc_term_recount( $product_cats, get_taxonomy( 'product_cat' ), false, false );
  484. }
  485. $product_terms = get_the_terms( $product_id, 'product_tag' );
  486. if ( $product_terms ) {
  487. $product_tags = array();
  488. foreach ( $product_terms as $term ) {
  489. $product_tags[ $term->term_id ] = $term->parent;
  490. }
  491. _wc_term_recount( $product_tags, get_taxonomy( 'product_tag' ), false, false );
  492. }
  493. }
  494. add_action( 'woocommerce_product_set_stock_status', 'wc_recount_after_stock_change' );
  495. /**
  496. * Overrides the original term count for product categories and tags with the product count.
  497. * that takes catalog visibility into account.
  498. *
  499. * @param array $terms
  500. * @param string|array $taxonomies
  501. * @return array
  502. */
  503. function wc_change_term_counts( $terms, $taxonomies ) {
  504. if ( is_admin() || is_ajax() ) {
  505. return $terms;
  506. }
  507. if ( ! isset( $taxonomies[0] ) || ! in_array( $taxonomies[0], apply_filters( 'woocommerce_change_term_counts', array( 'product_cat', 'product_tag' ) ) ) ) {
  508. return $terms;
  509. }
  510. $term_counts = $o_term_counts = get_transient( 'wc_term_counts' );
  511. foreach ( $terms as &$term ) {
  512. if ( is_object( $term ) ) {
  513. $term_counts[ $term->term_id ] = isset( $term_counts[ $term->term_id ] ) ? $term_counts[ $term->term_id ] : get_woocommerce_term_meta( $term->term_id, 'product_count_' . $taxonomies[0] , true );
  514. if ( $term_counts[ $term->term_id ] !== '' ) {
  515. $term->count = absint( $term_counts[ $term->term_id ] );
  516. }
  517. }
  518. }
  519. // Update transient
  520. if ( $term_counts != $o_term_counts ) {
  521. set_transient( 'wc_term_counts', $term_counts, DAY_IN_SECONDS * 30 );
  522. }
  523. return $terms;
  524. }
  525. add_filter( 'get_terms', 'wc_change_term_counts', 10, 2 );
  526. /**
  527. * Return products in a given term, and cache value.
  528. *
  529. * To keep in sync, product_count will be cleared on "set_object_terms".
  530. *
  531. * @param int $term_id
  532. * @param string $taxonomy
  533. * @return array
  534. */
  535. function wc_get_term_product_ids( $term_id, $taxonomy ) {
  536. $product_ids = get_woocommerce_term_meta( $term_id, 'product_ids', true );
  537. if ( false === $product_ids || ! is_array( $product_ids ) ) {
  538. $product_ids = get_objects_in_term( $term_id, $taxonomy );
  539. update_woocommerce_term_meta( $term_id, 'product_ids', $product_ids );
  540. }
  541. return $product_ids;
  542. }
  543. /**
  544. * When a post is updated and terms recounted (called by _update_post_term_count), clear the ids.
  545. * @param int $term_id
  546. * @param int $object_id Object ID.
  547. * @param array $terms An array of object terms.
  548. * @param array $tt_ids An array of term taxonomy IDs.
  549. * @param string $taxonomy Taxonomy slug.
  550. * @param bool $append Whether to append new terms to the old terms.
  551. * @param array $old_tt_ids Old array of term taxonomy IDs.
  552. */
  553. function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
  554. foreach ( $old_tt_ids as $term_id ) {
  555. delete_woocommerce_term_meta( $term_id, 'product_ids' );
  556. }
  557. foreach ( $tt_ids as $term_id ) {
  558. delete_woocommerce_term_meta( $term_id, 'product_ids' );
  559. }
  560. }
  561. add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 );