php - 在 WooCommerce 产品循环上显示可变产品的自定义价格范围
问题描述
我正在尝试为我的可变产品显示自定义价格范围。我设法插入了一个包含常规(最低和最高)价格和销售(最低和最高)价格的价格范围。
这是我的代码尝试:
add_filter( 'woocommerce_get_price_html', 'custom_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'custom_price_format', 10, 2 );
function custom_price_format( $price, $product ) {
// Main Price
$regular_priceMin = $product->is_type('variable') ? $product->get_variation_regular_price( 'min', true ) : $product->get_regular_price();
$regular_priceMax = $product->is_type('variable') ? $product->get_variation_regular_price( 'max', true ) : $product->get_regular_price();
$sale_priceMin = $product->is_type('variable') ? $product->get_variation_sale_price( 'min', true ) : $product->get_sale_price();
$sale_priceMax = $product->is_type('variable') ? $product->get_variation_sale_price( 'max', true ) : $product->get_sale_price();
if ( $regular_priceMin !== $sale_priceMin && $product->is_on_sale()) {
$price = '<p class="teste"><del>' . wc_price($regular_priceMin). 'a' . wc_price($regular_priceMax) . '</del></p> <ins>' . wc_price($sale_priceMin) . '</ins>';
}
return $price;
}
但是,某些销售价格具有相同的值并且格式不正确。
它创建 3 行:
- 一个为最低价格值,
- 另一个字母“a”
- 另一个是最高价格值。
我怎样才能正确地组织这个?
tag<del>
标签不在同一行。
如何解决这个问题?我做错了什么?
解决方案
您不会使用$product->get_price_html()
适用于所有类型产品的标准吗?
推荐阅读
- php - 如果 cond 精确计数,如何制作?
- flutter - 为什么我的小部件上的参数传递为空?
- javascript - 在 axios 中使用异步等待
- python - 为什么另一个模型的上下文在模板中不起作用
- wagtail - 具有“全角”格式的图像未在 Wagtail 的 RichTextField 中居中?
- r - 学习 R:在 col-A 中查找值,在 Col-B 中列出唯一值,然后在 Col C 中列出相关值
- javascript - 找不到模块'../drop-file.js'量角器,角度
- swift - 出现错误:传递给不带参数的调用的参数
- swift - WebView 文件和相机 Feld 将我发送到 Web 应用程序的主页
- c++ - 当我调用他们的方法时,我的对象的成员发生了变化