0

我正在使用 CODEIGNITER 购物车类。以下是VAR_DUMP购物车内容。我有一个单选字段,可让您选择一个SHIP选项。问题是我的 PHP 没有获取SHIP值,而只返回购物车值中的最后一项并乘以购物车项目的总数。如何SHIP从每个项目中获取数组并对值求和?

PHP

$ship_array = $this->cart->ship($item['rowid']); 
foreach ($ship_array as $index => $ship_option){
        #SHIPPING CALC
        
        $ship_total = 0;
        
        foreach ($cart as $item)
        {
         $ship_total += $ship_option;
        }
        
echo '<label><input type="radio" name="print" class="option" data-number="'. $ship_total .'" value="'. $index .'"/> UPS '. $index .' </label> '; }

^此代码只会从ship购物车中的最后一项中获取值。结果将显示ground运费为 43.35 美元(14.45(陆运价值 * 3 件总物品)

显然这是错误的,我需要添加每个的运费值$item

VAR_DUMP 数组(购物车中的所有商品)

array (size=3)
  '554ed09a5f80917741359cc9da50a75c' => 
    array (size=8)
      'rowid' => string '554ed09a5f80917741359cc9da50a75c' (length=32)
      'id' => string '101' (length=3)
      'qty' => string '1' (length=1)
      'price' => string '112.5' (length=5)
      'name' => string 'Business Card' (length=13)
      'ship' => 
        array (size=3)
          'Ground' => float 9.73
          '2nd Day Air' => float 18.54
          'Overnight' => float 26.27
      'options' => 
        array (size=2)
          'Print Package' => string 'Pro' (length=3)
          'Design Package' => string 'Premium' (length=7)
      'subtotal' => float 112.5

  '675d8a197a25e6720af7ac05707fee40' => 
    array (size=8)
      'rowid' => string '675d8a197a25e6720af7ac05707fee40' (length=32)
      'id' => string '102' (length=3)
      'qty' => string '1' (length=1)
      'price' => string '446' (length=3)
      'name' => string 'Booklet' (length=7)
      'ship' => 
        array (size=3)
          'Ground' => float 14.45
          '2nd Day Air' => float 22.57
          'Overnight' => float 34.12
      'options' => 
        array (size=2)
          'Print Package' => string 'Plus' (length=4)
          'Design Package' => string 'Ultimate' (length=8)
      'subtotal' => int 446

  'aea5d2f84151e65a0a1e50371aad26aa' => 
    array (size=8)
      'rowid' => string 'aea5d2f84151e65a0a1e50371aad26aa' (length=32)
      'id' => string '102' (length=3)
      'qty' => string '1' (length=1)
      'price' => string '325' (length=3)
      'name' => string 'Booklet' (length=7)
      'ship' => 
        array (size=3)
          'Ground' => float 14.45
          '2nd Day Air' => float 22.57
          'Overnight' => float 34.12
      'options' => 
        array (size=2)
          'Print Package' => string 'Pro' (length=3)
          'Design Package' => string 'Premium' (length=7)
      'subtotal' => int 325
4

2 回答 2

0

移动

$ship_total = 0;

超出第一个 foreach 循环。

编辑使用这个:

$ship_array = $this->cart->ship($item['rowid']); 
$ship_total = 0; //this moved
foreach ($ship_array as $index => $ship_option){
        #SHIPPING CALC

        foreach ($cart as $item)
        {
         $ship_total += $item['ship'][$ship_option] * $item['qtq']; //this changed
        }
        echo '<label><input type="radio" name="print" class="option" data-number="'. $ship_total .'" value="'. $index .'"/> UPS '. $index .' </label> ';
}
于 2012-10-08T17:04:04.747 回答
0

你可以试试

$array = array(     
'554ed09a5f80917741359cc9da50a75c' => array('rowid' => '554ed09a5f80917741359cc9da50a75c','id' => '101','qty' => '1','price' => '112.5','name' => 'Business Card','ship' => array('Ground' => 9.73,'2nd Day Air' => 18.54,'Overnight' => 26.27),'options' => array('Pr Package' => 'Pro','Design Package' => 'Premium','subtotal' => 112.5)),
'675d8a197a25e6720af7ac05707fee40' => array('rowid' => '675d8a197a25e6720af7ac05707fee40','id' => '102','qty' => '1','price' => '446','name' => 'Booklet','ship' => array('Ground' => 14.45,'2nd Day Air' => 22.57,'Overnight' => 34.12),'options' => array('Pr Package' => 'Plus','Design Package' => 'Ultimate','subtotal' => 446)),
'aea5d2f84151e65a0a1e50371aad26aa' => array('rowid' => 'aea5d2f84151e65a0a1e50371aad26aa','id' => '102','qty' => '1','price' => '325','name' => 'Booklet','ship' => array('Ground' => 14.45,'2nd Day Air' => 22.57,'Overnight' => 34.12),'options' => array('Pr Package' => 'Pro','Design Package' => 'Premium','subtotal' => 325)));


$shipArray = $array ; // Replace with $this->cart->ship($item['rowid']);
$shipTotal = array("prize"=>0,"ground"=>0,"air"=>0,"overnight"=>"0");
foreach ($shipArray as $index => $shipOption){
    $shipTotal['prize'] += $shipOption['qty'] * $shipOption['price'];
    $shipTotal['ground'] += $shipOption['ship']['Ground'];
    $shipTotal['air'] += $shipOption['ship']['2nd Day Air'];
    $shipTotal['overnight'] += $shipOption['ship']['Overnight'];
}

var_dump($shipTotal);

输出

array
  'prize' => float 883.5
  'ground' => float 38.63
  'air' => float 63.68
  'overnight' => float 94.51
于 2012-10-08T17:06:39.320 回答