Skip to content

Commit

Permalink
Allow to decide what to display in case there was no price change in …
Browse files Browse the repository at this point in the history
…the tracked history span.
  • Loading branch information
kkarpieszuk authored Dec 16, 2023
1 parent 6296ecb commit bd59bf0
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 30 deletions.
60 changes: 55 additions & 5 deletions app/PriorPrice/Prices.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,10 @@ public function lowest_price_html( \WC_Product $wc_product ): string {
$lowest = apply_filters( 'wc_price_history_lowest_price_html_raw_value_taxed', $lowest, $wc_product );

if ( (float) $lowest <= 0 ) {
return '';
return $this->handle_old_history( $wc_product, $days_number );
}

$lowest_html = '<div class="wc-price-history prior-price lowest">%s</div>';
$lowest_template = str_replace( [ '{price}', '{days}' ], [ $this->display_price_value_html( $lowest ), $days_number ], $this->settings_data->get_display_text() );

return sprintf( $lowest_html, $lowest_template );
return $this->display_from_template( $lowest, $days_number );
}

/**
Expand Down Expand Up @@ -205,4 +202,57 @@ private function is_main_product( \WC_Product $wc_product ) : bool {

return isset( $wp_query->queried_object_id ) && $wp_query->queried_object_id === $wc_product->get_id();
}

/**
* Handle history older than x days (returned price is 0).
*
* @since 1.9.0
*
* @param \WC_Product $wc_product WC Product.
* @param int $days_number Days number.
*
* @return string
*/
private function handle_old_history( \WC_Product $wc_product, int $days_number ) : string {

$old_history = $this->settings_data->get_old_history();

if ( $old_history === 'hide' ) {
return '';
}

if ( $old_history === 'current_price' ) {
return $this->display_from_template( (float) $wc_product->get_price(), $days_number );
}

$old_history_custom_text = $this->settings_data->get_old_history_custom_text();

$old_history_custom_text = str_replace(
[ '{price}', '{days}' ],
[ $this->display_price_value_html( (float) $wc_product->get_price() ), $days_number ],
$old_history_custom_text
);

return '<div class="wc-price-history prior-price lowest">' . $old_history_custom_text . '</div>';
}

/**
* Display full price HTML from template.
*
* @since 1.9.0
*
* @param float $lowest Lowest price.
* @param int $days_number Days number.
*
* @return string
*/
private function display_from_template( float $lowest, int $days_number ) : string {

$display_text = $this->settings_data->get_display_text();

$display_text = str_replace( '{price}', $this->display_price_value_html( $lowest ), $display_text );
$display_text = str_replace( '{days}', (string) $days_number, $display_text );

return '<div class="wc-price-history prior-price lowest">' . $display_text . '</div>';
}
}
38 changes: 38 additions & 0 deletions app/PriorPrice/SettingsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public function set_defaults() : void {
$update = true;
}

if ( ! isset( $settings['old_history'] ) ) {
$settings['old_history'] = 'custom_text'; // 'hide', 'current_price', 'custom_text'
$settings['old_history_custom_text'] = esc_html__( 'Price in the last {days} days is the same as current', 'wc-price-history' );
$update = true;
}

if ( $update ) {
update_option( 'wc_price_history_settings', $settings );
}
Expand Down Expand Up @@ -151,4 +157,36 @@ public function get_display_line_through() : bool {
}
return (bool) $settings['display_line_through'];
}

/**
* Get old history setting.
*
* @since 1.9.0
*
* @return string
*/
public function get_old_history() : string {

$settings = get_option( 'wc_price_history_settings' );
if ( ! isset( $settings['old_history'] ) ) {
return 'hide';
}
return $settings['old_history'];
}

