How to Change Product Price Programmatically on WooCommerce?

Product prices are not always constant in an eCommerce store. It must be fluctuating and flexible to attract customers to your store. When a store provides products at a minimum price compared to other competitors, customers will definitely choose that store over others. When more customers visit your store and purchase products frequently, it will definitely give a lift in your sales and site engagement. Improved site engagement will be advantageous for your site’s SEO ranking. When your store ranks on top in search results, more customers will visit your store! Hence, there are many reasons you have to provide frequent price drops and special offers in your store.

If your store is built with the WooCommerce platform, you can directly go to your product page dashboard and change the product prices as per your requirements. But, it is a tiring task if you need to go through multiple pages and update the product prices. You can update the sale price and regular price of multiple products together using the default WooCommerce bulk edit option. But, you can only change the product price statically with this option. Also, you will be required to change the prices every time using this option when you plan to change the price of your products.

There are third-party plugins and extensions to help you apply dynamic pricing based on specific products and different parameters associated with the customer’s purchase behavior. But, if you do not want to integrate any third-party plugin into your store, coding is the next option to change the product price.

In this article, we will be discussing how to change product prices programmatically on WooCommerce stores with an example. You can use the code or tweak it as per your requirements to implement the price changes in your store programmatically.

Change Product Price Programmatically on WooCommerce Store

WooCommerce is a plugin for WordPress websites, and they are built with PHP functions. Hence, almost all actions and events are coded with PHP in WordPress – WooCommerce websites.

In this example, we are changing the product price using a PHP hook, which you can tweak as per your business requirements.

What is a PHP hook?

A PHP code hook is a specially-defined part of a program code that is used to pass control to an add-on or extension. When it comes to the programming world, the PHP hook is declared by calling a special function in the necessary part of the program code.

In this example, let us consider the role based discount in the WooCommerce store and how to implement it using a code snippet. So, check the below code that is used to change product price for the user roles – administrator and vendor.

Check the snippet:

add_action( 'woocommerce_cart_calculate_fees', 'elex_discount_price' ); 

function elex_discount_price() 



global $woocommerce; //Set the price for user role. 

$discount_price = 5; 

$set_role = array('administrator','vendor'); //Get the login user role 

$user = wp_get_current_user(); 

$user_role = $user->roles; //Check the specifc user role for discount. 

if( in_array( $user_role[0] , $set_role ) )



$woocommerce->cart->add_fee( 'Discounted Price', -$discount_price, true, 'standard' ); 



As per the above example, $5 will be deducted from the product prices when a user is logged in as an administrator or vendor on your WooCommerce store.

The function used in the above code will fetch the product price from the customer’s shopping cart if the user comes under administrator or vendor roles.

But, what if you are not technically savvy and not at all interested in programming and code?

Then the perfect solution is to choose a plugin!

Change product price using dynamic pricing and discounts plugin

Changing the product price with a robust plugin will be the easiest and convenient solution if you need to apply more interesting and different types of discounts in your store. Choosing a plugin with a simple and intuitive interface will help you configure advanced discount rules in your store without complex coding.

ELEX WooCommerce Dynamic Pricing and Discounts Plugin is an advanced dynamic pricing plugin to apply various discounts and offers in the WooCommerce store with its top-notch features. It comes with 9 default discount rules using which you can apply discounts based on the products, categories, tags, combinations of products, and so on. Also, you can set free products or gift products with its seamless configuration process. In addition to these, it allows you to set volume pricing, quantity based discounts, tiered pricing, role-based pricing, flash sales, etc.

ELEX WooCommerce Dynamic Pricing and Discounts Plugin

To know more about the details of this plugin, refer to an article – An Ultimate Guide on WooCommerce Dynamic Pricing – Covers Simple and Complex Scenarios.

