Displaying Default Variation Regular and Sale Prices in WooCommerce

WooCommerce is a powerful platform for running online stores on WordPress, providing a range of features to enhance the shopping experience. For variable products, where different variations exist, displaying the default variation’s regular and sale prices can be essential for informing customers about the product’s pricing details. In this article, we’ll explore a simple method to achieve this customization.

WooCommerce: Display default Variation Regular and Sale Price

Step 1: Determine if the Product is Variable

To begin, we need to check if the product being viewed is a variable product. This ensures that the logic is applied only to products with multiple variations.

global $product;

if ($product->is_type('variable')) {
    // Logic for variable products
}

Step 2: Get Default Variation ID

For variable products, WooCommerce allows you to set a default variation. We can retrieve the default variation ID using the get_default_variation_id() method.

$default_variation_id = $product->get_default_variation_id();

Step 3: Retrieve Default Variation Object

Once we have the default variation ID, we can get the variation object using the wc_get_product() function.

$default_variation = wc_get_product($default_variation_id);

Step 4: Display Regular and Sale Prices

With the default variation object in hand, we can easily fetch and display the regular and sale prices.

// Output regular price
echo 'Regular Price: ' . wc_price($default_variation->get_regular_price());

// Output sale price if exists
if ($default_variation->is_on_sale()) {
    echo ' | Sale Price: ' . wc_price($default_variation->get_sale_price());
}

Step 5: Hook into the Single Product Page

To ensure that our code executes at the right time, we’ll hook into the woocommerce_before_single_product action.

add_action('woocommerce_before_single_product', 'display_default_variation_prices', 20);

Complete Code

 // Display default variation regular and sale prices
function display_default_variation_prices() {
    global $product;

    // Check if it's a variable product
    if ( $product->is_type('variable') ) {
        // Get the default variation ID
        $default_variation_id = $product->get_default_variation_id();

        // Get the default variation object
        $default_variation = wc_get_product($default_variation_id);

        // Output regular price
        echo 'Regular Price: ' . wc_price($default_variation->get_regular_price());

        // Output sale price if exists
        if ( $default_variation->is_on_sale() ) {
            echo ' | Sale Price: ' . wc_price($default_variation->get_sale_price());
        }
    }
}

// Hook to display prices
add_action('woocommerce_before_single_product', 'display_default_variation_prices', 20);

Conclusion

By following these steps, you can now display the default variation’s regular and sale prices on the single product page in WooCommerce. This customization provides valuable information to customers, helping them make informed purchasing decisions. Feel free to adjust the code to match your specific design and styling preferences.

Leave a Reply

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

Select your currency