i'm currently developing a shopping application with AngularJS, PHP, a webservice and some html/css and XML.
i'm currently working on the cart session and i've stumbled upon some problems creating the total price, total item count and VAT calculations.
So the session is written in PHP. The code is as following:
<?php
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);
$total_items = $_SESSION['cart'][$product_quantity];
// Add new item to session
$cartProduct = array(
"id" => $product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => $product_quantity,
"total_price" => $total_product_price,
"total_items" => $total_items
);
/*
* check if the 'cart' session array was created
* if it is NOT, create the 'cart' session array
*/
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = 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'][$product_id]=$cartProduct;
}
As the code above states all the information about the product is requested. the requested items like the product_id
are then set into an array()
. the array()
is, after some checks, added to a sessoion: $_SESSION['cart'];
. My there are no problems adding and showing the cart. The problem i'm having is with creating the calculations necessary to show the total_items in the cart, the total_price of the cart and the VAT calculation.
If more info is needed please let me know.
Thanks in advance!
</div>
Looking at your code this is the issue I see
$total_items = $_SESSION['cart'][$product_quantity];
Because
$_SESSION['cart'][$product_id]=$cartProduct;
And
$cartProduct = array(
"id" => $product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => $product_quantity,
"total_price" => $total_product_price,
"total_items" => $total_items
);
So if you have a $product_quantity
of 3
then you are pulling $product_id
'3' out which makes no sense. Because for example if we have $product_id
of 6
and $product_quantity
of 3
then:
$_SESSION['cart'][6] = array(
"id" => 6, //$product_id,
"name" => $product_name,
"price" => $product_price,
"size" => $product_size,
"quantity" => 3, //$product_quantity,
"total_price" => $total_product_price,
"total_items" => $total_items
);
So why would we pull out the quantity and not the product id, follow.
The only thing seasonable here in stead of$_SESSION['cart'][$product_quantity]
is
$total_items = $_SESSION['cart'][$product_id]['total_items'];
But I think you may also have other issues, because what is the purpose or difference between $product_quantity
and $total_items
? wouldn't the total items be the quantity of product. Or is one the number of total products ( count of unique Product IDs ), and one the number a that specific product ( qty of one Product ID ). If that is the case tracking the total number of all products in each product is unnecessary and error prone.
Your also accessing the session potentially before assigning the product to the cart, here
$total_items = $_SESSION['cart'][$product_quantity];
This is probably wrong too
array_key_exists($product_id and $product_size, $_SESSION['cart'])
AS I get this when testing something similar
Warning: array_key_exists(): The first argument should be either a string or an integer
This is probably because $product_id and $product_size
evaluates to true
from doing a Boolean comparison there.
Just my assessment.