1

所以,这很长,在此先抱歉,希望你能尽力帮助我,

它是这样的:

我制作了一个自定义购物车,并且我将集成一个结帐(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不同的items_id)!所以它只显示购物车中的 1 件商品(最后添加了一件),所以总和是错误的,并且所有商品都没有在结帐中列出。

所以有人知道我做错了什么吗?

预先感谢。

4

1 回答 1

1

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

$cart = array(...)

您可能希望将每个条目添加到购物车数组

$cart[] = array(...)
于 2013-03-17T21:24:35.987 回答