ustation3030
ustation3030

Reputation: 23

How can I modify get_price_html to create custom price display for on sale items

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

Upvotes: 2

Views: 4006

Answers (2)

Tami
Tami

Reputation: 744

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!!

 function custom_sale_price( $price, $product ){
    
        if( $product->is_on_sale() ){
             
             return '<span class="red-dot"></span>' . $price; 
         }
  
         return $price;  
}

add_filter('woocommerce_get_price_html', 'custom_sale_price', 10, 2 );

Upvotes: 1

disinfor
disinfor

Reputation: 11558

If you can't use CSS, here's how you do it:

/* Here's the woocommerce filter. Then we use a static function (anonymous function) to pass the two arguments that the original filter passes.*/
add_filter( 'woocommerce_get_price_html', static function( $price, $product ) {
    // Now we check to see if the product is onsale.
    if ( $product->is_on_sale() ) :
        // Change this to whatever worked for you when you modified core files.
        $price = wc_format_sale_price( wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ), wc_get_price_to_display( $product ) ) . $product->get_price_suffix();
    endif;

// Return the $price. This is here in the event the product that is passed isn't on sale. 
return $price;

}, 10, 2 );

This goes into your child theme functions.php.

Upvotes: 2

Related Questions