0

So I need to check if the cart on my magento site is empty or not. I can do this using php like this:

<?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. 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?

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
MariaL
  • 1,112
  • 2
  • 16
  • 41

1 Answers1

0

You could use AJAX to run a asynchronus php request (should be uncached) to get the state of your cart. With the result of that request you can work in javascript to change the state of your button.

You can find more info about ajax and how to use it here:

How does AJAX work?

Ajax tutorial for post and get

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

Fabian S.
  • 2,441
  • 2
  • 24
  • 34