Show discounted price on product page when using coupons

If you’re looking for a way to show discounted price on product page of WooCommerce, then this is just the thing.
Using the below codes will show the discounted price on product page if a customer uses a coupon.
Just paste this php code in your functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php // <-- be careful when copy pasting.. you might NOT NEED TO COPY this line
add_filter('woocommerce_get_price_html', 'woocommerce_get_price_html', 10, 2 );
function woocommerce_get_price_html( $price, $product ) {
 
	if ( WC()->cart->has_discount() ) {
 
		$values = array (
			'data'		=> $product,
			'quantity'	=> 1
		);
 
		$coupons = WC()->cart->get_coupons();
 
		$_price = $product->get_price();
		$undiscounted_price = $_price;
 
		if ( ! empty( $coupons ) ) {
 
			foreach ( $coupons as $code => $coupon ) {
 
				if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) {
					$discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $_price : $undiscounted_price, $values, true );
					$discount_amount = min( $_price, $discount_amount );
					$_price          = max( $_price - $discount_amount, 0 );
				}
 
				if ( 0 >= $_price ) {
					break;
				}
			}
			if ( ( $product->get_price() > 0 ) && ( $undiscounted_price !== $_price ) )
				$price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $undiscounted_price ) ), $_price ) . $product->get_price_suffix();
		}
 
	}
 
	return $price;
}

Above code works with WooCommerce 3.x. If your using lower versions, below code works for WooCommerce 2.6.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php // <-- be careful when copy pasting.. you might NOT NEED TO COPY this line
add_filter('woocommerce_get_price_html', 'woocommerce_get_price_html', 10, 2 );
function woocommerce_get_price_html( $price, $product ) {
 
	if ( WC()->cart->has_discount() ) {
 
		$values = array (
			'data'		=> $product,
			'quantity'	=> 1
		);
 
		$coupons = WC()->cart->get_coupons();
 
		$_price = $product->get_price();
		$undiscounted_price = $_price;
 
		if ( ! empty( $coupons ) ) {
 
			foreach ( $coupons as $code => $coupon ) {
 
				if ( $coupon->is_valid() && ( $coupon->is_valid_for_product( $product, $values ) || $coupon->is_valid_for_cart() ) ) {
					$discount_amount = $coupon->get_discount_amount( 'yes' === get_option( 'woocommerce_calc_discounts_sequentially', 'no' ) ? $_price : $undiscounted_price, $values, true );
					$discount_amount = min( $_price, $discount_amount );
					$_price          = max( $_price - $discount_amount, 0 );
				}
 
				if ( 0 >= $_price ) {
					break;
				}
			}
 
			$display_price         = $product->get_display_price( $_price );
			$display_regular_price = $product->get_display_price( $product->get_regular_price() );
 
			if ( ( $product->get_price() > 0 ) && ( $display_regular_price !== $display_price ) ) {
 
				$price = $product->get_price_html_from_to( $display_regular_price, $display_price ) . $product->get_price_suffix();
 
			}
		}
 
	}
 
	return $price;
}

You may also like...

2 Responses

  1. Joris says:

    Hi Reigel,

    Thanks for the great help already. Great functionality. I edited the snippet as instructed here: https://wpquestions.com/Show_discounted_price_when_using_coupon_link_on_product_page/27438.

    There is one bug i just found. After you have added a coupon via ?coupon-code all prices on the site are shown regular price regular price (thus €99 €99. This should off course only be €99.

    How to fix this?

    Thanks in advance and cheers!

    • Reigel Gallarde says:

      Hi Joris,

      I made changes to the whole woocommerce_get_price_html function. The second code snippets. Please copy and replace yours. See if that fixes it.

Leave a Reply

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