Skip to content

Commit

Permalink
Do not copy history on product duplicate (#60)
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
kkarpieszuk authored Jun 23, 2023
1 parent 910eced commit b130cfa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
17 changes: 17 additions & 0 deletions app/PriorPrice/HistoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 16 additions & 1 deletion app/PriorPrice/ProductUpdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
Expand All @@ -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 );
}
}
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b130cfa

Please sign in to comment.