skip to Main Content

EDIT:
Here is some examples:

  1. A product weighs 1g
  2. The price of the product is $10 / g
    (Customer chooses number of products, for example 2)
  3. The price will be: $20 (2 * 10)

Example 2:

  1. A product weighs 500g (Let’s say rice in this case)
  2. The price is $100 / kg
  3. Customer adds only 1 rice to basket
  4. The price should then be: $50

Declaration of the price:

  1. We convert g to kg, 500 g -> 0.5 kg
  2. 0.5 * price per kg
  3. 0.5 * 100 = $50

END OF EDIT.

I’m having a weird problem here and totally stuck. What I want to accomplish is:

  1. A product has the weight 500g
  2. The price of the product is $1/kg
  3. When a user adds only 1 quantity of the product, the price should be $0.5 because the weight of 1 product is 500g, the price is $1 for each kg, meaning 500g to kg becomes 0,5kg and that multiplied with the price of $1 becomes = 0,5 * 1 = $0,5. So the price in in cart should be $0,5.

Is this possible to do in woocommerce? I’m going to sell food that’s why the logic for pricing is that way.

Thanks!

3

Answers


  1. You can change the WooCommerce price to be by weight by appending some text after the actual price such as ‘per kg’, this can be done by manipulating 3 filters that display the price in the shop/product page, cart and checkout pages;

    woocommerce_get_price_html
    woocommerce_cart_item_price
    woocommerce_checkout_cart_item_quantity
    

    If all items were to be classified as ‘per kg‘ you could use the filters as below…

       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;
    
        }
    
    Login or Signup to reply.
  2. Based upon your query, I feel that you are looking to sell products by weight. The best way to do that is by creating a “Variable Product” using woocommerce.

    For that, please follow these steps:
    Step 1: The first step is to create an attribute for example refer to https://i.stack.imgur.com/Qsn8Z.png where
    (1) You login to the admin dashboard, and under products select “Attributes”
    (2) Then add a new attribute (for example “weight”).
    (3) Then in the right hand panel the attribute would get added, you would find a link over there which would say “Configure Terms”. You need to click on it.

    Step 2: Add Values to the attribute, for example please refer to https://i.stack.imgur.com/jIDut.png
    (4) Once you are done with (3), you would come to a page where you would be able to enter the value for the attribute for example 1kg, 500 gm.
    (5) The values added will show up in the right hand panel

    Step 3: Adding a product using the created attribute. For example please refer to https://i.stack.imgur.com/otvxf.png
    (6) Create a new product and select the type as "variable".
    (7) Click on the "Attributes" tab.
    (8) Click on the drop down which says "Custom product Attribute" and select "Weight" from there and click the "Add" button.
    (9) Click on the "Value(s)" text box and it would show all the values, make selections from there for example 500gm, 1kg.
    (10) check the "Visible from the product page" and "used for variations" and then click save attribute.

    Step 4: Creating variations of the products using the selected values. For example please refer to https://i.stack.imgur.com/d0EXL.png
    (11) Click on the "variations" tab
    (12) Select "create variations from all attributes" from drop down and click on the "go" button
    (13) The two variations would show up for example 500gm and 1kg would show up

    Step 5: Enter Price for each variation. For example please refer to https://i.stack.imgur.com/OFVxE.png
    (14) Click on the 1kg variation and enter the price as 1, select "stock status" as "in stock" and weight as "1" (this value is in kg by default.
    (15) Click on the 500gm variation and enter the price as 0.5, select "stock status" as "in stock" and weight as "0.5" (this value is in kg by default so 0.5 would mean half a and then hit "save changes" button followed by clicking the blue "update" button.

    Step 6: Seeing the displayed product. For example please refer to https://i.stack.imgur.com/2tx73.jpg
    You would notice the weights show up in a drop down and based upon the weight that you select from the drop down, the corresponding price changes. So if you select 1 Kg, it would show 1 dollar, if you select 500gm, it would show 0.5 dollars. The user can select quantities as per their liking. So if they prefer, they can purchase two quantities of 500 gm which would automatically update the cart price to be 1 dollar.

    Login or Signup to reply.
  3. I suggested you variable product since by doing that you can showcase the weights and give users options to purchase based on weight.
    If you create a simple product, it would enable the end user to add the product to his/her cart. So he/she can add one quantity or multiple quantities of it but the price per quantity will reflect in the cart. Woocommerce will not allow half a quantity of the product to be added to the cart which you are suggesting.

    As for me I feel variable product is the only option to go forward with as it will cater to your requirements and it will be user friendly as well. The option you are suggesting , can be achieved by coding a plugin but it will not be user friendly since it would show user price of a product but on adding the product to the cart it would show the cart price to be half of the product price. This would confuse the customer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search