用 WooCommerce 可变产品中的文本替换显示的产品零价格

问题描述 投票:0回答:1

我在 WooCommerce 中使用以下代码,用自定义文本替换显示的产品零价格:

add_filter( 'woocommerce_get_price_html', 'wtwh_price_free_zero', 9999, 2 );  
function wtwh_price_free_zero( $price, $product ) {
    if ( $product->is_type( 'variable' ) ) {
        $prices = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
        if ( 0 == $min_price ) {
            $max_price = end( $prices['price'] );
            $min_reg_price = current( $prices['regular_price'] );
            $max_reg_price = end( $prices['regular_price'] );
            if ( $min_price !== $max_price ) {
                $price = wc_format_price_range( 'FREE', $max_price );
                $price .= $product->get_price_suffix();
            } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
                $price = wc_format_sale_price( wc_price( $max_reg_price ), 'FREE' );
                $price .= $product->get_price_suffix();
            } else {
                $price = 'FREE';
            }
        }
    } elseif ( 0 == $product->get_price() ) {
        $price = '<span class="woocommerce-Price-amount amount">Contact us for a unique quote!</span>';
    }  
    return $price;
}

问题是这不适用于我的可变产品,我希望对这些可变产品有类似的行为,用“FREE”字符串替换零格式的价格。

如何用 WooCommerce 可变产品的文本替换显示零价格的产品?

php html wordpress woocommerce product
1个回答
0
投票

你就在附近。您可以使用

str_replace()
PHP 函数将零格式的价格替换为“FREE”字符串,例如:

add_filter( 'woocommerce_get_price_html', 'custom_display_for_product_with_zero_price', 9999, 2 );
function custom_display_for_product_with_zero_price( $price_html, $product ) {
    if ( $product->is_type( 'variable' ) ) {
        $prices    = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );

        if ( $min_price > 0 ) {
            return $price_html;
        }
        $max_price     = end( $prices['price'] );
        $min_reg_price = current( $prices['regular_price'] );
        $max_reg_price = end( $prices['regular_price'] );
        $free_txt      = esc_html__('FREE', 'woocommerce');

        if ( $min_price !== $max_price ) {
            $price_html = wc_format_price_range( $min_price, $max_price ) . $product->get_price_suffix();
            $price_html = str_replace( wc_price(0), $free_txt, $price_html ); // Replace formatted zero price with a text
        } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
            $price_html = wc_format_sale_price( wc_price( $max_reg_price ), wc_price( $min_price ) ) . $product->get_price_suffix();
            $price_html = str_replace( wc_price(0), $free_txt, $price_html ); // Replace formatted zero price with a text
        } else {
            $price_html = $free_txt . $product->get_price_suffix();
        }
    } elseif ( 0 == $product->get_price() ) {
        $price_html = sprintf(
            '<span class="woocommerce-Price-amount amount">%s</span>%s', 
            esc_html__('Contact us for a unique quote!', 'woocommerce'),
            $product->get_price_suffix()
        );
    }  
    return $price_html;
}

现在它应该适用于您显示的可变产品价格。

© www.soinside.com 2019 - 2024. All rights reserved.