SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Applying Discount across all line items in shopping cart

    Let's say you have a shopping cart with 3 items, $100, $20, $5 respectively. Now let's say you are receiving a discount of $24 off the order total. Behind the scenes you need to apply this discount across all of the individual line items. What would be the best method of doing this?

    The following works assuming that the discount / number of items is less than any of the item prices. The code is sticking each line item's discount in an array...

    PHP Code:
    $perItem = (float) $discount / (float) $numberOfItems;

    if (
    $perItem 100 - (int)($perItem 100) > 0.09)
    {
        
    $discounts array_fill (0$numberOfItems 1sprintf ("%.2f"$perItem));
        
    $discounts[$numberOfItems 1] = sprintf ("%.2f", ($perItem .01));
        
    $discounts[0] = sprintf ("%.2f", ($perItem .01));
    }
    else
    {
        
    $discounts array_fill (0$numberOfItemssprintf ("%.2f"$perItem));

    However I can't assume the discount / number of items will always be less than every item price. The only other method I can think of is doing a while loop, subtracting $1 off each line item until the discount is used up (with checks to verify item prices are > 1 and if not skip it and go to the next line item). This method doesn't seem efficient though if several orders were being placed on a site at the same time. Is there any better way to do this that I'm just currently not thinking of, or is using a while loop to simply distribute the discount until it's used up the best way to go?
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are applying a flat discount rate for all items, why not just loop through your shopping cart and apply that flat discount to each line item ?
    PHP Code:
    $sale_price $price .808
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it's not a flat rate. My example above was just an example for clarification purposes. Basically you have a random discount amount and a variable number of items in your cart. The discount HAS to be applied to each of the line items. So if you are receiving 10% off the entire order total, whatever 10% of the total is has to be divied up amongst all line items.

    Order Total: $100
    # Items: 2
    Item1: $98
    Item2: $2
    Discount: 10% = $10

    I have to take that and divy it between both line items. This example is fairly basic, but if you assume for example you have 50 items in a cart and your discount is $1000....

    What's the best way to distribute the discount amount up amongst all items in the cart? As I said in my original post, the only way I can think of doing it is with a for loop inside a while loop. It works but I'm only concerned about whether or not this is the best and most efficient way to accomplish it.
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    10% / item.
    Figure out what your random discount is going to be and then multiply the price times the reciprocal.
    In other words if you extend a 10% discount to fifty items, loop through your shopping cart and multiply each items price by .9 to get the discounted price.
    Fairly simple math.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're not understanding what I'm asking. I can do math....

    Order Total = $100
    Discount = $10

    Item1 = $96
    Item2 = $4

    I need to divide the $10 discount between both items in the cart. I'm not asking how to calculate discounts or percents... I'm asking how to take a known discount and divide it amongst every item. In the example above I'd subtract $3 from Item2 and $7 from Item1. Basically divy up the discount between each item but make sure that the item's final price (after discount) is not less than $1.

    Code:

    PHP Code:
    while ($discount >= 1)
    {
        for (
    $i 0$i $numberOfItems$i++)
        {
            if (
    $discount <= 1) break;

            if (
    $item['total'] - $discounts[$i] - 1)
                continue;

            
    $discounts[$i] += 1;
            
    $discount -= 1;

            if (
    $discount <= 1) break;
        }
    }

    // any remaining change do the same as above as long as price > change + 0.01 
    I have code similar to that working but I want to know if there's a more efficient way to do this. Basically the final price for each item would be $items[$i] - $discounts[$i]
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand completely, you want to discount the order 10&#37;. So using that formula :
    Item 1 cost $96.00 * .9 = $86.40.
    Item 2 cost $4.00 * .9 = $3.60.
    The total price is $90.00.
    The discount is $10.00.
    Each item received the same amount of discount. You do not have to check to see if the amount subtracted makes the product less than zero and you do not have to worry that a customer like me that can do math will complain the discount received was not equal for each product.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •