WooCommerce:如何编辑get_price_html
2 个回答
- 投票数
-
- 2013-01-27
绝对不要直接编辑核心文件和插件文件,因为任何更新都可能会覆盖您的更改.如果您通过
get_price_html
方法在WooCommerce源代码中查找,则可以使用许多过滤器修改函数的输出.有关在
add_filter
. http://codex.wordpress.org/Function_Reference/apply_filters">apply_filters
调用.来自
get_price_html
中的get_price_html
:return apply_filters('woocommerce_get_price_html', $price, $this);
因此要添加自己的过滤器:
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 ); }
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 toapply_filters
calls.From
get_price_html
inclass-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 ); }
-
谢谢您的回答,顺便说一句为什么当我删除main函数的内容时,它仍会正常返回输出Thanks for the answer, by the way why when I delete the contents of the main function it still returns the output as normal
- 0
- 2013-01-27
- Lucky Luke
-
因此,可以说如果有一笔交易,并且退回了我
£2£1 `,我如何将其更改为`Was:£2现在:带有过滤器的£1 `?So lets say if there was a sale on and it returns me `£2£1`, how can i change that into `Was:£2Now:£1` with a filter?- 1
- 2013-01-27
- Lucky Luke
-
不确定,不太熟悉WooCommerce,也许另一个类可以扩展它.请参阅上方的第二个问题.not sure, not too familiar with WooCommerce, perhaps another class extends it. see edit above for your second question.
- 0
- 2013-01-27
- Milo
-
Brill,;),有很大帮助Brill, ;), great help
- 0
- 2013-01-27
- Lucky Luke
-
我想知道默认$ woo的woocommerce_get_price_html过滤器中发生了什么.在我的网站上,woocommerce显示免费产品为0 $,而不是"免费!".I am trying to know that what happening in default `woocommerce_get_price_html` filter for `$price`. In my site,woocommerce shows 0$ for free products instead `Free!`
- 0
- 2016-12-07
- SKMohammadi
-
哪个文件具有该功能?我找不到文件.谢谢Which file has that function? I can't find the file. Thanks
- 0
- 2020-06-19
- Si8
-
- 2014-01-03
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>'; } }
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>'; } }
-
即使您的代码可以工作(并且我也没有理由认为它不可行),这是一个Q/A站点,而不是代码存储库,因此最好共享专业知识和知识来解释您的代码,而不是只编写没有解释的代码也没有内联评论...Even if your code can work (and I have no reason to think it doesn't) this is a Q/A site, not a code repository so it's better share expertice and knowledge explaining your code, instead of just write code with no explaination nor inline comments...
- 6
- 2014-01-03
- gmazzap
-
该代码还使用了不好的对象属性.the code also uses object properties which is not good.
- 0
- 2018-05-08
- Svetoslav Marinov
我正在尝试编辑单个产品的价格值.
在
single-product/price.php
中,有一个模板调用$product->get_price_html
.如何编辑该函数/方法以更改HTML的显示方式?此刻,即使我删除了位于
class-wc-product
中的函数的所有内容,它仍然奇迹般地显示吗?有人知道为什么吗?