This plugin comes with 9 default discount rules as follows:

  • Product Rules: This rule is to apply a percentage, flat, or fixed price discount based on the quantity, weight, and price for individual products, all products in a single category, or all products in the customer’s shopping cart.
  • Category Rules: This rule allows you to apply a percentage, flat or fixed price discount to products in multiple product categories based on the quantity, weight, and price of the products.
  • Combinational Rules: You can apply a percentage, flat, or fixed price discount when a customer purchases a specific combination of individual products from your store.
  • Category Combinational Rules: This discount rule will help you to set a percentage, flat or fixed price discount to customers when they purchase products from a specific combination of product categories.
  • Cart Rules: This rule will help to apply a percentage, flat, or fixed price discount based on the total number of products, weight, and total price of products in a shopping cart.
  • Buy and Get Free Offer (BOGO) Rules: You can set free products or gift products when a customer purchases a certain number of individual products.
  • Buy and Get Offer (BOGO) Category Rules: This rule allows you to offer free products when a customer purchases a certain number of products from specific categories.
  • Tag Rules: Apply a percentage, flat, or fixed price discount to products based on their tags by checking their quantity, weight, and price.
  • BOGO Tag Rules: Provide free products when a customer purchases products with certain tags.

You can apply many more discounts and configure additional features to improve the discount rules and set their priority of execution.

Wrap Up

As per your requirements, you can either implement programming code snippets to change product prices or apply more advanced discounts and pricing features in an intuitive interface using a dynamic pricing plugin!

 

Further Reading:

Popular Tags

Blog Business Case Code Snippet Documentation ELEX Address Validation & Google Address Autocomplete Plugin for WooCommerce ELEX Amazon Payments Gateway for WooCommerce ELEX Authorize.net Payment Gateway for WooCommerce ELEX Bulk Edit Products, Prices & Attributes for WooCommerce ELEX Dynamic Pricing and Discounts Plugin for WooCommerce ELEX EasyPost Auto-Generate & Email Labels Add-On ELEX EasyPost Shipping Method Plugin for WooCommerce ELEX Google Product Feed Plugin ELEX Hide WooCommerce Shipping Methods Plugin ELEX ShipEngine Multi-Carrier Shipping & Label Printing Plugin for WooCommerce ELEX Stamps.com Shipping Plugin with USPS Postage for WooCommerce ELEX Stamps.com USPS Auto-Generate & Email Labels Add-On ELEX WooCommerce Abandoned Cart Recovery with Dynamic Coupons ELEX WooCommerce Australia Post Bulk Label Printing Add-On ELEX WooCommerce Australia Post Shipping Plugin with Print Label & Tracking ELEX WooCommerce Catalog Feed for Facebook & Instagram ELEX WooCommerce Catalog Mode, Wholesale & Role Based Pricing ELEX WooCommerce Choose Your Delivery Date Plugin ELEX WooCommerce DHL Express / eCommerce / Paket Shipping Plugin with Print Label ELEX WooCommerce DHL Express Bulk Label Printing Add-On ELEX WooCommerce Discount per Payment Method Plugin ELEX WooCommerce EasyPost Bulk Label Printing Add-On ELEX WooCommerce EasyPost Return Label Add-On ELEX WooCommerce Name Your Price Plugin ELEX WooCommerce Product Price Custom Text (Before & After Text) and Discount Plugin ELEX WooCommerce Stamps.com Bulk Label Printing Add-On ELEX WooCommerce USPS Shipping Plugin with Print Label ELEX WooCommerce Request a Quote plugin for WooCommerce ELEX WordPress Embed YouTube Video Gallery ELEX WordPress Embed YouTube Video Gallery Plugin FAQ Getting Started Knowledge Base Open Source HelpDesk & Customer Support Ticketing System – Simple & Flexible Plugin Integrations ReachShip WooCommerce Multi-Carrier & Conditional Shipping Plugin Shipping Plugins Uncategorized WooCommerce WSChat - WordPress Live Chat Plugin WSDesk - WordPress Helpdesk Plugin