this is my first question on Stack Overflow…
I am modifying a WooCommerce website for a client. What they want is for all items which have a sale prices to display a red dot next to the sale price.
I have located the public function ‘get_price_html’ in abstract-wc-product.php:
/**
* Returns the price in html format.
*
* @param string $deprecated Deprecated param.
*
* @return string
*/
public function get_price_html( $deprecated = '' ) {
if ( '' === $this->get_price() ) {
$price = apply_filters( 'woocommerce_empty_price_html', '', $this );
} elseif ( $this->is_on_sale() ) {
$price = wc_format_sale_price( wc_get_price_to_display( $this, array( 'price' => $this->get_regular_price() ) ), wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
} else {
$price = wc_price( wc_get_price_to_display( $this ) ) . $this->get_price_suffix();
}
return apply_filters( 'woocommerce_get_price_html', $price, $this );
}
I have figured out that if I modify the ‘elseif ( $this->is_on_sale()’ section, I can do what I want, and this works, but only when I modify and upload the core version of abstract-wc-product.php, which I don’t want to do.
I have child theme going, and in my child theme directory, I have copied abstract-wc-product.php into the following folder structure:
woocommerce/includes/abstracts
This isn’t working, so I have also tried to copy the function above into my functions.php file, to see if this overrides the core version.
Neither of these is working – can anybody help with how to do this, please?
Thank you!
Stuart
2
Answers
If you can’t use CSS, here’s how you do it:
This goes into your child theme
functions.php
.Your key is in
return apply_filters( 'woocommerce_get_price_html', $price, $this );
Check the doc here about how to use filters in WordPress: https://developer.wordpress.org/reference/functions/apply_filters/
In your case, I believe you can achieve your goal with this function below – not tested!!