javascript - 在现有 session 中创建计算

标签 javascript php angularjs session

我目前正在使用 AngularJS、PHP、Web 服务以及一些 html/css 和 XML 开发一个购物应用程序。

我目前正在处理购物车 session ,在创建总价、总商品数和增值税计算时偶然发现了一些问题。

所以 session 是用 PHP 编写的。代码如下:

<?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;
}

正如上面的代码所示,请求有关产品的所有信息。然后将请求的项目(例如 product_id)设置到 array() 中。经过一些检查后,array() 被添加到 session 中:$_SESSION['cart'];。我的添加和显示购物车没有任何问题。我遇到的问题是创建显示购物车中的total_items、购物车的total_price 和增值税计算所需的计算。

如果需要更多信息,请告诉我。

提前致谢!

最佳答案

查看您的代码,这就是我看到的问题

$total_items = $_SESSION['cart'][$product_quantity];

因为

  $_SESSION['cart'][$product_id]=$cartProduct;

还有

  $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
);

因此,如果您的 $product_quantity3,那么您将把 $product_id '3' 拉出,这是没有意义的。因为例如,如果我们有 $product_id6$product_quantity3 那么:

$_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
);

那么为什么我们要提取数量而不是产品 ID,请遵循。

这里唯一可以代替$_SESSION['cart'][$product_quantity]的是

$total_items = $_SESSION['cart'][$product_id]['total_items'];

但我认为您可能还有其他问题,因为 $product_quantity$total_items 之间的目的或区别是什么?总项目不就是产品的数量吗?或者是产品总数(唯一产品 ID 的数量)和该特定产品的数量(一个产品 ID 的数量)的 1。如果是这种情况,则无需跟踪每个产品中所有产品的总数,而且容易出错。

您还可以在将产品分配到购物车之前访问 session ,此处

 $total_items = $_SESSION['cart'][$product_quantity];

这也可能是错误的

 array_key_exists($product_id and $product_size, $_SESSION['cart'])

因为我在测试类似的东西时得到了这个

Warning: array_key_exists(): The first argument should be either a string or an integer

这可能是因为 $product_id 和 $product_size 在进行 bool 比较时计算结果为 true

只是我的评估。

关于javascript - 在现有 session 中创建计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135462/

相关文章:

PHP 未在构建时正确链接 MySQL 共享库

javascript - AngularJS 问题仅允许在文本框中输入数字

javascript - 我如何使用 Lodash 实现这一目标?

javascript - 动态更改厚框弹出窗口大小

javascript - 在 angular service.ts 中使用谷歌的 api.js

javascript - angularjs ngOptions 有 2 个对象属性作为标签并通过 Id 进行跟踪

javascript - Angular 选择器问题

c# - 使用发布数据填充部分 View

php - Laravel TokenMismatchExceptionVerifyCsrfToken.php :67 find route

php - 测试 PDO 查询,如果失败,则执行其他操作