php - Display text of price (minus 20%) on every product page in a sentence.

admin2025-01-08  3

We have loads of products on our woo commerce and are trying to automate a few things.

All our products are 20% off if you use our specific promo code.

So at the top of every product page there is a sentence that says "Get this product for (price -20%) with (Promo code)

I would like to create a function that gets the product's price and subtracts 20% off it. Displaying in a sentence where we can update the promo code by updating one function.

Any ideas on the best way to achieve this? I dont wana use any plugins.

Thanks for any help!

We have loads of products on our woo commerce and are trying to automate a few things.

All our products are 20% off if you use our specific promo code.

So at the top of every product page there is a sentence that says "Get this product for (price -20%) with (Promo code)

I would like to create a function that gets the product's price and subtracts 20% off it. Displaying in a sentence where we can update the promo code by updating one function.

Any ideas on the best way to achieve this? I dont wana use any plugins.

Thanks for any help!

Share Improve this question asked Mar 14, 2017 at 4:27 PatrickPatrick 2953 gold badges10 silver badges26 bronze badges 1
  • Don't forget to comment further if you still have issues or doubts, or tick an answer as correct (if it helped solving your problems or doubts). – The J Commented Mar 20, 2017 at 4:24
Add a comment  | 

2 Answers 2

Reset to default 0

Assuming you would put this in a woocommerce template file, wherever you want it to display:

<?php

    global $product;
    $regular_price = esc_attr( $product->get_display_price() );
    $discount = 20;
    $currency = esc_attr( get_woocommerce_currency() );
    $new_price = ($regular_price * $discount) / 100;

    echo 'Get this product for '.$new_price.$currency.' with (Promo code)';

?>

Untested, but should do the trick.

Depending on what you take the percent from (before tax, after tax, etc) and

you'll want this code:

<?php
$reg_price = esc_attr( $product->get_display_price() );
$sale_price = .8 * $reg_price;

$echo 'On Sale now for ' . $sale_price . ' with promo code!';

?>

put it in your product single page where you want it to go

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266647a1137.html

最新回复(0)