This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

📣 Try the fastest hosting platform with pay-as-you-go pricing & 24/7 expert support! MIGRATE NOW →

How to Add Custom Dynamic Pricing Display in WooCommerce

Updated on March 17, 2022

5 Min Read
woocommerce dynamic pricing

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.

Change Price Display for All Products

Changing the WooCommerce pricing display involves two important filters:

  1. woocommerce_get_price_html
  2. woocommerce_cart_item_price

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:

Change WooCommerce Price Display

Similarly, on individual product pages:

individual product pages

The woocommerce_cart_item_price filter will adjust this display within the cart as well:

individual product pages

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:

change my product page

Along with the shop display:

shop display

While the second function will adjust my cart display with my new WooCommerce pricing:

adjust my cart display

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:

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.

pricing display on the product

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:

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour

CYBER WEEK SAVINGS

  • 0

    Days

  • 0

    Hours

  • 0

    Mints

  • 0

    Sec

GET OFFER

For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now