_wc_save_product_price

Save product price.

Description

_wc_save_product_price( (int) $product_id, (float) $regular_price, (string) $sale_price = '', (string) $date_from = '', (string) $date_to = '' ); 

This is a private function (internal use ONLY) used until a data manipulation api is built.

Parameters (5)

0. $product_id (int)
The product id.
1. $regular_price (float)
The regular price.
2. $sale_price — Optional. (string) => ''
The sale price.
3. $date_from — Optional. (string) => ''
The date from.
4. $date_to — Optional. (string) => ''
The date to.

Usage

  1. if ( !function_exists( '_wc_save_product_price' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-deprecated-functions.php'; 
  3.  
  4. // The product id. 
  5. $product_id = -1; 
  6.  
  7. // The regular price. 
  8. $regular_price = null; 
  9.  
  10. // The sale price. 
  11. $sale_price = ''; 
  12.  
  13. // The date from. 
  14. $date_from = ''; 
  15.  
  16. // The date to. 
  17. $date_to = ''; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = _wc_save_product_price($product_id, $regular_price, $sale_price, $date_from, $date_to); 
  21.  

Defined (1)

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

/includes/wc-deprecated-functions.php  
  1. function _wc_save_product_price( $product_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '' ) { 
  2. wc_doing_it_wrong( '_wc_save_product_price()', 'This function is not for developer use and is deprecated.', '3.0' ); 
  3.  
  4. $product_id = absint( $product_id ); 
  5. $regular_price = wc_format_decimal( $regular_price ); 
  6. $sale_price = '' === $sale_price ? '' : wc_format_decimal( $sale_price ); 
  7. $date_from = wc_clean( $date_from ); 
  8. $date_to = wc_clean( $date_to ); 
  9.  
  10. update_post_meta( $product_id, '_regular_price', $regular_price ); 
  11. update_post_meta( $product_id, '_sale_price', $sale_price ); 
  12.  
  13. // Save Dates 
  14. update_post_meta( $product_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' ); 
  15. update_post_meta( $product_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' ); 
  16.  
  17. if ( $date_to && ! $date_from ) { 
  18. $date_from = strtotime( 'NOW', current_time( 'timestamp' ) ); 
  19. update_post_meta( $product_id, '_sale_price_dates_from', $date_from ); 
  20.  
  21. // Update price if on sale 
  22. if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) { 
  23. update_post_meta( $product_id, '_price', $sale_price ); 
  24. } else { 
  25. update_post_meta( $product_id, '_price', $regular_price ); 
  26.  
  27. if ( '' !== $sale_price && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) { 
  28. update_post_meta( $product_id, '_price', $sale_price ); 
  29.  
  30. if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) { 
  31. update_post_meta( $product_id, '_price', $regular_price ); 
  32. update_post_meta( $product_id, '_sale_price_dates_from', '' ); 
  33. update_post_meta( $product_id, '_sale_price_dates_to', '' );