First of all, yes, I checked other options, questions and answers, but, unfortunately, this did not work for me.
So, I am currently working on a small project, including a shopping cart system. The shopping cart system is created by a PHP session and everything works fine. Products can be added, rows can be removed, and the cart can be completely cleaned.
Now I would like to add some functions, such as counting items in the basket, calculating the total price and VAT prices. Since the session is stored as a JSON object, I can use angularjs
both $http.get()
to read the cart object JSON
and override the data so that it can be used in ng-repeat
for presentation.
So, when the visitor clicks the add to cart button, the following code creates a session:
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);
$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
);
if(empty($_SESSION['cart']["cartItems"])){
$_SESSION['cart']["cartItems"] = array();
}
if(array_key_exists($product_id and $product_size, $_SESSION['cart'])){
echo "<script> alert('Dit product staat al in de winkelwagen')</script>])";
}
else{
$_SESSION['cart']["cartItems"][$product_id]=$cartProduct;
}
So, my first attempt was to add to the section "add an element to an array"
$arr = json_decode($_SESSION['cart']['cartItems'][$product_id], true);
$total_items['total_items'] = count($_SESSION['cart']);
array_push($arr['cartItems'], $total_items);
But unfortunately, this did not work. I also tried adding this to the getCart section.
session_start();
$json = json_encode($_SESSION['cart']["cartItems"]);
echo($json);
Unfortunately, without any results.
So my question is how can I add the calculation of total_items to the array. And how can I calculate the total price, VAT prices, etc.?
PS: single product JSON result:
{"16":{"id":"16","name":"TestDatumProduct","price":1000,"size":"M","quantity":"4","total_product_price":4000,"total_items":0}}
UPDATE
So, an update to combine the progress of the last few days:
So now my code is based on @ devionNL's answer, since I like the action method, and @ FranciscoRodríguez's answer was hardly the answer as it only advised me on the counting method (didn't work)
, @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);
$cartProduct = array(
"id" => $product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => $product_quantity,
"total_product_price" => $total_product_price
);
if(empty($_SESSION['cart']["cartItems"])){
$_SESSION['cart']["cartItems"] = array();
}
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 );
}
}
else if ($_REQUEST['action'] == 'removeRow')
{
if (array_key_exists($product_id, $_SESSION['cart']['cartItems']))
{
if ($_SESSION['cart']['cartItems']['totalItems'] > 1)
{
foreach ($cartDecode as $key => $cartItem)
{
if ($cartItem->{"id"} == $cartItemID)
{
unset($cartDecode[$key]);
}
}
$_SESSION['cart']['cartItems']['totalItems']--;
}
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
. JSON
:
{"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
:
{"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. (ID == "", Then delete
)
2. ?
3. dabatase, ? (id, ..). , foreach()
?
- , .
, !