skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search