How to Display Sale Price End Date in Woocommerce

How to Display Sale Price End Date in woocommerce
999 Views
3.8
(659)

Welcome to our comprehensive guide on displaying the sale price end date in WooCommerce! WooCommerce is a popular e-commerce platform built on WordPress, offering a range of powerful features for online store owners. By showcasing the sale price end date, you can create a sense of urgency and encourage customers to make a purchase before the offer expires.

In this guide, we will walk you through the steps to add the sale price end date field to your products and modify the necessary template files to display this information on your website.

Whether you’re a beginner or an experienced WooCommerce user, our instructions will help you implement this feature seamlessly.

By following our step-by-step instructions and using the provided code snippets, you’ll be able to enhance your customer’s shopping experience by showing them the remaining time for special discounts.

Define woo commerce Sale Price End Date

Make a Single Product Page Showing the Sale Price End Date

<?php
add_action( 'woocommerce_single_product_summary', 'cxc_display_single_sale_price_after_add_to_cart', 40 );
function cxc_display_single_sale_price_after_add_to_cart() {
	global $product;
	if ( is_object( $product ) && $product->is_on_sale() ) {
		$sale_price_end_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
		if ( ! empty( $sale_price_end_date ) ) {
			$date_format = apply_filters( 'woocommerce_sale_price_end_date_format', get_option( 'date_format' ) );
			echo '<div class="cxc-sale-end-date">' . sprintf( __( 'Sale Ends On %s', 'woocommerce' ), date_i18n( $date_format, strtotime( $sale_price_end_date ) ) ) . '</div>';
		}
	}
}
?>

Also read: How to Remove Pagination on WooCommerce shop page

Display the sale price and the end date below the price label

<?php
add_action( 'woocommerce_single_product_summary', 'cxc_display_single_sale_price_after_sale_price', 15 );
function cxc_display_single_sale_price_after_sale_price() {
	global $product;
	if ( is_object( $product ) && $product->is_on_sale() ) {
		$sale_price_end_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
		if ( ! empty( $sale_price_end_date ) ) {
			$date_format = apply_filters( 'woocommerce_sale_price_end_date_format', get_option( 'date_format' ) );
			echo '<div class="cxc-sale-end-date">' . sprintf( __( 'Sale Ends On %s', 'woocommerce' ), date_i18n( $date_format, strtotime( $sale_price_end_date ) ) ) . '</div>';
		}
	}
}
?>

Show Sale Price End Date After Shopping Loop Item

<?php
add_action( 'woocommerce_after_shop_loop_item_title', 'cxc_display_sale_price_shop_loop_item', 15 );
function cxc_display_sale_price_shop_loop_item() {
	global $product;
	if ( is_object( $product ) && $product->is_on_sale() ) {
		$sale_end_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
		if ( ! empty( $sale_end_date ) ) {
			$date_format = apply_filters( 'woocommerce_sale_price_date_format', get_option( 'date_format' ) );
			echo '<p class="sale-end-date">' . sprintf( __( 'Sale Ends On %s', 'woocommerce' ), date_i18n( $date_format, strtotime( $sale_end_date ) ) ) . '</p>';
		}
	}
}
?>

Also Read: How to change title and sub-title related products in WooCommerce

Show Sale Price End Date Following Shop Loop Item and Single Product Page

<?php
add_action( 'woocommerce_single_product_summary', 'cxc_display_show_both_sale_price_end_date', 15 );
add_action( 'woocommerce_after_shop_loop_item_title', 'cxc_display_show_both_sale_price_end_date', 15 );
function cxc_display_show_both_sale_price_end_date() {
	global $product;
	if ( is_object( $product ) && $product->is_on_sale() ) {
		$sale_end_date = get_post_meta( $product->get_id(), '_sale_price_dates_to', true );
		if ( ! empty( $sale_end_date ) ) {
			$date_format = apply_filters( 'woocommerce_sale_price_date_format', get_option( 'date_format' ) );
			echo '<p class="sale-end-date">' . sprintf( __( 'Sale Ends On %s', 'woocommerce' ), date_i18n( $date_format, strtotime( $sale_end_date ) ) ) . '</p>';
		}
	}
}
?>

Conclusion

Congratulations! You have successfully learned how to display the sale price end date in WooCommerce. By following the steps outlined in this guide, you have empowered your online store with a valuable feature that adds a sense of urgency to your sales promotions.

By adding the sale price end date field to your products and modifying the necessary template files, you can effectively communicate the remaining time for discounted prices to your customers. This helps create a compelling reason for them to take action and make a purchase before the offer expires.

Best of luck with your online business endeavors, and may your sales promotions thrive with the added value of displaying the sale price end date in WooCommerce!

How useful was this blog?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 659

No votes so far! Be the first to rate this blog.

By Subhash Katakiya

Hey Guys, My Self Subhash Katakiya. I am a resident of Gujarat. I have 2+ years of experience in full-stack web development. Currently, I am working in CodexCoach Company. In particular, I specialize in WooCommerce, plugins, and themes. So, read my blogs and learn full-stack web development skills with my guide.

Leave a comment

Your email address will not be published. Required fields are marked *