diff --git a/app/PriorPrice/HistoryStorage.php b/app/PriorPrice/HistoryStorage.php index 29fbd6e..e18d93a 100644 --- a/app/PriorPrice/HistoryStorage.php +++ b/app/PriorPrice/HistoryStorage.php @@ -140,6 +140,23 @@ public function add_price( int $product_id, float $new_price, bool $on_change_on return $this->save_history( $product_id, $history ); } + /** + * Add first price to the history. + * + * @since 1.7.4 + * + * @param int $product_id Product ID. + * @param float $price Price. + * + * @return int + */ + public function add_first_price( int $product_id, float $price ) { + + $history[ $this->get_time_with_offset() ] = $price; + + return $this->save_history( $product_id, $history ); + } + /** * Add price to the history at given timestamp. * diff --git a/app/PriorPrice/ProductUpdates.php b/app/PriorPrice/ProductUpdates.php index 0ec0288..7ec4755 100644 --- a/app/PriorPrice/ProductUpdates.php +++ b/app/PriorPrice/ProductUpdates.php @@ -21,7 +21,8 @@ public function __construct( HistoryStorage $history_storage ) { */ public function register_hooks(): void { - add_action( 'woocommerce_new_product', [ $this, 'update_price_history' ] ); + add_action( 'woocommerce_new_product', [ $this, 'start_price_history' ] ); + add_action( 'woocommerce_new_product_variation', [ $this, 'start_price_history' ] ); add_action( 'woocommerce_update_product', [ $this, 'update_price_history' ] ); add_action( 'woocommerce_save_product_variation', [ $this, 'update_price_history' ] ); } @@ -39,4 +40,18 @@ public function update_price_history( int $product_id ): void { $this->history_storage->add_price( $product_id, (float) $product_price, true ); } + + /** + * Start price history. + * + * @since 1.7.4 + * + * @param int $product_id Product ID. + */ + public function start_price_history( int $product_id ): void { + + $product_price = get_post_meta( $product_id, '_price', true ); + + $this->history_storage->add_first_price( $product_id, (float) $product_price ); + } } diff --git a/readme.txt b/readme.txt index 088d787..b908dfe 100644 --- a/readme.txt +++ b/readme.txt @@ -106,7 +106,8 @@ Please submit the [GitHub issue](https://github.com/kkarpieszuk/wc-price-history = 1.7.4 = * Improvement: Start saving the price before change with timestamps for last midnight and for 1 second ago. (#58) -* Improvement: Clean history from empty values vefore save. +* Improvement: Clean history from empty values before save. +* Fixed: Do not copy product price history when duplicating product. (#50) = 1.7.3 = * Fixed: When price displayed with shortcode, it was not respecting sale settings and it resulted in showing the current price.