我有一个嵌套数组的数组,如下所示。
cart (
[total] => 98
[itemcount] => 3
[items] => Array ( [0] => 0 [1] => 3 [2] => 5 )
[itemprices] => Array ( [0] => 33.00 [3] => 32.00 [5] => 33 )
[itemqtys] => Array ( [0] => 1 [3] => 1 [5] => 1 )
[iteminfo] => Array ( [0] => Chemistry [3] => Additional Mathematics [5] => Physics )
)
当用户输入正确的折扣代码时,我在交换机中有以下代码,此功能将触发。我可以让它显示折扣价。
$anewvalue = 16.50;
$physubject = "Physics";
$index = array_search($physubject , $cart->iteminfo);
if (false !== $index) {
$cart->itemprices[$index] = $anewvalue;}
问题是,当用户键入正确的折扣代码并提交表单时,如何更新总计,以便总价格始终是最新的,因为现在它不会这样做。
答案 0 :(得分:0)
每次应用正确的折扣时,您需要重新计算总数。
if (false !== $index) {
$cart->itemprices[$index] = $anewvalue;
//re-calculate the total
$total = 0;
foreach ($cart->itemprices as $key=>$itemprice) {
$total += $itemprice * $cart->itemqtys[$key];
}
$cart->total = $total
}