# Prices varies by Quantity

Thread began 1/13/2011 9:54 am by kingston.andrew401728 | Last modified 1/14/2011 8:11 am by Jason Byrnes | 6274 views | 5 replies |

## 1/13/2011 9:54 am  |  #1 kingston.andrew401728

### Prices varies by Quantity

Hi,
I have a list of products where the price per item varies depending on how many are being purchased. Its quite straightforward really - the more you buy the cheaper the product gets.
For example:

1 - 99 - €32 each
100 - 199 - €30 each
200+ - €28 each

I setup a separate table in my database to store these details and each one is tied to a product using the product ID.

I have managed to write some code into my products page to check what price to use according to the quantity selected, which works well, but the problem I have is if the person changes the quantity, either by adding more products through the products page or by changing the quantity in the shopping cart. If they change the quantity it continues to use the initial price regardless.

So basically I'm wondering how I can ensure it runs the same check again. Perhaps I need to clear out the price for the item in the session but I don't know how to do that. Alternatively I'm thinking maybe my code to check the quantity should be in the ecart1_PHP.php file but I have no idea where I would need to put it in there.

I hope this all makes sense. Please let me know if you need more details.

Thanks,
Andrew.

## 1/13/2011 2:36 pm  |  #2 Jason ByrnesWebAssist

the specifies depend on how elaborate this is going to be.

will it always have only 3 price breaks? will the breaks always be 1 - 99, 100 - 199 and 200+?

if so, then that's fairly easy, add 2 new columns to the cart lets say we call the, priceB and priceC.

In the add to cart button, the price column will get the initial price: 32, priceB will get the 100 - 199 price: 30 and the priceC column will get the 200+ price: 28

Now in the add top cart button, go to the calculations tab and create a new calculation:
realPrice:

`((abs([Quantity] >= 1) && abs([Quantity] <= 99))?[Price]:0)+((abs([Quantity] >= 100) && abs([Quantity] <= 199))?[priceB]:0)+((abs([Quantity] > 200))?[priceC]:0)`

you'll then have to edit the total price calculation from:

`[Price] * [Quantity]`

to:

`(((abs([Quantity] >= 1) && abs([Quantity] <= 99))?[Price]:0)+((abs([Quantity] >= 100) && abs([Quantity] <= 199))?[priceB]:0)+((abs([Quantity] > 200))?[priceC]:0)) * [Quantity]`

and in the cart, change the code that displays the item price:

php:
``<?php echo WA_eCart_DisplayMoney(\$eCart2, \$eCart2->DisplayInfo("Price")); ?>``

php:
``<?php echo WA_eCart_DisplayMoney(\$eCart2, \$eCart2->DisplayInfo("realPrice")); ?>``

Note, in the code above, eCart2 is the name of my cart object, if yours is differant, you will need to change that.

## 1/14/2011 2:22 am  |  #3 kingston.andrew401728

Hi Jason - thanks for the the reply. Unfortunately though its not so simple as there will not always be 3 price ranges and the ranges themselves could also change.

The way I did it on the products page was that I created an array from the table in the database and then just checked the quantity and used the corresponding price. As I mentioned though, the problem with that is when the quantity is updated it doesn't re-run the query to change the price.

I'm sure it will just need to be hard coded which I don't really mind but I just don't know where I need to put it and what exact variables I will need to change.

I had a similar problem a few weeks ago with shipping weights and managed to get around it in a similar way. At least then though I was just dealing with the total weight rather than the weight of each individual item.

If you could even just explain the logic to me of when the quantity of an item is updated how it then recalculates the price. Perhaps then I would know where to run the query again to check the price based on quantity.

I hope that all makes sense - I appreciate you help.

Andrew.

## 1/14/2011 6:51 am  |  #4 Jason ByrnesWebAssist

it's still the same idea, you just need to add in columns for the ranges as well.

so you have the price column by default, and the first range will always start at one, so you need to add the following columns to the cart:

priceRangeHigh
priceB
priceBRangeLow
priceBRangeHigh
priceC
priceCRangeLow

In the add to cart button, you will assign the values for the prices and the ranges, you initial example would have the add to cart button set as:
Price = 32
priceRangeHigh = 99
priceB = 30
priceBRangeLow = 100
priceBRangeHigh = 199
priceC = 28
priceCRangeLow = 200

then we just need to add the new range columns into the real price calculation:

`((abs([Quantity] >= 1) && abs([Quantity] <= [priceRangeHigh]))?[Price]:0)+((abs([Quantity] >= [priceBRangeLow]) && abs([Quantity] <= [priceBRangeHigh]))?[priceB]:0)+((abs([Quantity] > [priceCRangeLow]))?[priceC]:0)`

and add that into the total price calculation as well:

`(((abs([Quantity] >= 1) && abs([Quantity] <= [priceRangeHigh]))?[Price]:0)+((abs([Quantity] >= [priceBRangeLow]) && abs([Quantity] <= [priceBRangeHigh]))?[priceB]:0)+((abs([Quantity] > [priceCRangeLow]))?[priceC]:0)) * [Quantity]`

## 1/14/2011 7:50 am  |  #5 kingston.andrew401728

### 336. guarrifu

Thanks for the response again Jason. That sounds good alright. There might be a possibility there could be more than 3 ranges but I think it should be ok to tell my client that we need to decide on a fixed number.

I was getting errors in my code when I tried your earlier example. Unfortunately I haven't really had a chance to try debug it yet and anyway I was kind of waiting for your next response.

I will try your latest suggestion and see how I get on.

Thanks.

## 1/14/2011 8:11 am  |  #6 Jason ByrnesWebAssist

if you get errors using the calculation i provided, it is most likely because of case sensitivity.

If you name the columns using a different case than what I am using, you will get errors.

PHP is picky that way.

## Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

## Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

## Everything else!

Close Window

Account or customer service questions?

Need technical support?

Content

Layout

Ease of use

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Window

## We were unable to retrieve the attached file

Close Window

### Attach and remove files

• Text Formatting
• Insert Element
• Block Formatting
• Cancel
Close Window

Close Window

#### This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --