【发布时间】:2020-06-18 17:46:38
【问题描述】:
我正在尝试显示 X-X 的变化自定义价格跨度。
- 最低价格来自(多个)自定义字段值,我需要使用最低值。
- 最高价格应该是变化最大价格。
如果变体具有 bulk_price 值,我只想要这个,并且只在档案页面中显示它。我需要获取自定义字段值和最高价格。
我的工作地点:
“How can I get Min and Max price of a woocommerce variable product in Custom loop?”
和
“WooCommerce: Get custom field from product variations and display it as a suffix to variation prices”
这就是我所拥有的:
function change_product_price_display( $price) {
$bulk_price = get_post_meta([ 'variation_id' ], 'bulk_price', true);
$priceMax = $product->get_variation_price('max'); // Max price
//only show in archives
if (is_product_category()) {
//only if there is a bulk price
if ( $bulk_price ) {
return ' <span class="price-suffix">' . ' From ' . get_woocommerce_currency_symbol() .__( $bulk_price , "woocommerce") . ' - ' . $priceMax . '</span>';
}
}
//don't affect other products
else {
return $price;
}
}
add_filter( 'woocommerce_get_price_html', 'change_product_price_display');
add_filter( 'woocommerce_cart_item_price', 'change_product_price_display');
【问题讨论】:
-
在我回答你的问题之前。将完整代码放在此处以创建字段并在其上构建可能更方便获得完整概述。现在将用于自定义字段的值(来自您之前的问题/答案)与来自此问题的值进行比较变得有点困惑,因为您显然已重命名它?
$add_field与$bulk_price. -
我已经更新了代码。感谢您的回答。
-
好的,谢谢。因此,为了清楚起见,您只想显示该特定变量产品的所有自定义字段的最低值?例如,您有 3 个自定义字段 10、20 和 30,然后显示从 10 到最高价格?
-
是的,从最低价到最高价。
标签: php wordpress woocommerce product custom-fields