Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Show discounted price when using coupon link on product page WordPress

  • SOLVED

Hi,

We make a lot a of use of coupons. You have this very nice feature that you can add a coupon in a link.

By adding /?coupon-code=nameofcoupon a coupon is automatically added.

However, the price is only discounted at the checkout page.

What i would like is that the coupon (when it is discounted on product not on cart) gets discounted on the product page.
At present the price is only discounted at the product page when there is a general discount set for the product via woocommerce - products.
However I want to give discount only to people that use the link with the coupon code and show this discount on the product page.

Thus, dream scenario. My product costs €100 and i have a coupon called sjfo93 that gives €20.
When I click on the link -/?coupon-code=sjfo93. I get directed to the product page and see the discounted price, which would be €80.

I am using the WPLMS theme, so I don't use product pages but I have course pages. However, I believe that the price on the course page is fetched from the product page.

I have added an image to clear it up. Hope it is clear enough. Would be great if you can assist me!

site url: soofos.nl

Answers (2)

2017-05-04

Reigel Gallarde answers:

You may try my solution found here:
http://reigelgallarde.me/programming/show-discounted-price-product-page-using-coupons/

Let me know if works for you. This should work on product page, but may also work on WPLMS.
I'm not sure how WPLMS is showing the price.


Joris van den Berg comments:

Hi Reigel,

Thanks for responding! I tried the code, unfortunately it does not work. When I add it to functions.php the course page (also product page) are not fully shown any more they are cut of from the point that the price should be shown.

Also when I am in the backend I can only see 1 product in woocommerce - products.

Any idea what may be causing this?

Kind regards


Reigel Gallarde comments:

did you copy the whole thing?

you should just copy right after the first line where it says "<?php // <-- be careful when copy pasting.."


Joris van den Berg comments:

Yes I did. Made sure entire functions.php is in one php tag


Reigel Gallarde comments:

can you add the code now, and also, can you give me a coupon to test? I want to see it to debug further.


Joris van den Berg comments:

Offcourse! Code is added, I added it to our test server. You can check it here:

product page: http://testend.nl/demo1/product/photoshop/
course page: http://testend.nl/demo1/cursus/online-cursus-photoshop/

coupon: "testcoupon" (should give a product discount of €20 on all products)


Reigel Gallarde comments:

Weird. I have a friend who has this theme. We tried the code and it works.
I'm not really sure why it's not working on your site.


Joris van den Berg comments:

Hmm that is strange indeed. Could be a third party plugin conflict.. I will try at the live domain, if it works there


Reigel Gallarde comments:

I think your woocommerce is not the latest?


Joris van den Berg comments:

We run 2.6.4, could that be too outdated?


Reigel Gallarde comments:

That's it. hmm... My code uses WooCommerce 3.0.4

In that case, can you try replace:
$price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $undiscounted_price ) ), $_price ) . $product->get_price_suffix();

with:
$price = $_price;

see if that works.


Reigel Gallarde comments:

I updated my code snippets: http://reigelgallarde.me/programming/show-discounted-price-product-page-using-coupons/

You can use the second one.


Joris van den Berg comments:

OK nice, I tried and this was indeed the issue.

However, there are still a few things that are missing

- notation changes from "€99 (incl BTW)" to just the new amount "79". It should be <strike>€99</strike> €79 (incl BTW). See image. I guess this should be doable to edit

- bigger issue, what i am really looking for is that the coupon is only applicable to selected products. However when I select only one product to be applicable the coupon does not work anymore. This is because this option in the coupon settings looks whether the product is added to cart. As it is not added to cart this does not work. Any idea on how to fix this?


Reigel Gallarde comments:

did you use the "$price = $_price;" or my updated snippets? If you're using my updated snippets, the "notation" shouldn't be a problem.

On your bigger issue, yes that's kind of a problem. I'll try to see if we got a way around it.


Reigel Gallarde comments:

I'm not sure if we have a way around your bigger issue. I don't even think that /?coupon-code=nameofcoupon works on that.


Joris van den Berg comments:

Yes i am afraid so.

I figured this. If you add in the url ?add-to-cart=productid&coupon-code=nameofcoupon. It actually works. I thought it may add the product two times to the cart but this actually does not seem to happen, so quite nice solution i guess.

However, two issues remain:
- in the backend product > products is still messed up, only shows one item
- the notation is not quite right, it only shows the disount price not the original price striked through

I used the updated snippet on your site!

BTW thanks for the great help so far, absolutely amazing!!


Reigel Gallarde comments:

if my code is missing up the backend, you can change

this line:
if ( WC()->cart->has_discount() ) {

to:
if ( ! is_admin() && WC()->cart->has_discount() ) {

about the notation, I'm not so sure what you mean. Maybe provide a screenshot. Sorry, I really don't have setup for WooCommerce 2.6.x. And downgrading from 3.0 is not an option. The latest WooCommerce uses different database format.


Joris van den Berg comments:

Great that works!

I mean that the regular price disappears when the discounted price is shown. I would like to show the regular price with a strike through.

Hope the screenshot clears it up!


Reigel Gallarde comments:

using my second code snippets as reference,
please remove line 38 and line 40 - 48.
I think that should do it.


Reigel Gallarde comments:

line 38 and line3 43 - 48 I mean..


Reigel Gallarde comments:

line 38 and lines 43 - 48 I mean..


Joris van den Berg comments:

Ok Reigel, good news!

It FREAKING WORKS! I just implemented it at our live domain and it looks like it is working as a charm.

Thanks a million for the great help and quick feedback!

Really appreciate it, all thumbs up!

2017-05-03

Nathant Thoe answers:

I too want to know, sorry!