gpt4 book ai didi

php - WooCommerce 可变产品 : keep only "min" price with a custom label

转载 作者:行者123 更新时间:2023-12-02 05:33:59 25 4
gpt4 key购买 nike

在函数文件中,我添加了一个过滤器 Hook ,以在变体产品“最低”价格之前添加自定义标签。

如何让标签与价格在同一行?

查看我的代码和下面的屏幕截图:

add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
$min_price = $product->get_variation_price( 'min', true );
$price = sprintf( __( 'From%1$s', 'woocommerce' ), wc_price( $min_price ) );
return $price;
}

最佳答案

自 WooCommerce 3 起,woocommerce_variable_sale_price_html Hook 已弃用,不再有用。如果您不关心“最低”促销价格(当最低价格促销时),您可以使用以下内容:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );

$min_price_html = wc_price( $min_price ) . $product->get_price_suffix();
$price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );

return $price;
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并适用于 WooCommerce 3+。你会得到类似这样的东西:

enter image description here

如果您关心“最低”促销价(当最低价促销时),并且您想显示这两个价格,则应使用以下代码:

add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
$prices = $product->get_variation_prices( true );
$min_price = current( $prices['price'] );

$min_keys = current(array_keys( $prices['price'] ));
$min_price_regular = $prices['regular_price'][$min_keys];
$min_price_html = wc_price( $min_price ) . $product->get_price_suffix();

if( $min_price_regular != $min_price ){ // When min price is on sale (Can be removed)
$min_price_regular_html = '<del>' . wc_price( $min_price_regular ) . $product->get_price_suffix() . '</del>';
$min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>';
}
$price = sprintf( __( 'From %1$s', 'woocommerce' ), $min_price_html );

return $price;
}

代码位于事件子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并适用于 WooCommerce 3+。你会得到类似这样的东西:

enter image description here

To handle when all variations prices are the same:

WooCommerce variable products: Display the min price with a custom text for different prices

关于php - WooCommerce 可变产品 : keep only "min" price with a custom label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46005952/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com