例如文件plugins/woocommerce/includes/abstracts/abstract-wc-product.php
中的函数get_price_html_from_to()
public function get_price_html_from_to( $from, $to ) {
return '<del>' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</ins>';
}
在不进入文件本身并对其进行更改的情况下,如何更改该字符串?
是否可以创建我自己的abstract-wc-product.php
文件,并将wordpress/woo指向该文件而不是原始文件?或者有没有一种方法可以简单地去除&用我自己的功能替换这个功能?或者只是在使用之前更改它的字符串?
我认为实现这一点的一种方法是,在包含abstract-wc-product.php
文件之前,先声明新的函数get_price_html_from_to( $from, $to )
然后进入abstract-wc-product.php
文件,在函数的get_price_html_from_to( $from, $to )
定义附近添加以下代码:
if ( ! function_exists( 'get_price_html_from_to' ) )
public function get_price_html_from_to( $from, $to ) {
return '<del>' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</ins>';
}
}
尽管这需要你进入文件并对其进行实际更改。不过,确实有点。
不要更改模板,因为一旦有更新,您将丢失更改的模板。相反,将您的方法添加到过滤器中:
dd_filter( 'woocommerce_get_price_html', 'your_function_name', 100, 2 );
function your_function_name( $price, $product ){
return 'Was:' . str_replace( '<ins>', ' Now:<ins>', $price );
}