From 680b8b6b0166858343ca32951c8a528081a636fe Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Fri, 2 Feb 2018 10:52:24 -0800 Subject: [PATCH 1/2] Fix bulk sale/regular price percentage handling --- includes/admin/class-wc-admin-post-types.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/includes/admin/class-wc-admin-post-types.php b/includes/admin/class-wc-admin-post-types.php index a2027556696..15ac6947d4b 100644 --- a/includes/admin/class-wc-admin-post-types.php +++ b/includes/admin/class-wc-admin-post-types.php @@ -511,7 +511,10 @@ class WC_Admin_Post_Types { 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. - $regular_price = wc_format_decimal( wc_clean( wp_unslash( $_REQUEST['_regular_price'] ) ) ); // WPCS: input var ok, sanitization ok. + $regular_price = wc_clean( wp_unslash( $_REQUEST['_regular_price'] ) ); // WPCS: input var ok, sanitization ok. + if ( ! strstr( $regular_price, '%' ) ) { + $regular_price = wc_format_decimal( $regular_price ); + } switch ( $change_regular_price ) { case 1: @@ -547,7 +550,10 @@ class WC_Admin_Post_Types { 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. - $sale_price = wc_format_decimal( wc_clean( wp_unslash( $_REQUEST['_sale_price'] ) ) ); // WPCS: input var ok, sanitization ok. + $sale_price = wc_clean( wp_unslash( $_REQUEST['_sale_price'] ) ); // WPCS: input var ok, sanitization ok. + if ( ! strstr( $sale_price, '%' ) ) { + $sale_price = wc_format_decimal( $sale_price ); + } switch ( $change_sale_price ) { case 1: From 56230e9189110e284605de90749300e8c3e219cc Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Fri, 2 Feb 2018 11:45:23 -0800 Subject: [PATCH 2/2] Refactor to handle percents with comma separators --- includes/admin/class-wc-admin-post-types.php | 34 +++++++++----------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/includes/admin/class-wc-admin-post-types.php b/includes/admin/class-wc-admin-post-types.php index 15ac6947d4b..68f048c6acd 100644 --- a/includes/admin/class-wc-admin-post-types.php +++ b/includes/admin/class-wc-admin-post-types.php @@ -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. $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. - if ( ! strstr( $regular_price, '%' ) ) { - $regular_price = wc_format_decimal( $regular_price ); - } + $raw_regular_price = wc_clean( wp_unslash( $_REQUEST['_regular_price'] ) ); // WPCS: input var ok, sanitization ok. + $is_percentage = (bool) strstr( $raw_regular_price, '%' ); + $regular_price = wc_format_decimal( $raw_regular_price ); switch ( $change_regular_price ) { case 1: $new_price = $regular_price; break; case 2: - if ( strstr( $regular_price, '%' ) ) { - $percent = str_replace( '%', '', $regular_price ) / 100; + if ( $is_percentage ) { + $percent = $regular_price / 100; $new_price = $old_regular_price + ( round( $old_regular_price * $percent, wc_get_price_decimals() ) ); } else { $new_price = $old_regular_price + $regular_price; } break; case 3: - if ( strstr( $regular_price, '%' ) ) { - $percent = str_replace( '%', '', $regular_price ) / 100; + if ( $is_percentage ) { + $percent = $regular_price / 100; $new_price = max( 0, $old_regular_price - ( round( $old_regular_price * $percent, wc_get_price_decimals() ) ) ); } else { $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. $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. - if ( ! strstr( $sale_price, '%' ) ) { - $sale_price = wc_format_decimal( $sale_price ); - } + $raw_sale_price = wc_clean( wp_unslash( $_REQUEST['_sale_price'] ) ); // WPCS: input var ok, sanitization ok. + $is_percentage = (bool) strstr( $raw_sale_price, '%' ); + $sale_price = wc_format_decimal( $raw_sale_price ); switch ( $change_sale_price ) { case 1: $new_price = $sale_price; break; case 2: - if ( strstr( $sale_price, '%' ) ) { - $percent = str_replace( '%', '', $sale_price ) / 100; + if ( $is_percentage ) { + $percent = $sale_price / 100; $new_price = $old_sale_price + ( $old_sale_price * $percent ); } else { $new_price = $old_sale_price + $sale_price; } break; case 3: - if ( strstr( $sale_price, '%' ) ) { - $percent = str_replace( '%', '', $sale_price ) / 100; + if ( $is_percentage ) { + $percent = $sale_price / 100; $new_price = max( 0, $old_sale_price - ( $old_sale_price * $percent ) ); } else { $new_price = max( 0, $old_sale_price - $sale_price ); } break; case 4: - if ( strstr( $sale_price, '%' ) ) { - $percent = str_replace( '%', '', $sale_price ) / 100; + if ( $is_percentage ) { + $percent = $sale_price / 100; $new_price = max( 0, $product->regular_price - ( $product->regular_price * $percent ) ); } else { $new_price = max( 0, $product->regular_price - $sale_price );