WooCommerce: How to edit the get_price_html

admin2025-01-08  4

I am trying to edit the price value for a single product.

In single-product/price.php there is a template call to $product->get_price_html. How can I edit that function/method to change the way the HTML is presented?

At the moment even if I delete all the contents of the function located in class-wc-product it still miraculously displays? Anyone know why?

I am trying to edit the price value for a single product.

In single-product/price.php there is a template call to $product->get_price_html. How can I edit that function/method to change the way the HTML is presented?

At the moment even if I delete all the contents of the function located in class-wc-product it still miraculously displays? Anyone know why?

Share Improve this question edited Sep 17, 2016 at 4:50 Ethan Rævan 4,0295 gold badges27 silver badges55 bronze badges asked Jan 27, 2013 at 19:15 Lucky LukeLucky Luke 5052 gold badges6 silver badges18 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 26

Core and plugin files should never be edited directly, as any updates could overwrite your changes. If you look in WooCommerce source at the get_price_html method, there are a number of filters available to modify the output of the function.

See add_filter in Codex for more info on adding filters to apply_filters calls.

From get_price_html in class-wc-product:

return apply_filters('woocommerce_get_price_html', $price, $this);

So to add your own filter:

add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
    return 'Was:' . str_replace( '<ins>', ' Now:<ins>', $price );
}
function wpa83368_price_html( $price,$product ){
   // return $product->price;
    if ( $product->price > 0 ) {
      if ( $product->price && isset( $product->regular_price ) ) {
        $from = $product->regular_price;
        $to = $product->price;
        return '<div class="old-colt"><del>'. ( ( is_numeric( $from ) ) ? woocommerce_price( $from ) : $from ) .' Retail </del>  | </div><div class="live-colst">'.( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) .'Our Price</div>';
      } else {
        $to = $product->price;
        return '<div class="live-colst">' . ( ( is_numeric( $to ) ) ? woocommerce_price( $to ) : $to ) . 'Our Price</div>';
      }
   } else {
     return '<div class="live-colst">0 Our Price</div>';
   }
}

in the functions.php

function h08831n_get_price($product){
        $price_html = '<div class="product-price">';
        if ( $product->get_price() > 0 ) {
            if ($product->get_price() && $product->get_regular_price()) {
                $from = $product->get_regular_price();
                $to = $product->get_price();
                $price_html .= '<del>'. ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) .'</del><ins>'.( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) .'</ins>';
            }else{
                $to = $product->get_price();
                $price_html .= '<ins>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</ins>';
            }
        }else{
            $price_html .= '<div class="free">free</div>';
        }
        $price_html .= '</div>';
        return $price_html;
    }

in the where you want to display the price like single-product.php

global $product;
echo h08831n_get_price($product);

you can also remove wc_price in the function to remove woocommerce default HTML formats

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

最新回复(0)