使用AJAX动态刷新购物车?

发布于 2024-12-18 06:59:48 字数 614 浏览 3 评论 0原文

WP eCommerce 中遇到一个小问题。当我将商品添加到购物车时,我的购物车小部件会立即使用 AJAX 反映更改。但是,如果我的购物车中有商品并且我刷新页面,则我的购物车默认显示“0 件商品”(当我的购物车中实际上仍然有商品时)。然后,当您添加新商品时,将进行 AJAX 调用,并且我的购物车会刷新(添加新商品)。

我的问题是,如何在不实际添加商品的情况下刷新我的购物车?通过调用下面的脚本,我能够向我的购物车添加一个价值为 0 美元的空白商品,但这非常hacky。我只需要发送 AJAX 调用来动态刷新我的购物车,但实际上不需要向其中添加任何内容。有什么想法吗?

<body onload="document.getElementById('refresh-cart').click()">

    <form class="product_form">
        <input type="hidden" name="wpsc_ajax_action" value="add_to_cart">
        <input id="refresh-cart" name="Empty" type="submit">
    </form>

</body>

Have a small problem here in WP eCommerce. When I add items to my cart, my cart widget reflects the changes immediately using AJAX. However, if there are items in my cart and I refresh the page, my cart shows "0 items" by default (when there are still actually items in my cart). Then when you add a new item, the AJAX call is made, and my cart refreshes (adding the new item).

My question is, how do I refresh my cart without actually adding an item? By calling the script below, I was able to add a blank item to my cart with a value of $0, but this is pretty hacky. I just need to send an AJAX call to refresh my cart dynamically, but not actually add anything to it. Any ideas?

<body onload="document.getElementById('refresh-cart').click()">

    <form class="product_form">
        <input type="hidden" name="wpsc_ajax_action" value="add_to_cart">
        <input id="refresh-cart" name="Empty" type="submit">
    </form>

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-12-25 06:59:48

您可以在一定的时间间隔调用 ajax 函数来刷新购物车:

var intervalID = setInterval(function(){your_ajax_function();}, 5000);

希望有帮助

You could call your ajax function at certain intervals fr refreshing cart:

var intervalID = setInterval(function(){your_ajax_function();}, 5000);

Hope it helps

少跟Wǒ拽 2024-12-25 06:59:48

首先,您尝试做的事情不是有效的电子商务方法,这可能会导致许多其他问题。
您所说的是,我相信是迷你购物车,并且在页面刷新时,如果其显示的总量为零,则您的代码或迷你购物篮的实现存在问题。

mu的建议是调试导致此问题的问题,而不是胡思乱想:)

The first thing is place is what you trying to do is not a valid e-commerce approach and this can lead to many other issues.
what you are talking about is i believe is mini-cart and on page refresh if its showing total quantity as zero than either there is some problem in your code or in the implementation of mini-basket.

mu suggestion would be to debug the issue which is causing this problem rather than going for a heck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文