From fb2d8cb3946b7388bb41aa08a639cb248a0edf21 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 22 Aug 2017 11:43:48 +0100 Subject: [PATCH] Prevent regular price being lower than the sale price Closes #16560 --- .../class-wc-product-data-store-cpt.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index 9bffbfacdb2..d4a533b6476 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -85,7 +85,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * @param WC_Product $product */ public function create( &$product ) { - if ( ! $product->get_date_created() ) { + if ( ! $product->get_date_created( 'edit' ) ) { $product->set_date_created( current_time( 'timestamp', true ) ); } @@ -544,10 +544,16 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * Handle updated meta props after updating meta data. * * @since 3.0.0 - * @param WC_Product $product + * @param WC_Product $product Product Object. */ protected function handle_updated_props( &$product ) { - if ( in_array( 'date_on_sale_from', $this->updated_props ) || in_array( 'date_on_sale_to', $this->updated_props ) || in_array( 'regular_price', $this->updated_props ) || in_array( 'sale_price', $this->updated_props ) ) { + if ( in_array( 'regular_price', $this->updated_props, true ) || in_array( 'sale_price', $this->updated_props, true ) ) { + if ( $product->get_sale_price( 'edit' ) >= $product->get_regular_price( 'edit' ) ) { + update_post_meta( $product->get_id(), '_sale_price', '' ); + $product->set_sale_price( '' ); + } + } + if ( in_array( 'date_on_sale_from', $this->updated_props, true ) || in_array( 'date_on_sale_to', $this->updated_props, true ) || in_array( 'regular_price', $this->updated_props, true ) || in_array( 'sale_price', $this->updated_props, true ) ) { if ( $product->is_on_sale( 'edit' ) ) { update_post_meta( $product->get_id(), '_price', $product->get_sale_price( 'edit' ) ); $product->set_price( $product->get_sale_price( 'edit' ) ); @@ -557,11 +563,11 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da } } - if ( in_array( 'stock_quantity', $this->updated_props ) ) { + if ( in_array( 'stock_quantity', $this->updated_props, true ) ) { do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock' , $product ); } - if ( in_array( 'stock_status', $this->updated_props ) ) { + if ( in_array( 'stock_status', $this->updated_props, true ) ) { do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock_status' : 'woocommerce_product_set_stock_status' , $product->get_id(), $product->get_stock_status(), $product ); }