WC_Admin_Post_Types::set_new_price
Set the new regular or sale price if requested.
Метод класса: WC_Admin_Post_Types{}
Хуков нет.
Возвращает
true|false. true if a new price has been set, false otherwise.
Использование
// private - только в коде основоного (родительского) класса $result = $this->set_new_price( $product, $price_type );
- $product(WC_Product) (обязательный)
- The product to set the new price for.
- $price_type(строка) (обязательный)
- 'regular' or 'sale'.
Код WC_Admin_Post_Types::set_new_price() WC Admin Post Types::set new price WC 10.4.3
private function set_new_price( $product, $price_type ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$request_data = $this->request_data();
if ( empty( $request_data[ "change_{$price_type}_price" ] ) || ! isset( $request_data[ "_{$price_type}_price" ] ) ) {
return false;
}
$old_price = $product->{"get_{$price_type}_price"}();
$old_price = '' === $old_price ? (float) $product->get_regular_price() : (float) $old_price;
$price_changed = false;
$change_price = absint( $request_data[ "change_{$price_type}_price" ] );
$raw_price = wc_clean( wp_unslash( $request_data[ "_{$price_type}_price" ] ) );
$is_percentage = (bool) strstr( $raw_price, '%' );
$price = wc_format_decimal( $raw_price );
switch ( $change_price ) {
case 1:
if ( empty( $price ) ) {
$new_price = $product->get_regular_price();
} else {
$new_price = $price;
}
break;
case 2:
if ( $is_percentage ) {
$percent = $price / 100;
$new_price = $old_price + ( $old_price * $percent );
} elseif ( ! empty( $price ) ) {
$new_price = $old_price + $price;
}
break;
case 3:
if ( $is_percentage ) {
$percent = $price / 100;
$new_price = max( 0, $old_price - ( $old_price * $percent ) );
} elseif ( ! empty( $price ) ) {
$new_price = max( 0, $old_price - $price );
}
break;
case 4:
if ( 'sale' !== $price_type ) {
break;
}
$regular_price = $product->get_regular_price();
if ( $is_percentage && is_numeric( $regular_price ) ) {
$percent = $price / 100;
$new_price = max( 0, $regular_price - ( NumberUtil::round( $regular_price * $percent, wc_get_price_decimals() ) ) );
} else {
$new_price = max( 0, (float) $regular_price - (float) $price );
}
break;
default:
break;
}
if ( isset( $new_price ) && $new_price !== $old_price ) {
$price_changed = true;
$new_price = NumberUtil::round( $new_price, wc_get_price_decimals() );
$product->{"set_{$price_type}_price"}( $new_price );
}
return $price_changed;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
}