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.

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.

About the author

We provide WordPress & WooCommerce Support, Maintenance and Development Services.
6 Responses
  1. Simon

    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?

Leave a Reply