简体   繁体   English

如何在会话中放入购物车总额

[英]How to put a shopping cart total in a session

I am building a shopping cart using sessions. 我正在使用会话建立购物车。 How do I put the total amount in a session such that it changes as the user adds or removes from the shopping cart. 如何将总金额放入会话中,以便随着用户添加或从购物车中删除而改变总金额。 This is the function I use in getting my total price: 这是我用来获取总价的功能:

public function grand_total($grand_total, $coupon){
    if ($coupon != 0){
      $discount = $grand_total * 0.1;
      $actual = $grand_total - $discount;
      return $actual;
    }else{
      return $grand_total;
    }
  }

On cart.php, where all items being bought are shown, I use this to display the total: 在cart.php上,其中显示了所有正在购买的物品,我用它来显示总数:

grand_total($grand_total, $discount);

How do I put this total in session such that it can be accessible in any page and also changes as items are added or removed? 我该如何在会话中添加此总计,以便可以在任何页面中访问它,并且随着添加或删除项目而更改?

Thank you. 谢谢。

You can access session using $_SESSION. 您可以使用$ _SESSION访问会话。 So in your case, you can have 因此,就您而言,您可以

$_SESSION['grand_total'] = grand_total($grand_total, $discount)

whenever the grand total is updated. 每当总计更新时。

And anywhere you want to display the grand total, just reference $_SESSION['grand_total'] , like 在任何要显示总计的地方,只需引用$_SESSION['grand_total'] ,例如

<?php echo $_SESSION['grand_total'] ? $_SESSION['grand_total'] : 0; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM