Refactor to handle percents with comma separators

This commit is contained in:
claudiulodro 2018-02-02 11:45:23 -08:00
parent 680b8b6b01
commit 56230e9189
1 changed files with 16 additions and 18 deletions

View File

@ -511,26 +511,25 @@ class WC_Admin_Post_Types {
if ( ! empty( $_REQUEST['change_regular_price'] ) && isset( $_REQUEST['_regular_price'] ) ) { // WPCS: input var ok, sanitization ok. if ( ! empty( $_REQUEST['change_regular_price'] ) && isset( $_REQUEST['_regular_price'] ) ) { // WPCS: input var ok, sanitization ok.
$change_regular_price = absint( $_REQUEST['change_regular_price'] ); // WPCS: input var ok, sanitization ok. $change_regular_price = absint( $_REQUEST['change_regular_price'] ); // WPCS: input var ok, sanitization ok.
$regular_price = wc_clean( wp_unslash( $_REQUEST['_regular_price'] ) ); // WPCS: input var ok, sanitization ok. $raw_regular_price = wc_clean( wp_unslash( $_REQUEST['_regular_price'] ) ); // WPCS: input var ok, sanitization ok.
if ( ! strstr( $regular_price, '%' ) ) { $is_percentage = (bool) strstr( $raw_regular_price, '%' );
$regular_price = wc_format_decimal( $regular_price ); $regular_price = wc_format_decimal( $raw_regular_price );
}
switch ( $change_regular_price ) { switch ( $change_regular_price ) {
case 1: case 1:
$new_price = $regular_price; $new_price = $regular_price;
break; break;
case 2: case 2:
if ( strstr( $regular_price, '%' ) ) { if ( $is_percentage ) {
$percent = str_replace( '%', '', $regular_price ) / 100; $percent = $regular_price / 100;
$new_price = $old_regular_price + ( round( $old_regular_price * $percent, wc_get_price_decimals() ) ); $new_price = $old_regular_price + ( round( $old_regular_price * $percent, wc_get_price_decimals() ) );
} else { } else {
$new_price = $old_regular_price + $regular_price; $new_price = $old_regular_price + $regular_price;
} }
break; break;
case 3: case 3:
if ( strstr( $regular_price, '%' ) ) { if ( $is_percentage ) {
$percent = str_replace( '%', '', $regular_price ) / 100; $percent = $regular_price / 100;
$new_price = max( 0, $old_regular_price - ( round( $old_regular_price * $percent, wc_get_price_decimals() ) ) ); $new_price = max( 0, $old_regular_price - ( round( $old_regular_price * $percent, wc_get_price_decimals() ) ) );
} else { } else {
$new_price = max( 0, $old_regular_price - $regular_price ); $new_price = max( 0, $old_regular_price - $regular_price );
@ -550,34 +549,33 @@ class WC_Admin_Post_Types {
if ( ! empty( $_REQUEST['change_sale_price'] ) && isset( $_REQUEST['_sale_price'] ) ) { // WPCS: input var ok, sanitization ok. if ( ! empty( $_REQUEST['change_sale_price'] ) && isset( $_REQUEST['_sale_price'] ) ) { // WPCS: input var ok, sanitization ok.
$change_sale_price = absint( $_REQUEST['change_sale_price'] ); // WPCS: input var ok, sanitization ok. $change_sale_price = absint( $_REQUEST['change_sale_price'] ); // WPCS: input var ok, sanitization ok.
$sale_price = wc_clean( wp_unslash( $_REQUEST['_sale_price'] ) ); // WPCS: input var ok, sanitization ok. $raw_sale_price = wc_clean( wp_unslash( $_REQUEST['_sale_price'] ) ); // WPCS: input var ok, sanitization ok.
if ( ! strstr( $sale_price, '%' ) ) { $is_percentage = (bool) strstr( $raw_sale_price, '%' );
$sale_price = wc_format_decimal( $sale_price ); $sale_price = wc_format_decimal( $raw_sale_price );
}
switch ( $change_sale_price ) { switch ( $change_sale_price ) {
case 1: case 1:
$new_price = $sale_price; $new_price = $sale_price;
break; break;
case 2: case 2:
if ( strstr( $sale_price, '%' ) ) { if ( $is_percentage ) {
$percent = str_replace( '%', '', $sale_price ) / 100; $percent = $sale_price / 100;
$new_price = $old_sale_price + ( $old_sale_price * $percent ); $new_price = $old_sale_price + ( $old_sale_price * $percent );
} else { } else {
$new_price = $old_sale_price + $sale_price; $new_price = $old_sale_price + $sale_price;
} }
break; break;
case 3: case 3:
if ( strstr( $sale_price, '%' ) ) { if ( $is_percentage ) {
$percent = str_replace( '%', '', $sale_price ) / 100; $percent = $sale_price / 100;
$new_price = max( 0, $old_sale_price - ( $old_sale_price * $percent ) ); $new_price = max( 0, $old_sale_price - ( $old_sale_price * $percent ) );
} else { } else {
$new_price = max( 0, $old_sale_price - $sale_price ); $new_price = max( 0, $old_sale_price - $sale_price );
} }
break; break;
case 4: case 4:
if ( strstr( $sale_price, '%' ) ) { if ( $is_percentage ) {
$percent = str_replace( '%', '', $sale_price ) / 100; $percent = $sale_price / 100;
$new_price = max( 0, $product->regular_price - ( $product->regular_price * $percent ) ); $new_price = max( 0, $product->regular_price - ( $product->regular_price * $percent ) );
} else { } else {
$new_price = max( 0, $product->regular_price - $sale_price ); $new_price = max( 0, $product->regular_price - $sale_price );