/**
* Get old history custom text setting.
*
* @since 1.9.0
*
* @return string
*/
public function get_old_history_custom_text() : string {

$settings = get_option( 'wc_price_history_settings' );
if ( ! isset( $settings['old_history_custom_text'] ) ) {
return esc_html__( 'Price in the last {days} days is the same as current', 'wc-price-history' );
}
return esc_html( $settings['old_history_custom_text'] );
}
}
77 changes: 74 additions & 3 deletions app/PriorPrice/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class="wc-history-price-display-when-input"
value="sale_start_inclusive"
<?php checked( isset( $settings['count_from'] ) ? $settings['count_from'] : false, 'sale_start_inclusive' ); ?>
/>
<?php esc_html_e( 'Day when product product went on sale (includes promotional price)', 'wc-price-history' ); ?>
<?php esc_html_e( 'Day when product went on sale (includes promotional price)', 'wc-price-history' ); ?>
</label>
</p>
<p class="description">
Expand Down Expand Up @@ -248,6 +248,7 @@ class="wc-history-price-display-when-input"
<input
type="number"
name="wc_price_history_settings[days_number]"
id="wc-price-history-days-number"
value="<?php echo isset( $settings['days_number'] ) ? $settings['days_number'] : 30; ?>"
/>
<?php esc_attr_e( 'days', 'wc-price-history' ) ?></label>
Expand All @@ -263,12 +264,13 @@ class="wc-history-price-display-when-input"
<td>
<fieldset>
<p>
<label>
<label class="wc-price-history-wide-field">
<input
type="text"
name="wc_price_history_settings[display_text]"
class="wc-price-history-wide-field"
<?php /* translators: Do not translate {price}, it is template slug! */ ?>
value="<?php echo isset( $settings['display_text'] ) ? $settings['display_text'] : __( '30-day low: {price}', 'wc-price-history' ); ?>"
value="<?php echo isset( $settings['display_text'] ) ? $settings['display_text'] : esc_html__( '30-day low: {price}', 'wc-price-history' ); ?>"
/>
</label>
</p>
Expand All @@ -279,6 +281,75 @@ class="wc-history-price-display-when-input"
</fieldset>
</td>
</tr>
<tr>
<th scope="row">
<?php
printf(
/* translators: %s: {days-set} template slug */
esc_html__( 'What to do when price history is older than %s days:', 'wc-price-history' ),
'<span class="wc-price-history-days-set">' . $settings['days_number'] . '</span>'
);
?>
</th>
<td>
<fieldset id="wc-price-history-old-history-fieldset">
<p class="description" >
<?php
printf(
/* translators: Do not translate {days-set}, it is template slug! */
esc_html__( 'It could happen that the last change of price was more than %s days ago. In that case you can choose to hide minimal price, display current price or display custom text.', 'wc-price-history' ),
'<span class="wc-price-history-days-set">' . $settings['days_number'] . '</span>'
);
?>
</p>
<p>
<label>
<input
type="radio"
name="wc_price_history_settings[old_history]"
value="hide"
<?php checked( isset( $settings['old_history'] ) ? $settings['old_history'] : false, 'hide' ); ?>
/>
<?php esc_html_e( 'Hide minimal price', 'wc-price-history' ); ?>
</label>
<br />
<label>
<input
type="radio"
name="wc_price_history_settings[old_history]"
value="current_price"
<?php checked( isset( $settings['old_history'] ) ? $settings['old_history'] : false, 'current_price' ); ?>
/>
<?php esc_html_e( 'Display current price', 'wc-price-history' ); ?>
</label>
<br />
<label>
<input
type="radio"
name="wc_price_history_settings[old_history]"
value="custom_text"
<?php checked( isset( $settings['old_history'] ) ? $settings['old_history'] : false, 'custom_text' ); ?>
/>
<?php esc_html_e( 'Display custom text', 'wc-price-history' ); ?>:
</label>
</p>
<p class="wc-price-history-old-history-custom-text-p <?php echo isset( $settings['old_history'] ) && $settings['old_history'] === 'custom_text' ? '' : 'hidden-fade'; ?>">
<label class="wc-price-history-wide-field">
<input
type="text"
name="wc_price_history_settings[old_history_custom_text]"
class="wc-price-history-wide-field"
<?php /* translators: Do not translate {days}, it is template slug! */ ?>
value="<?php echo isset( $settings['old_history_custom_text'] ) ? $settings['old_history_custom_text'] : esc_html__( 'Price in the last {days} days is the same as current', 'wc-price-history' ); ?>"
/>
</label>
</p>
<p class="description wc-price-history-old-history-custom-text-p <?php echo isset( $settings['old_history'] ) && $settings['old_history'] === 'custom_text' ? '' : 'hidden-fade'; ?>">
<?php esc_html_e( 'If you choose to display custom text, use placeholder {days} to display number of days and {price} to display current price.', 'wc-price-history' ); ?>
</p>
</fieldset>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'When displaying minimal price:', 'wc-price-history' ); ?></th>
<td>
Expand Down
15 changes: 15 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

.wc-price-history-wide-field {
width: 90%;
}

.wc-price-history-old-history-custom-text-p {
opacity: 1;
height: 100%;
overflow: hidden;
transition: opacity 0.5s, height 0.5s;
}

.wc-price-history-old-history-custom-text-p.hidden-fade {
opacity: 0;
height: 0;
}

.wc-history-price-admin__left {
float: left;
Expand Down
25 changes: 25 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
jQuery(document).ready(function($) {

// Toggle the custom text field when the custom text radio button is selected.
$( '#wc-price-history-old-history-fieldset input' ).on(
'change',
function() {
if ( $( '#wc-price-history-old-history-fieldset input:checked' ).val() == 'custom_text' ) {
$( '.wc-price-history-old-history-custom-text-p' ).removeClass( 'hidden-fade' );
} else {
$( '.wc-price-history-old-history-custom-text-p' ).addClass( 'hidden-fade' );
}
}
);

// There are tesxts like "What to do when price history is older than {days-set} days" on the page's HTML code.
// copy value from input field #wc-price-history-days-number to every place on page where is {days-set} placeholder in pages HTML (replace it).
$( '#wc-price-history-days-number' ).on(
'input',
function() {
$( '.wc-price-history-days-set' ).each(
function() {
$( this ).text( $( '#wc-price-history-days-number' ).val() );
}
);
}
);
});
Loading

0 comments on commit bd59bf0

Please sign in to comment.