まずは他のオプション、質問と回答をチェックしましたが、悲しいことにそれらは私にとってはうまくいきませんでした。JSONファイルを編集する
私は現在、ショッピングカートシステムを含む小さなプロジェクトに取り組んでいます。 ショッピングカートシステムはPHPセッションによって作成され、すべて正常に動作します。商品を追加したり、行を削除したり、カートを完全にクリアすることができます。
ここでは、カート内のアイテムの数をカウントし、合計価格と付加価値税の価格を計算するなどの機能を追加したいと考えています。セッションはJSONオブジェクトとして保存されているので、angularjs
と$http.get()
を使用してJSON
カートオブジェクトを読み取り、データを再定義して、ビュー用にng-repeat
に使用できるようにします。
訪問者は、「買い物かごに入れる」ボタンをクリックしたときに、次のコードは、セッションを作成します
session_start();
$product_id = $_REQUEST['product_id'];
$product_name = $_REQUEST['product_name'];
$product_price = round($_REQUEST['product_price'], 2);
$product_size = $_REQUEST['product_size'];
$product_quantity = $_REQUEST['product_quantity'];
$total_product_price = $product_price*$product_quantity;
round($total_product_price, 2);
// Add new item to session
$cartProduct = array(
"id" => $product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => $product_quantity,
"total_product_price" => $total_product_price,
"total_items" => 0
);
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if(empty($_SESSION['cart']["cartItems"])){
$_SESSION['cart']["cartItems"] = array();
}
// check if the item is in the array, if it is, do not add
if(array_key_exists($product_id and $product_size, $_SESSION['cart'])){
// redirect to product list and tell the user it was added to cart
echo "<script> alert('Dit product staat al in de winkelwagen')</script>])";
}
// else, add the item to the array
else{
$_SESSION['cart']["cartItems"][$product_id]=$cartProduct;
}
だから私の最初の試みは、セクションに以下を追加することでしたが、「に項目を追加します配列 "
$arr = json_decode($_SESSION['cart']['cartItems'][$product_id], true);
$total_items['total_items'] = count($_SESSION['cart']);
array_push($arr['cartItems'], $total_items);
しかし、残念ながらこれはうまくいきませんでした。これをgetCartセクションに追加しようとしました。
session_start();
$json = json_encode($_SESSION['cart']["cartItems"]);
echo($json);
悲しいことにも結果はありません。
私の質問は、どのようにtotal_items計算を配列に追加できるかということです。そして、私はどのように総価格、付加価値税の価格などを計算することができますか?
PS:1、製品のJSONの結果:
だから、現在、私のコードは基づいています。最後の数日間の進捗状況を結合するので、更新
{"16":{"id":"16","name":"TestDatumProduct","price":1000,"size":"M","quantity":"4","total_product_price":4000,"total_items":0}}
UPDATE
私はアクションメソッドが好きで、@FranciscoRodríguezの答えは彼が計数方法について私にアドバイスして以来ほとんど役に立たなかったので、@devionNLの答えには、(仕事しなかった)
@devionNLのアクション例を使用して、私はいくつかの小さな変更を行い、次のコードを思いつきました。
<?php
session_start();
$cartItemID = $_REQUEST['cartItem_id'];
$product_id = $_REQUEST['product_id'];
$product_name = $_REQUEST['product_name'];
$product_price = round($_REQUEST['product_price'], 2);
$product_size = $_REQUEST['product_size'];
$product_quantity = $_REQUEST['product_quantity'];
$total_product_price = $product_price*$product_quantity;
round($total_product_price, 2);
// Add new item to session
$cartProduct = array(
"id" => $product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => $product_quantity,
"total_product_price" => $total_product_price
);
// If the session is empty create an empty array
if(empty($_SESSION['cart']["cartItems"])){
$_SESSION['cart']["cartItems"] = array();
}
// Add to cart
if ($_REQUEST['action'] == 'addToCart')
{
if (array_key_exists($product_id, $_SESSION['cart']['cartItems']))
{
$_SESSION['cart']['cartItems']['totalItems']++;
}
else
{
$_SESSION['cart']['cartItems']['totalItems']++;
array_push($_SESSION['cart']['cartItems'], $cartProduct);
}
}
// RemoveRow
else if ($_REQUEST['action'] == 'removeRow') // If you want a delete action
{
if (array_key_exists($product_id, $_SESSION['cart']['cartItems']))
{
if ($_SESSION['cart']['cartItems']['totalItems'] > 1)
{
foreach ($cartDecode as $key => $cartItem)
{
// IF THE TITLE MATCHES THE SIGNAL STRING
if ($cartItem->{"id"} == $cartItemID)
{
// REMOVE THIS OBJECT
unset($cartDecode[$key]);
}
}
$_SESSION['cart']['cartItems']['totalItems']--; // Deduct by 1.
}
else
{
$_SESSION['cart']['cartItems'] = $cartProduct;
}
}
$_SESSION['cart']['cartItems']['totalPrice'] = array_sum(array_map(function($item) {
return $item['price'] * $item['totalItems'];
},
$_SESSION['cart']['cartItems']));
}
$cart = json_encode($_SESSION['cart']['cartItems']);
echo ($cart);
したがって、カートに追加してcartItemsを計算することは、期待通りに機能しています。しかし、私はまだ行を削除する方法を見つけることができません。カート内。それに私はまだcart
の合計価格を得ることに苦労しています。
{"totalItems":2,"0":{"id":"7","name":"Bedrukt jurkje van chiffon","price":20.5,"size":"M","quantity":"3","total_product_price":61.5},"1":{"id":"5","name":"Bedrukte Zomerjurk","price":30.5,"size":"M","quantity":"3","total_product_price":91.5}}
JSON
で完全なカートは次のようになります:以下のようJSON
結果がある
{"cartItems":{"totalItems":2,"0":{"id":"7","name":"Bedrukt jurkje van chiffon","price":20.5,"size":"M","quantity":"3","total_product_price":61.5},"1":{"id":"5","name":"Bedrukte Zomerjurk","price":30.5,"size":"M","quantity":"3","total_product_price":91.5}}}
だから私の質問は何ですか? 1.どのように1行を削除することができますか(ID == "", Then delete
) 2.カートの全額をどのように計算できますか? 3.ダバターゼの中に注文するには、どのように各行を選択することができますか? (ID、名前など)。私はforeach()
声明を使う方法を考えていましたか?
ご不明な点がございましたら、コメントにお尋ねください。
いつもありがとうございます!一見
'$ total_items ['total_items']'の値は何ですか?それが有効な値を持っている場合は、ループを使用してarray_push()を実行してください。 –
@ RedBottle JSONファイルのtotal_itemsの値は0に設定されています。配列のプッシュをセッション数に設定します。はい、カウントが機能しているかどうかを確認しました。 – Deathstorm
jsonは文字列なので、$ _SESSION ['cart'] ['cartItems'] [$ product_id] 'または' $ _SESSION ['cart'] ["cartItems"] 'にjson文字列を正確に入れていますか?または、カート全体がjsonとして保管されていますか? – jeroen