Vote up!
Vote down!

How do you add two price fields together?

Each product in my store has a price. That price is total the customer will pay plus a flat rate shipping cost. My client doesn't want to use flat rate. He wants to enter the shipping amount himself. I thought this was going to be easy... I created a new field and called it "shipping" and I made it a "price" type for my product. I added it to the product display, moved it around, made it look pretty and bam... it didn't work.

There are not two prices for each product. The "product price" and the "shipping price." The problem I'm having now is this: My "shipping price" is not calculated when a customer checks out. Only the "product price" is calculated. How can I calculate two prices per item? I've tried messing around in rules but have been unlucky so far.

There are some excellent tutorials out there and I'm educating myself... but I'm a full time electrician for my day job and I only spend nights and weekends doing website work. Does someone know how I can make both prices of a product type calculate? Thank you so much.

Asked by: cjaspencer
on February 8, 2013


This has GOT to have an easy answer. Can ANYONE please help me out with this. I'm struggling right now and don't want to leave my client hanging anymore. I keep digging in the rules. I've learned a lot, but I am getting no where. Please. I need assistance.

- cjaspencer on February 12, 2013

1 Answer

Vote up!
Vote down!

I just read through your question and I agree, there is a simple solution. I've created a rule, and I'm going to tell you how to have two price fields on your product and have them calculated correctly.

  1. Create a new Price Calculation Rule. Visit admin/commerce/config/product-pricing/rules/add and give it the name "Add Shipping Price to Product Price"
  2. Bring in the Product related to the line item. Add a condition "Entity has field" to the entity "commerce-line-item" and choose the field "commerce_product." This effectively loads the product for further inspection.
  3. Bring in the custom shipping field. Add a condition "Entity has field" to the entity "commerce-line-item:commerce-product" and choose the field that you created, mine is called "field_shipping_price."
  4. Confirm the new custom price field is not empty. Always a good idea to skip a calculation if the field is empty, so add a condition "Data is Empty" and choose the "amount" of the custom price field. My token looks like this: "commerce-line-item:commerce-product:field-shipping-price:amount" ... and make sure you click "NEGATE" at the bottom of the condition, we want all fields are *NOT* empty.
  5. Finally, add the custom price to the final price with a shipping component. Now we simply need to add one action called "Add an amount to the unit price" found under "Line Item" actions. Choose the price field's amount like you did before in the "NOT empty" condition. Mine looks like this: commerce-line-item:commerce-product:field-shipping-price:amount. Finally, choose the "component" type "shipping."

Export of this rule: http://pastebin.com/JmKjNPE4

First, you should add a product to your cart and see if you are getting an additional "shipping" component at the bottom in the "Subtotal" area. If not, then confirm your products have some amount in their new custom price field.

A few thoughts on price calculations and price components. Once you've done this, each price will include the "base price" and the new "shipping price" in one total. So I added $15 to my $30 product, the price field now outputs "$45." But what's *awesome* is that your price components know where that $15 is coming from. You can use the "Manage Display" for your Product Type's price field and choose "Formatted Amount with Components" to see all the component's that go into your product. The only real drawback to adding shipping in this manner is that the "shipping" cost is always a part of your product and you will have to "work" to display the components on your catalog and in other areas.

There are some contributed modules that help you with displaying your components in a friendlier manner than the default "table" that commerce outputs.

http://www.drupalcommerce.org/extensions/module/price for some of the modules that could help you.

Josh Miller
Answer by: Josh Miller
Posted: Feb 13, 2013



This is amazing. Thank you very much. The idea of adding a negate for a 0 shipping total is brilliant. The guy I do work for is qwerky. He's a "junker" and sells most of his items on ebay. However, ebay won't allow him to sell certain things. An odd one is flight wings or patches from airline companies... ebay thinks the person who buys them is going to stroll right into the cockpit and fly a the play... sure thing, right?

Needless to say, he ships a lot of products and, as people do, remembers how much it costs to ship something. "I don't mind losing a couple bucks on shipping. People will bid thousands for an item and argue over $25 dollars in shipping... it's crazy." This fix just made him a VERY happy camper. Thank you for the thorough response as well. I've been messing around is rules for about 5 days after work but couldn't quite get the right combo to work. You've really helped me understand where the data goes and what it is doing.

Thank you,

- cjaspencer on February 14, 2013

Hi joshmiller, your sample rules help my case thanks :)

- sibiru on April 19, 2013

Hi Josh - great solution that for sure has taken us one step closer our goal. The only thing is that Drupal commerce only seem to calculate VAT from the base price leaving out the fact that is should also include the second price field - or just calculating VAT of the total sum of the two as this will actually be the product price. Do you have any tricks that will solve this issue?
The price will be calculated using this formula: (Product base price * qty) + Second price field. This meaning that only the base price will change as the QTY changes up or down and adding/(+) the static secondary price/amount.

Hope there is a solution out there :-)

Kind regards Johan

- Johan Rube on April 20, 2013

For VAT taxes, you could go into the VAT tax calculation rule (each tax is a specially generated rule here: admin/commerce/config/taxes'

Look for something called "Configure Component" and the rest here is just me "thinking it through" -- don't have time to fully test if this works.

  • Click Configure Component
  • I wouldn't add any additional conditions beyond the "Entity has field" that I described above
  • Add an action that affects the line item's price using "Set a data value"

Caution: Not sure if setting the data value is the best solution, but it is the first thing I would try.

- Josh Miller on April 22, 2013

hi, thanks for guidance. I am facing same problem and this works for me. But I am facing one new problem is that when select other currency through currency selector the Unit Price converted properly but the Shipping Price is not converted and the amount remains same but the Currency changed. Can you help me to resolve this...

- Pankaj Pareek on October 9, 2014