This is a read-only archive. No new posts or registrations. Privacy Page Still need help? Ask the volunteers at Geeks to Go, free.
Software

Adding a Service Charge to Shopping Cart using PHP

1 min read

This thread's last reply is from . Advice, software, and links below may be out of date — treat specific steps and download links with caution.

Hi there,

Today I created a shopping cart following the tutorial on YouTube from phpacademy.

If you take a look here, you can see exactly what my (phpacademy's) shopping cart does.

I want to know how you could add a service charge to this shopping cart.

So when the users click PayPal Checkout, the receipt will show up with the users items + a service charge.

Could anyone help me out on this?
Hello GaryORLFC, Without seeing the source code I can't tell you exactly how to implement this, but if you're using the usual HTML form method for PayPal you can just add the service charge to the "amount" field. If you post your code in the code tags, I can have a proper look at it for you. ^_^
Hi Nahumi,

Thanks for the reply.

Here is the code for the Shopping Cart


session_start();

$page = 'index2.php';

$connect = mysql_connect("localhost","organis2_admin","dublin") or die("Could not connect to the database.");
mysql_selectdb("organis2_project") or die("Could not find the database.");

if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM shoppingcart WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['quantity_'.(int)$_GET['add']]) {
$_SESSION['quantity_'.(int)$_GET['add']]+='1';
}
}
header('Location: '.$page);
}

if (isset($_GET['remove'])) {
$_SESSION['quantity_'.(int)$_GET['remove']]–;
header('Location:'.$page);
}

if (isset($_GET['delete'])) {
$_SESSION['quantity_'.(int)$_GET['delete']]='0';
header('Location:'.$page);
}

function products(){
$get = mysql_query('SELECT id, product, description, price FROM shoppingcart WHERE quantity > 0 AND id BETWEEN 1 AND 2 ORDER BY id DESC');
if (mysql_num_rows($get)==0){
echo "There are no products to display.";
}
else{
while($get_row = mysql_fetch_assoc($get)){
echo '

'.$get_row['product'].'
'.$get_row['description'].'
€'.number_format($get_row['price'], 2).'Add

';

}
}
}

function products1(){
$get = mysql_query('SELECT id, product, description, price FROM shoppingcart WHERE quantity > 0 AND id BETWEEN 3 AND 5 ORDER BY id DESC');
if (mysql_num_rows($get)==0){
echo "There are no products to display.";
}
else{
while($get_row = mysql_fetch_assoc($get)){
echo '

'.$get_row['product'].'
'.$get_row['description'].'
€'.number_format($get_row['price'], 2).'Add

';

}
}
}

function paypal_products() {
$num = 0;
foreach($_SESSION as $product => $value) {
if ($value!=0) {
if (substr($product, 0, 9)=='quantity_') {
$id = substr($product, 9, (strlen($product)-9));
$get = mysql_query('SELECT id, product, price FROM shoppingcart WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$num++;
echo '';
echo '';
echo '';
echo '';
echo '';
}
}
}
}
}


function cart() {
$sub="";
$total="";
foreach($_SESSION as $product => $value) {
if ($value>0) {
if (substr($product, 0, 9)=='quantity_') {
$id = substr($product, 9, (strlen($product)-9));
$get = mysql_query('SELECT id, product, price FROM shoppingcart WHERE id='.mysql_real_escape_string((int)$id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo $get_row['product'].' x '.$value.' @ €'.number_format($get_row['price'], 2).' = €'.number_format($sub, 2).'[-] [+] [Delete]
';
}
}
$total += $sub;
}
}
if ($total==0) {
echo "Your cart is empty.";
}
else {
echo 'Total: €'.number_format($total, 2);
?>


method="post">






name="submit" alt="Make payments with PayPal - it's fast, free and secure!">




}
}

?>

Also, could someone help me out with the quantity part of the code? I want to be able to type in the number of items that I want instead of having to click [+] a certain amount of times. So, a form data that lets you input, let's say 7 of a specific item, and then when you click "Add to Cart" it will add 7 to the cart.

Still having this problem?

This topic is from the What the Tech archive and can't take new replies. Geeks to Go is an active, free help community run by volunteers. Post your question there and a person, not a bot, will work through it with you.

Ask on Geeks to Go →