简体   繁体   English

使用AJAX显示购物车中当前有多少种当前产品

[英]Using AJAX to display how many of the current product is currently in the shopping cart

really hoping someone can help me out here. 真的希望有人能在这里帮助我。

What I want to achieve is to show a little icon displaying how many of the current product is in the shopping cart. 我要实现的是显示一个小图标,显示购物车中有多少当前产品。 Eg user is browsing the product page "fresh oranges", the user adds 2 fresh oranges to the cart then I want to display "2" somewhere on the product page. 例如,用户正在浏览产品页面“新鲜的橘子”,该用户向购物车中添加了2个新鲜的橘子,那么我想在产品页面的某处显示“ 2”。 Here is my code: 这是我的代码:

<?php foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];    
if( $_product->id == get_the_ID() ) { ?>
    <div class="quantity-in-cart">
        <span><?php echo $values['quantity'] ?></span>
    </div>
<?php } } ?>

It's working if you refresh the product page, but since I'm using AJAX so the whole page doesn't refresh every time the user adds a product, I need this quantity field to update as more of this product is added to the cart. 如果您刷新产品页面,则可以使用,但是由于我使用的是AJAX,因此用户每次添加产品时都不会刷新整个页面,因此,随着更多产品被添加到购物车中,我需要此数量字段进行更新。

If anyone could help I'd really appreciate it, any questions or if none of what I wrote makes sense please let me know. 如果有人可以帮助我,我将不胜感激,如有任何疑问,或者我写的东西都没有道理,请告诉我。

I've found a workaround without needing to use AJAX which suits my needs. 我找到了一种解决方法,而无需使用适合我需要的AJAX。

Display the number of the current product in the cart in case the user comes back to the product page later. 在用户稍后返回产品页面时,在购物车中显示当前产品的编号。 If empty, nothing will display. 如果为空,将不显示任何内容。

<div class="quantity-in-cart">
<span><?php foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];    
    if( $_product->id == get_the_ID() ) { ?><?php echo $values['quantity'] ?>
<?php } } ?></span>
</div>

So if there are none of the current product in the cart, nothing will show up in that span. 因此,如果购物车中没有当前产品,那么该时间段内将不会显示任何产品。 So I added this JS to add a 0: 因此,我添加了此JS以添加0:

$span = $(".quantity-in-cart span");
if($span.text() == ""){
    $span.text("0");
}   

Now with a default value of 0 set in that span, if the user adds more of the current product to the cart, we can add the number in the span to the quantity number selected by the user via the add to cart form. 现在,在该跨度中设置了默认值0,如果用户将更多当前产品添加到购物车,我们可以通过“添加到购物车”表单将跨度中的数字添加到用户选择的数量编号中。

$(".description .add-to-cart .add_to_cart_button").click(function() {
    $('.quantity-in-cart span').text(parseInt($('.quantity-in-cart span').text()) + parseInt($('.quantity .qty').val()));
});

...And I'm happy with that. ...我对此感到满意。 There's probably a better way to get a 0 in that span if there are none of the current product in the cart via php, but I'm not great with php. 如果通过php购物车中没有当前产品,可能有一个更好的方法可以在该范围内获得0,但是我对php不太满意。 Just figured I'd share my workaround on the off chance someone is looking to achieve something similar. 只是想着,我会与其他人分享我的变通办法,以防止有人希望实现类似的目标。 I would still be interested in seeing how to accomplish this properly using WooCommerce's AJAX functionality (there's probably some code from the mini-cart that could be used but I don't understand it well enough myself.) 我仍然希望了解如何使用WooCommerce的AJAX功能正确完成此操作(可能会使用微型购物车中的一些代码,但我自己对此不太了解。)

I am not familiar with the shopping cart you mention, but conceptually this is how I'd go about it (option one of my initial comment): 我不熟悉您提到的购物车,但是从概念上讲,这就是我的处理方式(我最初评论的选项之一):

<div class="quantity-in-cart">
    <span data-qty="0" id="qty">0</span>
</div>

<input type="number" value="0" step="1" id="add_qty">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
var qty = $('#qty').data('qty');

$('#add_qty').on('click', function(event){
    $.ajax({
        url: "path/to/your/script.php",
    }).done(function() {
        $('#qty').attr('data-qty', $(this).val() );
        $('#qty').html( $(this).val() );
    });
});

</script>

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

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