Retrieve the minimum price excluding 0 and display it, rather than showing a range

JS Code Snippets/Functions
// Min Price in Shop Page
add_filter( 'woocommerce_get_price_html', 'custom_change_variable_price_display', 10, 2 );
function custom_change_variable_price_display( $price, $product_obj ) {
    global $product;

    if ( 'variable' !== $product_obj->get_type() || 'product_variation' === $product_obj->post_type ) {
        return $price;
    } else {
        $variations = $product_obj->get_available_variations();
        $prices = array( $product_obj->get_variation_price( 'min', true ), $product_obj->get_variation_price( 'max', true ) );

        // Translators: %s is the lowest variation price.
        $variation_price = array();
        foreach ( $variations as $variation ) {
            $c = 0;
            array_push( $variation_price, $variation['display_price'] );
        }
        sort( $variation_price );

        if ( $variation_price[0] == '0' ) {
            $price = "$ " . $variation_price[1];
        } else {
            $price = "$ " . ( $prices[0] !== $prices[1] ? sprintf( __( '%s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] ) );
        }
    }

    return $price;
}

Leave a Reply

Your email address will not be published. Required fields are marked *