Reputation: 2003
I got this code from here ( Display woocommerce sale end date )
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product )
{
global $post;
$sales_price_to = get_post_meta($post->ID, '_sale_price_dates_to', true);
if(is_single() && $sales_price_to != "")
{
$sales_price_date_to = date("j M y", $sales_price_to);
return str_replace( '</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price );
}
else
{
return apply_filters( 'woocommerce_get_price', $price );
}
}
This only displays the Sale Date to. How do I make it display the sale date from till sale date to?
I tried editing this line:
return str_replace( '</ins>', ' </ins> <b>(Offer till '.$sales_price_date_to.')</b>', $price );
to
return str_replace( '</ins>', ' </ins> <b>(Offer from '.$sales_price_date_from.' till '.$sales_price_date_to.')</b>', $price );
but it doesn't display the sale date from.
So can someone please tell me how I display both the sale date from and sale date to?
Upvotes: 0
Views: 5239
Reputation: 398
I've made a plugin exactly for this (100% free, no ads and no hidden features).
The main difference from others on the store is that it also works with Variations (even if each variation has different dates).
https://wordpress.org/plugins/woo-on-sale-information/
Also, it was developed with simplicity in mind and with filters for developers to be able to tweak it without issues (the code is well documented also).
Give it a look ;)
Upvotes: 1
Reputation: 649
I have tried it on my site and it works for me.
add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ) {
$sales_price_from = get_post_meta( $product->id, '_sale_price_dates_from', true );
$sales_price_to = get_post_meta( $product->id, '_sale_price_dates_to', true );
if ( is_single() && $product->is_on_sale() && $sales_price_to != "" ) {
$sales_price_date_from = date( "j M y", $sales_price_from );
$sales_price_date_to = date( "j M y", $sales_price_to );
$price = str_replace( '</ins>', ' </ins> <b>(Offer from ' . $sales_price_date_from . ' till ' . $sales_price_date_to . ')</b>', $price );
}
return apply_filters( 'woocommerce_get_price', $price );
}
Upvotes: 4