[英]Display a custom product price in woocommerce archive page and for upsells/related products
Is there a way to enable a backend field in the WordPress dashboard to show a custom price per product in the archive page and for upsells/related products but leave the price in product-summary? 有没有一种方法可以使WordPress仪表板中的后端字段在存档页面和加售/相关产品中显示每个产品的自定义价格,但将价格保留在产品摘要中?
Example: Product A has a price of 10€ but I would like to show "from 6€/kg" instead. 示例:产品A的价格为10欧元,但我想显示“起价为6欧元/千克”。
The easiest way would be to use a custom field which is overriding the woocommerce_template_loop_price
but this code doesn't work but I don't understand why. 最简单的方法是使用覆盖
woocommerce_template_loop_price
的自定义字段,但是此代码不起作用,但我不明白为什么。
add_action('woocommerce_template_loop_price', 'shopprice_change', 10, 2);
function shopprice_change ($price, $product) {
global $post, $blog_id;
$post_id = $post->ID;
$price = get_post_meta($post_id, 'shoppricechange', true);
return $price;
wp_reset_query();
}
UPDATE UPDATE
I've found a solution for changing the price in archive page without changing on single product page: 我找到了一种解决方案,可以在不更改单个产品页面的情况下更改存档页面中的价格:
function cw_change_product_html( $price_html, $product ) {
$unit_price = get_post_meta( $product->id, 'shoppricechange', true );
if (is_product()) return $price_html;
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
Problem: Actually the upsells and related products should change as well on the single product page. 问题:实际上,加售商品和相关产品也应该在单个产品页面上进行更改。 But with
if (is_product()) return $price_html;
但是
if (is_product()) return $price_html;
its excluding them as well. 也将它们排除在外。 Who helps solving this will get the bounty.
谁能解决这个问题将得到赏金。 Thank you!
谢谢!
But with
if (is_product()) return $price_html;
但是
if (is_product()) return $price_html;
its excluding them as well.也将它们排除在外。
Using this in place of the is_product()
works well for me: 用它代替
is_product()
对我来说很好:
is_single( $product->get_id() )
And you shouldn't call $product->id
directly. 而且您不应该直接调用
$product->id
。 Instead, use $product->get_id()
. 而是使用
$product->get_id()
。
Here's the full code I used: 这是我使用的完整代码:
function cw_change_product_html( $price_html, $product ) {
if ( is_single( $product->get_id() ) )
return $price_html;
$unit_price = get_post_meta( $product->get_id(), 'shoppricechange', true );
if ( ! empty( $unit_price ) ) {
$price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';
}
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
woocommerce_template_loop_price
is not a action it's a function woocommerce_template_loop_price
不是动作,而是函数
Use the code below 使用下面的代码
function cw_change_product_html( $price_html, $product ) { $unit_price = get_post_meta( $product->id, 'shoppricechange', true ); $returnedSignleProdctP =false; $trace = debug_backtrace(); $callstack = (array) $trace; foreach ($callstack as $key => $value) { if(isset($value['function']) && $value['function'] == 'woocommerce_output_related_products' ){ if ( ! empty( $unit_price ) ) { $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>'; } } if(isset($value['function']) && $value['function'] == 'woocommerce_template_single_price' ){ $price_html = $price_html; } if(isset($value['function']) && $value['function'] == 'woocommerce_template_loop_price' ){ if ( ! empty( $unit_price ) ) { $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>'; } } } return $price_html; } add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 );
Woocommerce Extra Price Fields (也许是这个插件可以提供帮助)(来源: https : //wordpress.org/plugins/woocommerce-extra-price-fields/ )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.