[英]Show total price for grouped product on shop page in Woocommerce
I want to show the total summed price of my grouped products instead of the price range.我想显示分组产品的总价格,而不是价格范围。 I already fixed this on the product page with this snippet.
我已经使用此代码段在产品页面上修复了此问题。
How can I use this code or fix my issue on the shop page?如何在商店页面上使用此代码或解决我的问题?
global $product;
$price = $product->get_price_html();
if ( $product->get_type() == 'grouped') {
$children = $product->get_children();
$price = 0;
foreach ($children as $key => $value) {
$_product = wc_get_product( $value );
$price += $_product->get_price();
}
$price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
}
?>
<p class="price"><?php echo $price; ?></p>
Try to use global filter to change the price returned in shop loop: 尝试使用全局过滤器来更改商店循环中返回的价格:
add_filter( 'woocommerce_get_price_html', 'highest_price', 10, 2 );
function highest_price( $price, $product ) {
if ( $product->get_type() == 'grouped') {
$children = $product->get_children();
$price = 0;
foreach ($children as $key => $value) {
$_product = wc_get_product( $value );
$price += $_product->get_price();
}
$price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
}
return $price;
}
I can not test this solution, but if it's not working, try to add filter to another function, not woocommerce_get_price_html, for example woocommerce_template_loop_price 我无法测试此解决方案,但如果它不起作用,请尝试将过滤器添加到另一个函数,而不是woocommerce_get_price_html,例如woocommerce_template_loop_price
The answer of Alexander use the right hook and works. 亚历山大的答案使用正确的钩子并起作用。 I am just revising a bit your code in a cleaner way, using for example Woocommerce formatting function
wc_price()
. 我只是以更干净的方式修改你的代码,使用例如Woocommerce格式化函数
wc_price()
。 You will have to remove your own code from single product pages, as this code works everywhere (on single product pages and archives pages too: 您必须从单个产品页面中删除自己的代码 , 因为此代码适用于所有地方 (在单个产品页面和存档页面上也是如此:
add_filter( 'woocommerce_get_price_html', 'grouped_product_price', 10, 2 );
function grouped_product_price( $price, $product ) {
if ( $product->is_type( 'grouped' ) && ! is_admin() ) {
$grouped_price = 0;
foreach ($product->get_children() as $child_id ) {
$child = wc_get_product( $child_id );
$grouped_price += $child->get_price();
}
$price = wc_price($grouped_price);
}
return $price;
}
Code goes in function.php file of your active child theme (or theme). 代码位于活动子主题(或主题)的function.php文件中。
Tested and works. 经过测试和工作。
I tested the following solution and it works on Woocommerce 3.6.3. 我测试了以下解决方案,它适用于Woocommerce 3.6.3。 After this the group product displays a price which is the sum total of all its child products.
在此之后,组产品显示的价格是其所有子产品的总和。
function max_grouped_price( $price_this_get_price_suffix, $instance, $child_prices ) {
return wc_price(array_sum($child_prices));
}
add_filter( 'woocommerce_grouped_price_html', 'max_grouped_price', 10, 3 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.