简体   繁体   中英

Adding items to shopping cart based on number of rows in a form

I am looking for a way to link the number of rows in a form to items in a simple shopping cart/checkout script.

Here is the scenario. Users are booking places at a conference. There is a three tier pricing structure, and a section of the form for each price. When the user first navigates to the booking page he is asked to add the number of people he/she wishes to book into the conference at each pricing structure. Extra rows can be added to each section of the form using javascript. Once the names/details of each person being booked in has been completed the user clicks on a 'Submit' button which submits all of the information to a MySQL Database and redirects the user to a payment page.

I would like this payment page to consist of a simple shopping cart/checkout which is automatically filled with the number of people at each rate, with the total for each rate, and a grand total. The user could then just click on a button to complete the PayPal payment.

So the parts I am stuck on is how to automatically pass the information from the forms (ie the number of rows (people) at each rate) to the 'checkout' page, and which script I might use to generate the checkout page.

I'd be grateful for any help to get this working,

Thanks,

Nick

I'd use a POST array such that, when each row is added it uses something similar to the format:

<input name='tickets[][name]' />
<input name='tickets[][email]' />

Then on the PHP side, we can simply count up the number of tickets and do pricing based on the array that is generated via $_POST

<?php

$tiers = array(
    array(
        'num_people' => 3,
        'price'      => 50.00,
    ),
    array(
        'num_people' => 10,
        'price'      => 20.00,
    ),
    array(
        'num_people' => 50,
        'price'      => 10.00,
    )
}

$price = $tiers[0]['price'];

$num_people = count($_POST['tickets']);

for($i=count($tiers); $i>=0; $i--){
    if ($num_people >= $tiers[$i]['num_people']) {
        $pricing = $tiers[$i]['pricing'];
        break;
    }
}

$total = $price * $num_people;

On the javascript side, you could expose a web service that calculates the rate and returns the details via ajax or some such.

On the final page, you just take the results of these calculations (stored in a session or against the order in the database) and print it out in your view.

It sounds like you have the adding of users handled, so let me touch on submitting the shopping cart. I did something similar, but created a PHP function to handle the addition of each item, which essentially echoed each product (desc, price, etc) inside a form, then simply submitted this form at the proper time to PayPal.

function FormatPaypal($iItemno, $sItemname, $iQty, $fPrice, $fDiscountAmount, $sCode)
{
    global $iUser;
$s1 = sprintf('<input type="hidden" name="item_name_%d" value="%s">', $iItemno, $sItemname); 
$s2 = sprintf('<input type="hidden" name="amount_%d" value="%.2f"> ', 
$iItemno, $fPrice); 
$s3 = sprintf('<input type="hidden" name="quantity_%d" value="%d"> ', 
$iItemno, $iQty); 

$s4 = sprintf('<input type="hidden" name="discount_amount_%d" value="%.2f">', $iItemno, $fDiscountAmount); 

$sCode = sprintf("%s-%d", $sCode, $iUser );
$s5 = sprintf('<input type="hidden" name="item_number_%d" value="%s">', $iItemno, $sCode); 

$sReturn = $s1.$s2.$s3.($fDiscountAmount ?  $s4 : '').$s5;
echo $s5;

return $sReturn;
}

which was placed inside a standard PayPal form:

<form action="https://www.paypal.com/cgi-bin/webscr" method="post" name="frmPaypal"> <input type="hidden" name="cmd" value="_cart">

Using this method I only have to increment the $iItemNo with each use. I am currently building a PHP class to simplify this even further by managing the items and total item count, if interested I'll try to remember to come back and share it here.

a variation on Josh's idea.

I'd use parallel arrays.

Josh also seems to base pricing on the number of people (group discounts)... which is not my understanding of the problem, instead you want to offer a differentiated pricing structure, correct?

<input name='tickets[][name]' />
<input name='tickets[][email]' />
<input name='tickets[][rate]' />


$rates = array(
    1 => 10,
    2 => 50,
    3 => 200,
    ...  // more as needed
);

$totals = array('all' => 0);
$people = array();

foreach ($rates as $rate) {
    $totals[$rate] = 0;
    $people[$rate] = 0;
}

foreach ($tickets as $ticket) {
    $totals['all'] += $rates[$ticket['rate']];
    $totals[$ticket['rate']] += $rates[$ticket['rate']];
    $people[$ticket['rate']]++;
}


foreach ($rates as $rate => $price) {
    print "{$totals[$rate]}";
    print "{$people[$rate]} people X $ {$price} = {$totals[$rate]}";
}

print "total = {$totals['all']} ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM