wc_delete_product_transients

Clear all transients cache for product data.

Description

wc_delete_product_transients( (int) $post_id = 0 ); 

Parameters (1)

0. $post_id — Optional. (int)
The post id.

Usage

  1. if ( !function_exists( 'wc_delete_product_transients' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_delete_product_transients($post_id); 
  9.  

Defined (1)

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

/includes/wc-product-functions.php  
  1. function wc_delete_product_transients( $post_id = 0 ) { 
  2. // Core transients 
  3. $transients_to_clear = array( 
  4. 'wc_products_onsale',  
  5. 'wc_featured_products',  
  6. 'wc_outofstock_count',  
  7. 'wc_low_stock_count',  
  8. ); 
  9.  
  10. // Transient names that include an ID 
  11. $post_transient_names = array( 
  12. 'wc_product_children_',  
  13. 'wc_var_prices_',  
  14. 'wc_related_',  
  15. 'wc_child_has_weight_',  
  16. 'wc_child_has_dimensions_',  
  17. ); 
  18.  
  19. if ( $post_id > 0 ) { 
  20. foreach ( $post_transient_names as $transient ) { 
  21. $transients_to_clear[] = $transient . $post_id; 
  22.  
  23. // Does this product have a parent? 
  24. $product = wc_get_product( $post_id ); 
  25.  
  26. if ( $product && $product->get_parent_id() > 0 ) { 
  27. wc_delete_product_transients( $product->get_parent_id() ); 
  28.  
  29. // Delete transients 
  30. foreach ( $transients_to_clear as $transient ) { 
  31. delete_transient( $transient ); 
  32.  
  33. // Increments the transient version to invalidate cache 
  34. WC_Cache_Helper::get_transient_version( 'product', true ); 
  35.  
  36. do_action( 'woocommerce_delete_product_transients', $post_id );