根据表单中的行数将商品添加到购物车

发布于 2024-11-07 09:29:22 字数 432 浏览 0 评论 0原文

我正在寻找一种将表单中的行数链接到简单购物车/结账脚本中的项目的方法。

这是场景。用户正在预订会议席位。有一个三层定价结构,每个价格都有一个表格部分。当用户第一次导航到预订页面时,系统会要求他添加他/她希望在每个定价结构下预订参加会议的人数。可以使用 JavaScript 将额外的行添加到表单的每个部分。完成每个预订人员的姓名/详细信息后,用户单击“提交”按钮,将所有信息提交到 MySQL 数据库并将用户重定向到付款页面。

我希望这个支付页面包含一个简单的购物车/结帐,它会自动填充每个费率的人数、每个费率的总数以及总计。然后,用户只需单击按钮即可完成 PayPal 付款。

因此,我所关注的部分是如何自动将信息从表单(即每个速率的行数(人))传递到“结帐”页面,以及我可能使用哪个脚本来生成结帐页面。

我将不胜感激任何帮助让这个工作,

谢谢,

尼克

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 (i.e. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木森分化 2024-11-14 09:29:22

我会使用 POST 数组,这样,当添加每一行时,它会使用类似于以下格式的内容:

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

然后在 PHP 方面,我们可以简单地计算门票数量,并根据通过 $ 生成的数组进行定价_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;

在 javascript 方面,您可以公开一个 Web 服务来计算费率并通过 ajax 等返回详细信息。

在最后一页上,您只需获取这些计算的结果(存储在会话中或根据数据库中的顺序)并将其打印在视图中。

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.

简美 2024-11-14 09:29:22

听起来您已经处理了用户的添加,所以让我谈谈提交购物车。我做了类似的事情,但创建了一个 PHP 函数来处理每个项目的添加,这基本上在表单中回显了每个产品(描述、价格等),然后只需在适当的时间将该表单提交给 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;
}

它被放置在标准 PayPal 表单中:

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

使用此方法,我只需在每次使用时增加 $iItemNo 。我目前正在构建一个 PHP 类,通过管理项目和项目总数来进一步简化这一过程,如果有兴趣,我会尽量记得回来并在这里分享。

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.

很快妥协 2024-11-14 09:29:22

乔什想法的一个变种。

我会使用并行数组。

乔什似乎还根据人数定价(团体折扣)......
这不是我对问题的理解,相反,您想提供差异化​​的定价结构,对吗?

<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']} ";

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']} ";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文