It’s common that prices change continuously, from one day to the next, from one-time slot to the next, and also based on the device used to search for products. The advantage is obvious: if the offer changes according to consumer demand, profits can be maximized.
The mechanism behind these variations is called dynamic pricing. It constantly recalculates prices based on predefined variables through different algorithms and store owners can leverage it considering seasonality and user behavior.
In this article, I’ll tell you how you can add custom dynamic pricing in WooCommerce for all products and specific products based on product fields.
Future-Proof Your Online Store with Autonomous
Keep your online store ready for all the unexpected traffic spikes with Cloudways Autonomous. Stay ahead of the curve with advanced scalability and cutting-edge technology.
Change Price Display for All Products
Changing the WooCommerce pricing display involves two important filters:
woocommerce_get_price_html changes how the price is shown on the product and shop pages.
function cw_change_product_price_display( $price ) { $price .= ' At Each Item Product'; return $price; } add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' ); add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
woocommerce_cart_item_price changes the way product prices are shown in the cart table.
ct_price_display' ); add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
The woocommerce_get_price_html filter will change this on the shop pages:
Similarly, on individual product pages:
The woocommerce_cart_item_price filter will adjust this display within the cart as well:
Sort Single Product Price Display
What if you want to change a particular product’s price display? This can be done using the same filters, as they will pass additional arguments for conditionally changing the price.
From launching to customizing your WooCommerce stores, Cloudways is at your service.
Whether you’re a beginner or an expert, Cloudways Platform is based on UI, where you can create and customize your online store in a few seconds.
This time I will separate out these filters and use two functions to modify the product and cart prices.
I will only check the product ID and change the pricing display for that product.
function cw_change_product_html( $price_html, $product ) { if ( 22 === $product->id ) { $price_html = '<span class="amount">$50.00 per Unit</span>'; } return $price_html; } add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 ); function sv_change_product_price_cart( $price, $cart_item, $cart_item_key ) { if ( 22 === $cart_item['product_id'] ) { $price = '$50.00 per Unit<br>(7-8 skewers per Unit)'; } return $price; } add_filter( 'woocommerce_cart_item_price', 'sv_change_product_price_cart', 10, 3 );
The first function will change my product page:
Along with the shop display:
While the second function will adjust my cart display with my new WooCommerce pricing:
However, if you have multiple products with unit prices, this method could be very cumbersome, and I probably do not want to use a switch or an if statement block for every product ID. Instead, I recommend using a product custom field to adjust the price display. I will use the ID to get this field and then save myself from a lot of coding because I will pull the field in programmatically.
Change Price Display Based on Product Fields
If you want to change the pricing display for some products (each product might have a different unit price), a custom field will result in far simpler code, as I can add the field for each product on the list and then simply retrieve this field in the code:
Add Custom Field in WooCommerce
Once you have added the custom field, you can then retrieve the unit price, and if one is set for the product, adjust the pricing display to use the unit price and a custom label instead:
function cw_change_product_html( $price_html, $product ) { $unit_price = get_post_meta( $product->id, 'unit_price', true ); if ( ! empty( $unit_price ) ) { $price_html = '<span class="amount">' . wc_price( $unit_price ) . ' per kg</span>'; } return $price_html; } add_filter( 'woocommerce_get_price_html', 'cw_change_product_html', 10, 2 ); // Change the cart prices if a unit_price is set function cw_change_product_price_cart( $price, $cart_item, $cart_item_key ) { $unit_price = get_post_meta( $cart_item['product_id'], 'unit_price', true ); if ( ! empty( $unit_price ) ) { $price = wc_price( $unit_price ) . ' per kg'; } return $price; } add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_cart', 10, 3 );
Note: wc_price() is a handy function for formatting numbers within the shop pricing display settings. All the codes are put in functions.php, which is located in the theme folder.
In this case, the pricing display on the product/shop page will only be changed if a unit price is set. If not, the price will remain the same.
This will also happen with the items in the cart — the WooCommerce price display will be conditionally adjusted depending on whether the item has a unit_price set in the custom field:
Display WooCommerce Pricing using WP_Query
The following code displays dynamic WooCommerce pricing using WP_Query.
<?php $product_categories = array('cat-name'); $wc_query = new WP_Query( array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 10, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $product_categories, ) ) ) ); ?> <h1> Category Name </h1> <?php if ($wc_query->have_posts()) : ?> <?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail('full'); ?> <h1><?php the_title(); ?> </h1> <?php $price = get_post_meta( get_the_ID(), '_price', true ); ?> <p><?php echo wc_price( $price ); ?></p> </a> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else: ?> <li> <?php _e( 'You have no entry' ); ?> </li> <?php endif; ?>
The most important function in the code snippet above is WP_Query, which provides several important functions such as post_type, post_status, posts_per_page, and tax_query. All these functions provide common functionality within the main loop. The get_post_meta() fetches the meta values. the currently selected currency is displayed through the wc_price() function. In the main query, wp_reset_postdata() is used to set the $post global to the current post. Finally, $woocommerce_price_display_cat has the currently selected WooCommerce product category.
Summary
In this article, I discussed the issue of changing the price display on WooCommerce product pages. This easy alteration could be done by adding simple code to product page filters. Leave a comment below if you have questions or suggestions about the code or the article.
Owais Khan
Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.