0

I using "Display the discounted percentage near sale price in Single product pages for WC 3.0+" (Original answer code)

I used this code to show sale percentage after price on single product page. but I want to customize it - to give it background, paddings, etc...

So I need to include CSS class name in that function to be able to customize it via CSS

I want to add it only to "-percentage" (I don't want it to be applied neither to discounted or regular price)

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
Gelebrin
  • 29
  • 9
  • 1
    Do you mean? ``. Your question is not entirely clear to me, can you adjust it with more details? – 7uc1f3r Jun 12 '20 at 18:54
  • I used this code to show sale percentage after price on single product page. but I want to customize it - to give it background, paddings and etc. So I need to include CSS class name in that function("myclass") to be able to customize it via css – Gelebrin Jun 12 '20 at 19:08
  • 1
    yes, you already mentioned that in your question. I asked for more and clearer details. The function provides a certain output, on which element do you want to add the class? Can I assume that what I just mentioned as an example has solved your question? – 7uc1f3r Jun 12 '20 at 19:10
  • I want it to apply both "-" and price, not only price – Gelebrin Jun 12 '20 at 19:52

1 Answers1

2

Some additional information

So you get

function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = '<span class="my-class">' . __('-', 'woocommerce' ) . $percentage . '</span>';
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del>
    <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';

    return $price;
}
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Hi again, Sorry for bothering... I want this function to be applied only on single product page.(now it applies to shop page too) how to do that? – Gelebrin Jun 13 '20 at 06:41
  • 1
    Add a if statement with a [Conditional Tag](https://docs.woocommerce.com/document/conditional-tags/) - `is_product()` Returns true on a single product page. See how this is applied in [this answer](https://stackoverflow.com/a/61753313/11987538) – 7uc1f3r Jun 13 '20 at 06:43
  • Hi :) I encountered problem with variable product. If product is variable it shows NAN% do you know how to solve it? Thannk you – Gelebrin Jun 23 '20 at 17:23
  • 1
    I just retested this code, this code still works for both single and variable products without any problems. I think the problem is elsewhere, you will have to debug in your own setup to fix the problem – 7uc1f3r Jun 23 '20 at 17:39