Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Change Product Prices via a WooCommerce Hook?

WooCommerce’s Edit Product settings allow you to modify the prices of individual products in your online store. But what if you want to provide a discount or increase prices for all the products across your website? Manually editing each product, would be a daunting task, as it would require editing every single product. Even when using bulk actions, you can only modify the prices of a limited number of products that are available on a single page.

This post helps you to change product prices via a hook in WooCommerce.

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Change Product Prices via a Hook in WooCommerce

Imagine you have an online store with a massive collection of products. If you wish to make price adjustments, such as increasing the prices of all products in your store, you don’t need to do it manually. The code snippets below are helpful when you want to increase the prices of all your products. The good part is that you can revert back to the original prices easily.

// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
    return 2; // x2 for testing
}

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'ts_custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'ts_custom_price', 99, 2 );
// Variations
add_filter('woocommerce_product_variation_get_regular_price', 'ts_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'ts_custom_price', 99, 2 );
function ts_custom_price( $price, $product ) {
    return (float) $price * get_price_multiplier();
}

// Variable (price range)
add_filter('woocommerce_variation_prices_price', 'ts_custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'ts_custom_variable_price', 99, 3 );
function ts_custom_variable_price( $price, $variation, $product ) {
  

    return (float) $price * get_price_multiplier();
}

Output 

Use-case: Increased price of all products

Once you’ve applied the code, you’ll notice that the prices of all the products in the store have been doubled.

How to Change Product Prices via a WooCommerce Hook?

Use-case: Regular sale price of all products

The below output shows that when the code is not applied the original, regular prices of all products will be present in your online store.

How to Change Product Prices via a WooCommerce Hook?

Code Explanation

  1. get_price_multiplier Function:
    1. This function is a utility function that returns a price multiplier. In the code, it’s set to 2, which means it will double the prices for testing.
  2. Filters for Simple, Grouped, and External Products:
    1. These filters are used to modify the regular and sale prices of simple, grouped, and external products. They apply the custom_price function to calculate the new price based on the multiplier.
    2. The woocommerce_product_get_price filter handles the regular price.
    3. The woocommerce_product_get_regular_price filter handles the sale price.
  3. Filters for Variations:
    1. These filters are used to modify the regular and sale prices of product variations. Variations are used in WooCommerce for products with different options, like size or color.
    2. The woocommerce_product_variation_get_regular_price filter modifies the regular price of variations.
    3. The woocommerce_product_variation_get_price filter modifies the sale price of variations.
  4. custom_price Function:
    1. This function takes the original price ($price) and the product object ($product) as parameters.
    2. It calculates the new price by multiplying the original price by the price multiplier obtained from the get_price_multiplier function.
    3. The function ensures that the result is returned as a float (decimal number).
  5. Filter for Variable Products (Price Range):
    1. These filters are used to modify the prices of variable products, which are products with multiple variations that have different prices.
    2. The woocommerce_variation_prices_price filter adjusts the displayed price of variations.
    3. The woocommerce_variation_prices_regular_price filter adjusts the regular (non-sale) price of variations.
  6. custom_variable_price Function:
    1. This function is similar to the custom_price function but is used specifically for variable products.
    2. It takes the original price ($price), the variation object ($variation), and the product object ($product) as parameters.
    3. It calculates the new price by multiplying the original price by the price multiplier.

Conclusion

The above code will double the product prices. But you can also modify the code to provide percentage-based discounts based on order quantity as well.

Doubling the product prices can also be achieved via a plugin named Product Prices By User Roles for WooCommerce that lets you set up price multipliers based on the user roles when you set prices globally for products.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x