Magento – How to refresh Cart using PHTML file with AJAX Magento

ajaxjavascriptmagento-1.7

In following File, there is a file "reload.php"

But i want to use "myajaxcart.phtml" in Magento, How ??

Is it possible to use .phtml there, as magento doesn't allow direct access to .phtml files ??

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script>
var auto_refresh = setInterval(
function()
{
$('#loaddiv').fadeOut('slow').load('reload.php').fadeIn("slow");
}, 200);
</script>

I want like this :

$('#loaddiv').fadeOut('slow').load('myajaxcart.phtml').fadeIn("slow");

Thanks in Advance.

Best Answer

Using a standalone PHP file is not right way. You should be following the MVC structure of Magento.

AOE made a module that loosely does what you need it to do (ie. render out a block to be used for AJAX calls). It opens a little bit of a minefield in terms of security as potentially any block can be loaded, but you can add some appropriate filters to prevent issues.

There is a repo here, https://github.com/fbrnc/Aoe_Static

The basic principle relies on a simple block of code within the main controller,

$this->loadLayout();
$layout = $this->getLayout();

$requestedBlockNames = $this->getRequest()->getParam('getBlocks');
if (is_array($requestedBlockNames)) {
  foreach ($requestedBlockNames as $id => $requestedBlockName) {
    $tmpBlock = $layout->getBlock($requestedBlockName);
    if ($tmpBlock) {
      $response['blocks'][$id] = $tmpBlock->toHtml();
    } else {
      $response['blocks'][$id] = 'BLOCK NOT FOUND';
    }
  }
}
$this->getResponse()->setBody(Zend_Json::encode($response));