Woocommerce php 代码 get_price_html()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19346067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:19:51  来源:igfitidea点击:

Woocommerce php code get_price_html()

phpwordpresswoocommerce

提问by user2612580

I am new to WordPress and WooCommerce, I believe I have identified the line of code that is producing the output I want changed.

我是 WordPress 和 WooCommerce 的新手,我相信我已经确定了产生我想要更改的输出的代码行。

I am using free artificertheme from WooCommerce and the index.php has a line:

我正在使用来自 WooCommerce 的免费artificer主题,并且 index.php 有一行:

<h3>
    <?php the_title(); ?>
    <span class="price">
        <?php echo $_product->get_price_html(); ?>
    </span>
</h3>

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)

我想要“Black Stone
$43”之类的东西
(即产品标题<br/>价格

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.

看起来“get_price_html()”函数有一些过滤器,但文档不是很好,或者我只是不明白如何浏览它。

Any direction would be appreciated.
Thanks.

任何方向将不胜感激。
谢谢。

回答by Alexander Ivashchenko

all the $product->get_price_html();produces something like this:

所有的$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

要操作此数据,您必须从该字符串中提取它

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:

如果您使用 WP 过滤器 - 您将get_price_html()在任何地方更改输出,如果您只需要get_price_html()在一个地方更改输出,您应该执行以下操作:

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 );

回答by Alex

This is probably the filter you're looking for:

这可能是您正在寻找的过滤器:

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
    $price = '';
    $price .= woocommerce_price($product->min_variation_price);
    return $price;
}

This just changes it so that the min price is displayed (and nothing else) as it's not clear how you want to format/style it. You can access various other details via the $product object to customise the output. Use it within your functions.php file.

这只是改变它,以便显示最低价格(而不是其他任何东西),因为不清楚您想如何对其进行格式化/样式设置。您可以通过 $product 对象访问各种其他详细信息以自定义输出。在您的functions.php 文件中使用它。

回答by Raunak Gupta

You can achieve this by modifying the said line and adding a small css code in theme custom.cssfile.

您可以通过修改上述行并在主题custom.css文件中添加一个小的 css 代码来实现这一点。

Replace the given code by this:

用这个替换给定的代码:

<h3>
    <?php the_title(); ?>
    <br/>
    <span class="price">
        <?php echo $_product->get_price_html(); ?>
    </span>
</h3>

And add the following css code either in custom.css(recommended) file or in last line of theme style.cssfile.

并在custom.css(推荐)文件或主题style.css文件的最后一行添加以下 css 代码。

ul.featured-products li h3 .price::before{
    content : '' !important;
}

Please Note: The above code is tested and working fine with Artificer version 1.3.16 (which was released on 05 May, 2016.)

请注意:以上代码已在 Artificer 1.3.16 版(2016 年 5 月 5 日发布)上测试并正常工作。

Hope this helps!

希望这可以帮助!