【问题标题】:Displaying the number of items, similar to a shopping cart显示商品数量,类似于购物车
【发布时间】:2018-01-18 20:16:09
【问题描述】:

我希望显示一个类似于购物车的数字。我得到了 php 代码,它当前显示一个 cookie 值,如果您单击添加到购物车并且您输入的内容有错误,它将向 cookie 添加 1,但不会向购物车添加任何内容。

我尝试使用 AngularJS 来显示会话变量的长度,效果更好一些,但在刷新页面之前它不会更新。

谁能指出我正确的方向,以便我自己尝试解决这个问题。

这是我最初得到的代码:

if (!isset($_COOKIE['count']))
{
    $cookie = 0;
    setcookie("count", $cookie);
}
else
{

    if (isset($_GET["add"]))
        {
        $cookie = ++$_COOKIE['count'];
        setcookie("count",$cookie);
        }
    else if (isset($_GET["remove"]))
        {
        $cookie = --$_COOKIE['count'];
        setcookie("count", $cookie);
        }
    else {
        $cookie = $_COOKIE['count'];
        setcookie("count", $cookie);
        }

    $cookie = $_COOKIE['count'];

    if ($cookie <= 0)
    {
        $cookie = 0;
        setcookie("count", $cookie);
    }
}

然后像这样打印出来

<li><a><div ng-app="" class="circle"> <?php echo $cookie ?></div></a></li>

我尝试使用 echo $cookie 来回显会话数组长度,并尝试使用

<li><a><div ng-app="" class="circle">{{ <?php echo count($_SESSION['certificates']) ?>}}</div></a></li>

我也试过这个链接:https://codepen.io/anon/pen/mMwVPb

但没有完全理解,无法在我的页面上运行。

它几乎按照我想要的方式工作,但我只需要刷新页面即可显示会话数组长度。如果有一种方法可以显示会话的值而无需刷新页面,我认为可以解决它。

【问题讨论】:

  • 你能贴出你试过的代码吗?
  • 由于您没有提供任何代码,无法为您提供帮助。
  • 更新问题

标签: javascript php html angularjs shopping-cart


【解决方案1】:

我不会弄乱 cookie,除非您可能希望在用户离开您的网站时将购物车保留在购物车中,然后返回您的商店。那时,您可以将购物车项目存储在数据库中,或者使用 cookie 的引用 ID 或其他东西。人们可以关闭 cookie,因此如果您的购物车依赖于 cookie 并且用户将其关闭,那么您就是 SOL。

我会将您的购物车存储在会话中,因为您可以在会话中轻松存储数组:

# Simple example of add to cart function
addToCart($sku,$qty=1)
    {
        # Make sure the quantity is a number
        if(!is_numeric($qty))
            $qty = 1;
        # If the cart is not yet set, create it
        if(!isset($_SESSION['cart']))
            $_SESSION['cart'] = array();
        # If the item is already in the cart, increment the quantity
        if(isset($_SESSION['cart'][$sku]))
            $_SESSION['cart'][$sku] += $qty;
        # If not in the cart already, create it
        else
            $_SESSION['cart'][$sku] = $qty;
    }

# Remember to start session on every page
session_start();

# To add to cart
if(isset($_REQUEST['add'])) {
    # Insert the sku in param 1, quantity into param 2
    addToCart($_REQUEST['ITEMCODE'],$_REQUEST['QTY']);
}
# Set some storage variables
$totalQty = 
$itemsQty = 0;
# To get qty in cart
if(!empty($_SESSION['cart'])) {
    # Loop through items in cart
    foreach($_SESSION['cart'] as $sku => $qty) {
        $totalQty += $qty;
        $itemsQty += 1;
    }
}
?>
<!-- If you have 5 products in the cart, this will say 5 -->
<h2>Total products in cart: <?php echo $itemsQty ?></h2>
<!-- If you have 5 products with quantity of 2 per product, this will write 10 -->
<h2>Total items in cart: <?php echo $totalQty ?></h2>

【讨论】:

  • 所以我为此使用了会话,它几乎按照我想要的方式工作。购物车编号显示在标题中,并且始终比实际编号落后 1,因此如果是 2 件商品,它将显示 1
  • 如果您将商品添加到购物车,并且在刷新页面之前它不会注册,那么您只需在添加购物车后进行标题重定向,
猜你喜欢
  • 2021-11-27
  • 2015-06-01
  • 2017-06-05
  • 2014-07-03
  • 2017-02-22
  • 1970-01-01
  • 2017-09-27
  • 1970-01-01
  • 2018-01-30
相关资源
最近更新 更多