Woocommerce php 代码 get_price_html() [英] Woocommerce php code get_price_html()
问题描述
我是 WordPress 和 WooCommerce 的新手,我相信我已经确定了产生我想要更改的输出的代码行.
I am new to WordPress and WooCommerce, I believe I have identified the line of code that is producing the output I want changed.
我正在使用 WooCommerce 免费的artificer 主题,并且 index.php 有一行:
I am using free artificer theme from WooCommerce and the index.php has a line:
<h3>
<?php the_title(); ?>
<span class="price">
<?php echo $_product->get_price_html(); ?>
</span>
</h3>
这会产生类似Black Stone - $43"(即产品标题 - 价格)
This produces something like "Black Stone - $43" (i.e. product title - price)
我想要类似黑石
$43"
(即产品标题
价格)
I want something like "Black Stone
$43"
(i.e. product title <br/>
price)
看起来 ``get_price_html()` 函数有一些过滤器,但文档不是很好,或者我只是不明白如何浏览它.
It looks like there are some filters for the ``get_price_html()` function, but the documentation is not very good or I just don't understand how to navigate through it.
任何方向将不胜感激.
谢谢.
Any direction would be appreciated.
Thanks.
推荐答案
所有$product->get_price_html();
产生这样的东西:
<del><span class="amount">£8.00</span>–<span class="amount">£9.00</span></del>
<ins><span class="amount">£7.00</span>–<span class="amount">£8.00</span></ins>
要操作这个数据,你必须从这个字符串中提取它
to manipulate this data, you must extract it from this string
如果您使用 WP 过滤器 - 您将在任何地方更改 get_price_html()
输出,如果您需要仅在一个地方更改 get_price_html()
输出,您应该做下一步:
If you use WP filters - you will change get_price_html()
output everywhere and if you need to change get_price_html()
output just in one place, you should do next:
global $product;
$price_html = $product->get_price_html();
$price_html_array = price_array($price_html);
function price_array($price){
$del = array('<span class="amount">', '</span>','<del>','<ins>');
$price = str_replace($del, '', $price);
$price = str_replace('</del>', '|', $price);
$price = str_replace('</ins>', '|', $price);
$price_arr = explode('|', $price);
$price_arr = array_filter($price_arr);
return $price_arr;
}
现在你在数组中有相同的数据
now you have same data in array
Array ( [0] => £8.00–£9.00 [1] => £7.00–£8.00 )
你可以用它做你想做的一切
and you can do with it everything you want
要应用全局过滤器,您必须添加
to apply global filter, you must add
add_filter( 'woocommerce_get_price_html', 'price_array', 100, 2 );
这篇关于Woocommerce php 代码 get_price_html()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!