Customize WooCommerce Variation Text

Intermediate

PHP

Details

This code snippet for WooCommerce or Divi BodyCommerce allows you to customize the text for your variable products as they are displayed in the product loop. This code should be added to the functions.php file of your child theme and can be customized to display formats like “From…”, “Starting at..”, etc instead of the default min to max pricing format.

Snippets Demo

Example of custom text for variable products in WooCommerce and Divi BodyCommerce

Code

1

Description:
This code will give you the framework to customize the way that variation pricing is displayed in the product loop.

Language: PHP

Where to add: functions.php

                        add_filter('woocommerce_variable_sale_price_html', 'custom_variation_price_format', 10, 2);
add_filter('woocommerce_variable_price_html', 'custom_variation_price_format', 10, 2);

function custom_variation_price_format($price, $product) {
    // Get all variation prices
    $prices = $product->get_variation_prices(true);

    // Get the minimum and maximum price
    $min_price = current($prices['price']);
    $max_price = end($prices['price']);

    // Set your custom price format
    if ($min_price == $max_price) {
        $price = wc_price($min_price); // Single price
    } else {
        // Custom price formats
        $price = sprintf(__('From %1$s', 'woocommerce'), wc_price($min_price)); // Example: "From $50"
        // $price = sprintf(__('%1$s+', 'woocommerce'), wc_price($min_price)); // Example: "$50+"
        // $price = sprintf(__('Starting at %1$s', 'woocommerce'), wc_price($min_price)); // Example: "Starting at $50"
    }

    return $price;
}

                    

Related Snippets

Explore more from Divi Engine