简体   繁体   中英

Overriding public functions in jigoshop

I need to override the public function that returns the discount percentage in jigoshop

/**
 * Returns the products sale value, either with or without a percentage
 *
 * @return string HTML price of product (with sales)
 */
public function get_calculated_sale_price_html()
{
    if ($this->is_on_sale()) {
        if (strstr($this->sale_price, '%')) {
            return '<del>'.jigoshop_price($this->regular_price).'</del>
                <ins>'.jigoshop_price($this->get_price()).'</ins><br/>
                <span class="discount">'.sprintf(__('%s off!', 'jigoshop'), $this->sale_price).'</span>';
        } else {
            return '<del>'.jigoshop_price($this->regular_price).'</del>
                <ins>'.jigoshop_price($this->sale_price).'</ins>';
        }
    }

    return '';
}

I tried with

if (!function_exists('calculated_price')) {
    function calculated_price(){
        if ($this->is_on_sale()) {
            if (strstr($this->sale_price, '%')) {
                return '<del>'.jigoshop_price($this->regular_price).'</del>
                    <ins>'.jigoshop_price($this->get_price()).'</ins><br/>
                    <span class="discount">'.$this->sale_price.'</span>';
            } else {
                return '<del>'.jigoshop_price($this->regular_price).'</del>
                    <ins>'.jigoshop_price($this->sale_price).'</ins>';
            }
        }

        return '';
        }
}

add_action('get_calculated_sale_price_html', 'calculated_price');

And I tried with add_filter() but no luck.

Is it possible to modify the existing public function?

For adding your new function, I assume you have a main class that contains your function, so, add a new class that extends that,

class myclass extends already_externs_class {
        function calculated_price(){
        }
}

In this case, you have to instantiate from new class that you've written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM