How to Sell Products by Weight WooCommerce?

You’d agree with me that not all products can be sold according to their quantity. Some products, such as nut butter, water, brine, vinegar, syrups, fruit or vegetable juice and a host of other products are sold according to their weight.

For example, selling a bunch of fruits by weight using price per kilogram, will give buyers the complete freedom to shop in the prices they want. The customer will be able to type in exactly how many grams he wants to buy. This will then dynamically display the price.

I had a client who wanted to sell his “Thin Pork Sausages” on WooCommerce using price per kg, instead of selling the products in quantities. So, the more the weight of the Thin Pork Sausages, the higher the price it would be.

But, this was quite difficult for him because the system (WooCommerce) was built for selling in quantities.

The solution to this, involved a manipulation and complete hack of the system in order to sell certain products by weight.

In this article, we’re going to show you a step by step tutorial on how we did it and how you can also sell your product on WooCommerce price per kg.

Step 1: ​The very first step you want to take is to add the price according to the weight. To do this, head straight to your Dashboard -> Appearance -> Editor -> Theme Function

Step 2: After that, scroll down to the bottom of the code section, which is displayed on your screen and enter the following code:

add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
	// Change and return $price_html variable using the $price and weight amount
	function wb_change_product_html( $price ) {
		$price_html = '<span class="amount">' . $price . ' per kg </span>';	// change weight measurement here
		return $price_html;
	add_filter( 'woocommerce_cart_item_price', 'wb_change_product_price_cart' );
	// Change the cart prices with $price variable and weight amount
	function wb_change_product_price_cart( $price ) {
		$price = $price . ' per kg';	// change weight measurement here
		return $price;
	add_filter( 'woocommerce_checkout_cart_item_quantity', 'wb_checkout_review', 10, 3 );
	// Change the checkput prices with $cart_item variable and weight amount
	function wb_checkout_review ( $quantity, $cart_item, $cart_item_key ) {
		$cart_item = ' <strong class="product-quantity">' . sprintf( '&times; %s', $cart_item['quantity'] ) . ' kg </strong>';	// change weight measurement here
		return $cart_item;

Step 3: After entering the code, make sure you Update File to save the changes.

After you have updated the file, move straight to your store to see if the prices of the products are now displayed according to their weight in ‘per kg’ just like it’s shown in the picture below.

If all the products display its price in per kg, then congratulations! You can now sell your products by weight in your store.

Sell By Weight for a Certain Category:

Many People were asking me about How to Sell by Weight for a Specific Category. So here is the Code for it.

Just change the Category name according to your’s.

    add_filter( 'woocommerce_get_price_html', 'wb_change_product_html' );
// Change and return $price_html variable using the $price and weight amount
function wb_change_product_html( $price ) {
	// Here is urban is demo category name
	if ( is_product_category('fresh') ) {
		$price_html = '<span class="amount">' . $price . ' per kg </span>'; // change weight measurement here
	} else {
		$price_html = '<span class="amount">' . $price . ' </span>';
	return $price_html;

I believe with this knowledge, you can start selling some of your products per kg in your store instead of selling them according to their quantities. This is the simplest way you can easily add the weight amount to your products on WooCommerce.

I hope you’ve found this post interesting and useful? Let me know if you have any question concerning this tutorial.


I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

11 thoughts on “How to Sell Products by Weight WooCommerce?”

  1. Great read, thanks you! I am going to try and see if I can customize it the way I want.

    Currently I am also experimenting with this plugin which also allows you to add units to your price:

    But I noticed it does not change the text everywhere while your code change does. I am going to try and modify your code so it does not work for all products by default. I want to add a hidden custom field to all products to determine if they are sold by grams or not and then use an if statement to add your code only to the products which are sold by grams.

    Thanks a lot of sharing your code, helps me further a lot!

  2. It works wonderful for all products, thank you for the comments and such!
    You wouldn’t happen to have the code to do this for SOME products instead of ALL?

  3. Very helpful, could we also exclude some products? because they are sold by pieces.
    Is there an option to show 100gram on the store page, but not selling this amount on the product page?
    Thanks for your feedback

  4. Hello from Peru.
    I have the same question Greg has… “You wouldn’t happen to have the code to do this for SOME products instead of ALL?”…
    Due to the Covid19 pandemic, there is a necessity by vendors to sell their wares via online stores, as people are very wary of going to supermarkets… I am trying to setup a store for a vegetable store, and indeed vegetables are sold mostly by weight but not all, some (the least) are sold by units (Beet (bundles), lettuce (units), Corn (units))… Is there a way you could modify the code you so graciously shared, to accommodate for both options?

  5. HI
    tnx for this piece of code!
    just a problem, when i try to set Weight for a Certain Category, I’ve just copied your code and changed category according mine product category.. but it doesn’t work 🙂

  6. Hi, your article helped me a lot. However, I am unable to display by category. Could you revise the code? Thanks in advance if you read this?

  7. Good piece of information. I had a similar requirement. Customer wanted to sell all products in 500g. This solution helps to display the price. But how this can be included for all calculations in shopping cart? Appreciate your thoughts. I don’t wanna use any plugins though

Leave a Comment

Your email address will not be published. Required fields are marked *