Jump to content

Add discount depending on the product:


roldahayes
Go to solution Solved by mac_gyver,

Recommended Posts

Yeah mate I know the feeling, we have been cranking this for a couple of days lol. Need to look at the logic and follow it through, think it is almost there, check your sums and see if they are out putting correct and echo them in different places in your code to see if changes occured. See how you go, give it a good crack and post again if you have not got a resolve.

Link to comment
Share on other sites

sorry to jump in here, but your code is all over the place, difficult to follow, and contains several things that just shouldn't be done. some examples are running three select queries against your basket table to decide if you are going to insert a new item; using  htmlspecialchars() all over the place, not just on values you are outputting into the html on the page; running the postage query inside of a loop (you should just get all the cart's 'Post_ID' values and run one query to get the lowest postage rate); hard-coding these discount comparisons and values into the code; the whole `shopper` table (you should not store calculated/derived data.)

 

for your discount rate, assuming this is dynamic and could change for any product_make, it should be stored in a database table (or an array) that holds the non-zero discount and its corresponding product_make (there's no point in storing the zero discounts as that will be the default action of the code.)

 

based on your 'full code' in post #18, you need to first calculate the cost (price * quantity) of the product and store that in a php variable, so that you can calculate the discount (if any) for that product, BEFORE you sum the item cost with the running total. your logic is currently summing the item cost into the running total without the discount, then figuring the discount using the running total.

Edited by mac_gyver
Link to comment
Share on other sites

i have a question about the values you are producing/displaying. which of - Price ex vat, Price inc vat, Discount on this item (-), or Total are unit amounts and which are multiplied by the quantity?

 

also, i'm guessing the 1.2 value that showed up in the math has to do with the VAT? the only place you should have that value is in your calcVAT() fucntion.

 

after looking at your code, you need to individually get/calculate the correct Price ex vat, Price inc vat, Discount on this item (-), and the Total as you are looping over the rows in the cart, storing each of those values in its own variable and only sum the correct one of those values with the running sub-total.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

the logic (i think) you need would look like -

$counter = 1; // for identifying each form
$price = 0; // running sub-total

$discount_table['SONY'] = 15/100;
// add more entries to this table as needed, you would normally store this in a file and include it or use a database table
    
while ($row = mysql_fetch_assoc($result))
{
    $qty = $row['quantity']; // quantity of this item
    $price_inc_vat = calcVAT($row['Price_ExVat']); // price including vat for this item
    
    // determine discount percentage, if any
    $i = $row["Prod_Make"];
    $percentage = 0; // default (i.e. none)
    if(isset($discount_table[$i])){
        $percentage = $discount_table[$i]; // get the defined percentage for this make
    }

    $discount = $price_inc_vat * $percentage; // calculate discount amount (zero if no discount for this make)
    $amount = $price_inc_vat - $discount; // net amount
    $total = $qty * $amount; // total for this item
    $price = $price + $total; // add to running sub-total
    
    // use the available values to output your form -

    // ... your form code ...
    
    $counter ++;
} // end of the while loop

 

Link to comment
Share on other sites

Hi Mac_Gyver,

 

This is code that I have inherited to work with.  I know that i really should be using the database to work with things dynamically but this really only needs a short term solution to sell of some stock.

 

The column that needs to be multiplyed is the "Total" column.

 

After using your above code, I have got things working perfectly in each row.

 

The basket knows what products have discount, displays the correct amout of discount and then deducts that to show at the end of the row.

 

The only part I now need to solve is the SUBTOTAL

 

$subtotal = ???

 

How do I get it to add all the rows together?

Link to comment
Share on other sites

Somewhere along the line here I seem to have broken the postage.  Everything seems to be coming in at £0.00 post even when it should be there....

 

what troubleshooting have you done to find the problem?

 

this thread seems to be concerned with adding/modifying the contents of the cart and displaying the cart. unless you are offering a flat shipping fee, it would take knowing where you are shipping the items to before you can determine the shipping cost.

 

most shopping carts have a 'check out' phase, where you finalize the order and gather (or let someone log in with an existing account, if not already logged in) information about where the order is being shipped to, then you finally let them goto the payment processor page to pay for the items in the order.

 

the code you have shown in this thread for postage, in addition to what i have already stated about it, is nonsense. you set an initial postagerate (15.00), then you get and check the Post_Cost for each item in the cart. if the Post_Cost for an item is lower than the current postagerate, you use the lower value as the current postagerate. if the Post_Cost is equal to or greater than the current postagerate, you set the current postagerate to zero (you also set the postagerate to zero if there is no Post_Cost for an item.) your comments in the postage code are also contradictory. at one point you state - 'decide which postage value is the highest' and in the actual code - 'get the lowest postage rate.'

 

before you can try to fix your postage code, you need to define the logic you want, because it's impossible to write code when you don't know what end result that code is supposed to produce.

Link to comment
Share on other sites

I realise the code is a bit of a mess, as I said it was inherited for me to work with.

 

The postage should only ever display the lowest postage of all the items.

 

My original problem about adding discount is now working correctly so I'll mark this as solved and then start again to sort out the postage issue.

 

Many thanks for all your help with this.

 

Regards,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.