From 5e149537963507c301783993bbcdf9fa817805a2 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 11 Jun 2018 11:44:58 +0100 Subject: [PATCH 1/2] Avoid price setting when synced with children --- .../class-wc-product-data-store-cpt.php | 29 +++++++++++-------- ...lass-wc-product-grouped-data-store-cpt.php | 4 ++- ...ass-wc-product-variable-data-store-cpt.php | 2 ++ 3 files changed, 22 insertions(+), 13 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 d85151f8bd3..8c1cc6e3dc3 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -594,19 +594,24 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * @param WC_Product $product Product Object. */ protected function handle_updated_props( &$product ) { - 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( '' ); + $price_is_synced = $product->is_type( array( 'variable', 'grouped' ) ); + + if ( ! $price_is_synced ) { + 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 ) || in_array( 'product_type', $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' ) ); - } else { - update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) ); - $product->set_price( $product->get_regular_price( 'edit' ) ); + + 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 ) || in_array( 'product_type', $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' ) ); + } else { + update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) ); + $product->set_price( $product->get_regular_price( 'edit' ) ); + } } } diff --git a/includes/data-stores/class-wc-product-grouped-data-store-cpt.php b/includes/data-stores/class-wc-product-grouped-data-store-cpt.php index 393663c3e1d..ea3b9adacdd 100644 --- a/includes/data-stores/class-wc-product-grouped-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-grouped-data-store-cpt.php @@ -74,11 +74,13 @@ class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implem foreach ( $product->get_children( 'edit' ) as $child_id ) { $child = wc_get_product( $child_id ); if ( $child ) { - $child_prices[] = $child->get_price(); + $child_prices[] = $child->get_price( 'edit' ); } } $child_prices = array_filter( $child_prices ); delete_post_meta( $product->get_id(), '_price' ); + delete_post_meta( $product->get_id(), '_sale_price' ); + delete_post_meta( $product->get_id(), '_regular_price' ); if ( ! empty( $child_prices ) ) { add_post_meta( $product->get_id(), '_price', min( $child_prices ) ); diff --git a/includes/data-stores/class-wc-product-variable-data-store-cpt.php b/includes/data-stores/class-wc-product-variable-data-store-cpt.php index 951a0dc97d3..91e19171492 100644 --- a/includes/data-stores/class-wc-product-variable-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-variable-data-store-cpt.php @@ -515,6 +515,8 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple $prices = $children ? array_unique( $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . ' )' ) ) : array(); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared delete_post_meta( $product->get_id(), '_price' ); + delete_post_meta( $product->get_id(), '_sale_price' ); + delete_post_meta( $product->get_id(), '_regular_price' ); if ( $prices ) { sort( $prices ); From 9a550c4a35a5b255b20db019d87552564155c0e0 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 14 Jun 2018 16:34:36 +0100 Subject: [PATCH 2/2] Update to correct type in unit test --- tests/unit-tests/product/query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-tests/product/query.php b/tests/unit-tests/product/query.php index 8d9da11f1a8..a1c4dc7f6f0 100644 --- a/tests/unit-tests/product/query.php +++ b/tests/unit-tests/product/query.php @@ -31,7 +31,7 @@ class WC_Tests_WC_Product_Query extends WC_Unit_Test_Case { $product1->set_sale_price( '5.00' ); $product1->save(); - $product2 = new WC_Product_Grouped(); + $product2 = new WC_Product_Simple(); $product2->set_sku( 'sku2' ); $product2->set_regular_price( '12.50' ); $product2->set_sale_price( '5.00' );