简体   繁体   English

使用javascript检查magento购物车是否为空?

[英]Check if magento shopping cart is empty using javascript?

So I need to check if the cart on my magento site is empty or not. 因此,我需要检查magento网站上的购物车是否为空。 I can do this using php like this: 我可以这样使用php来做到这一点:

<?php
    $count = $this->helper('checkout/cart')->getSummaryCount();
    if($count==0){
        echo '<a href="#" data-url="checkout/cart/add/product/59/form_key/' . Mage::getSingleton('core/session')->getFormKey() . '" class="btn btn-success ac-button-top add-to-cart">Order</a>';
    }
    else {
        echo '<a href="' . $this->getUrl('checkout/cart') .'" class="btn btn-success ac-button-top add-to-cart">CHECKOUT</a>';
    }
?>

However the problem is that using php it will get cached. 但是问题是使用php会被缓存。 So in order for the button to change you'd have to refresh the cache. 因此,为了更改按钮,您必须刷新缓存。 So I was wondering if there is any way to do this check in javascript instead of php as I can't think of a way to do it? 所以我想知道是否有任何方法可以在javascript中而不是在php中进行此检查,因为我想不出一种方法?

You could use AJAX to run a asynchronus php request (should be uncached) to get the state of your cart. 您可以使用AJAX运行异步php请求(应取消缓存)以获取购物车的状态。 With the result of that request you can work in javascript to change the state of your button. 根据该请求的结果,您可以使用javascript来更改按钮的状态。

You can find more info about ajax and how to use it here: 您可以在这里找到有关ajax以及如何使用它的更多信息:

How does AJAX work? AJAX如何工作?

Ajax tutorial for post and get 用于发布和获取的Ajax教程

https://www.atwix.com/magento/ajax-requests-in-magento/ https://www.atwix.com/magento/ajax-requests-in-magento/

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

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