_wc_save_product_price()
Deprecated since 3.0.0. It is no longer supported and may be removed in future releases. It is recommended to replace this function with the same one.
Save product price.
This is a private function (internal use ONLY) used until a data manipulation api is built.
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
null. Nothing (null).
Usage
_wc_save_product_price( $product_id, $regular_price, $sale_price, $date_from, $date_to );
- $product_id(int) (required)
- .
- $regular_price(float) (required)
- .
- $sale_price(float)
- .
Default:'' - $date_from(string)
- .
Default:'' - $date_to(string)
- .
Default:''
Changelog
| Deprecated since | 3.0.0 |
_wc_save_product_price() wc save product price code WC 10.6.2
function _wc_save_product_price( $product_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '' ) {
wc_doing_it_wrong( '_wc_save_product_price()', 'This function is not for developer use and is deprecated.', '3.0' );
$product_id = absint( $product_id );
$regular_price = wc_format_decimal( $regular_price );
$sale_price = '' === $sale_price ? '' : wc_format_decimal( $sale_price );
$date_from = wc_clean( $date_from );
$date_to = wc_clean( $date_to );
update_post_meta( $product_id, '_regular_price', $regular_price );
update_post_meta( $product_id, '_sale_price', $sale_price );
// Save Dates
update_post_meta( $product_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' );
update_post_meta( $product_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' );
if ( $date_to && ! $date_from ) {
$date_from = strtotime( 'NOW', current_time( 'timestamp' ) );
update_post_meta( $product_id, '_sale_price_dates_from', $date_from );
}
// Update price if on sale
if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) {
update_post_meta( $product_id, '_price', $sale_price );
} else {
update_post_meta( $product_id, '_price', $regular_price );
}
if ( '' !== $sale_price && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $product_id, '_price', $sale_price );
}
if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $product_id, '_price', $regular_price );
update_post_meta( $product_id, '_sale_price_dates_from', '' );
update_post_meta( $product_id, '_sale_price_dates_to', '' );
}
}