购物车+结帐问题

时间:2013-03-17 17:39:58

标签: php arrays multidimensional-array integration shopping-cart

所以,这很长,很抱歉,希望你无论如何都会试着帮助我,

这就是:

我已经制作了一个自定义购物车,而且我将整合一个结账(klarna), 我的问题是我尝试使用json将项目发送到结帐:

 $sendInfo = base64_encode(json_encode($_SESSION['cart_array']));
 echo '<input type="hidden" name="info" id="info" value="'.$sendInfo.'"/>';


 And to pick it up i use another one:
 $info = (array) json_decode(base64_decode($_POST['info'])); var_dump($info);

给出了:

array (size=2)
 0 => 
object(stdClass)[1]
  public 'item_id' => string '7' (length=1)
  public 'quantity' => int 1
1 => 
object(stdClass)[2]
  public 'item_id' => string '3' (length=1)
  public 'quantity' => int 1

 So to build it further and to get the products out from the database:

 $cartOutput ="";
 $cartTotal ="";
 $cartTOTAL ="";
 $shipping ="";
 $include ="";


 if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
   $cartOutput = "<h2 align='center'>Din kundvagn är tom</h2>";
   } else {

//Starta for each loop
$i = 0; 
   foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM tblproducts WHERE idProducts='$item_id' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
        $product_name = $row["strName"];
        $price = $row["dbPrice"];
        $details = $row["strDescription"];
        $artnummer = $row["strArtnummer"];

    }

并将其放置在数组中以进行集成:

 $cart = array(
  array(
    'reference' => $artnummer,
    'name' => $product_name,
    'quantity' => $each_item['quantity'],
    'unit_price' => $price,
    'discount_rate' => 0,
    'tax_rate' => $tax,
   ),
   array(
    'type' => 'shipping_fee',
    'reference' => $artnummer,
    'name' => 'Shipping Fee',
    'quantity' => 1,
    'unit_price' => $shipping,
    'tax_rate' => $tax,
)
   );

 foreach($cart as $fieldarray){
   $body .= $fieldarray['reference'].' - '. $fieldarray['name'].' - '. $fieldarray['quantity'].' - '. $fieldarray['unit_price']; 
 }

我得到了数组:

  //$cart var_dump
 array (size=2)
 0 => 
array (size=6)
  'reference' => string '40719013' (length=8)
  'name' => string 'Socka 2 par/fp WS Cotton' (length=24)
  'quantity' => int 1
  'unit_price' => int 4800
  'discount_rate' => int 0
  'tax_rate' => int 2500
 1 => 
array (size=6)
  'type' => string 'shipping_fee' (length=12)
  'reference' => string '40719013' (length=8)
  'name' => string 'Shipping Fee' (length=12)
  'quantity' => int 1
  'unit_price' => int 14500
  'tax_rate' => int 2500

 // $body var_dump
  string '40719013 - Socka 2 par/fp WS Cotton - 1 - 480040719013 - Shipping Fee - 1 - 14500' (length=81)

到目前为止它工作正常,没有出现任何问题,如果我在购物车中添加数量然后走到收银台,它也可以正常工作,但有些人可能已经注意到第一个var_dump,我有两个项目在购物车(2个不同的商品_id)! 所以它只显示购物车中的1件商品(最后一件商品),所以总和是错误的,并且所有商品都没有在结账时列出..

所以谁知道我做错了什么?

事先谢谢。

1 个答案:

答案 0 :(得分:1)

每次设置$ cart-Array时,都会覆盖以前的所有条目:

$cart = array(...)

您可能希望将每个条目添加到cart-Array

$cart[] = array(...)