How to Change Product Price Programmatically in WooCommerce

WooCommerce Change Product Price ProgrammaticallyYou can change product price programmatically using a filter for any WooCommerce product as you will see in this tutorial and on the example.  When creating a WooCommerce plugin or theme, you may wish to change the price of the product based on certain conditions. This code will enable you to change product price programmatically.

Changing the price programmatically is important when you are developing a theme that supports product options that have different prices for each option selected.

In the specific example I have been using in most WooCommerce tutorials, the product may have additional options like the size, color, etc. When each of these options is selected the price of the product should be adjusted accordingly.

Change Price Programmatically in WooCommerce

On the product page, we can create some logic that changes the price programmatically by adding the price to the existing price. For example, we can display the new price and update the original price by adding a new option as shown in the image below:

We will change the price by adding a checkbox that when a user selects the price is programmatically changed.To add a checkbox on the place indicated by the arrow you can use the code below:

add_action('woocommerce_before_add_to_cart_button', 'add_check_box_to_product_page', 30 );

function add_check_box_to_product_page(){ ?>

          <form action=" ">

                   <label for="newprice"> New Price</label>

                     <input type="checkbox" name="newprice" value="new_price"><br     

              </form>

      <?php

}

WooCommerce Change Product Price Programmatically

How to Change Product Price Programmatically

To change the price on the product page we need to get the price of the specific product from the global post object since the product is a custom post type and has all the methods of the global $post object.

So we need to create the filter hook that will hook on the ‘woocommerce_get_price’ then we filter the price and add the change we need to programmatically alter the price.  This code to alter the price will be contained in the callback function.

So if we want the new price to have an additional, deduction or to have a multiple we can apply it in the code.

The following is the code that will change the original price programmatically by adding the extra cost of 35, first we need to have the original product price set as follows:

WooCommerce Change Product Price Programmatically

We can add the code that adds 35 to the existing price and the new price will be displayed with the additional price:

add_filter('woocommerce_get_price', 'woocommerce_change_price_by_addition', 10, 2);

function woocommerce_change_price_by_addition($price, $product) {
	
	//global post object & post id
     global $post;
	 $post_id = $post->ID;
	 
	 //get the product 
    $product = wc_get_product( $post_id );
   
   // change the price by adding the 35 
    $price = ($price + 35);
	
	//return the new price
    return  $price;
}

The new price will be displayed like the price as you can see on the image below:

WooCommerce Change Product Price Programmatically

We can now display the new price anywhere we want on the WooCommerce product page. For example, the price can be displayed with the checkbox that conditionally changes the price as shown in the image above.

You can also change the code to reflect change you want and you can extend this code to have a backend option that changes the price based on quantity, size or even have your own entry of the price to be added as shown in this case where I have added the option to change the price dynamically using the product field at the backend:

WooCommerce Change Product Price Programmatically

Wrapping Up

There are so many ways you can change the price of the product programmatically, in this post; I wanted to just illustrate with an example of how the price can be changed. You can employ this solution in several other cases like when you are adding taxes, working with variable products or applying discounts and partial payments in WooCommerce.

Comments are closed.