I've been reading about ajax in magento and their is alot of talk about modules and controllers, so after managing to setup a custom module, controller and frontend router i'm now having problems, at the moment i just want my ajax call to return an category page and its products depending on what id/param is posted to the controller. I dont know alot about PHP so i looked around and came cross this
public function indexAction() {
$id = $this->getRequest()->getParam('id');
if($id) {
$_category = Mage::getModel('catalog/category')->load($id);
$product = Mage::getModel('catalog/product');
//load the category's products as a collection
$_productCollection = $product->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($_category)
->load();
// build an array for conversion
$json_products = array();
foreach ($_productCollection as $_product) {
$_product->getData();
$json_products[] = array(
'name' => ''.$helper->htmlEscape($_product->getName()).'',
'url' => ''.$_product->getProductUrl().'',
'description' => ''.nl2br($_product->getShortDescription()).'',
'price' => ''.$_product->getFormatedPrice().'');
}
$data = json_encode($items);
echo $data;
}
$this->loadLayout(array('helloworld_index_index'));
$this->renderLayout();
}
}
i know this is not exactly what i need but im having trouble coming to grips ajax in magento what i had in mind is that:
if anyone can please help me i would be truly greatful
Thank you
There is this one module that may help you in working in Ajax http://www.magentocommerce.com/magento-connect/ajaxify-8411.html
From your code, you render the layout of the page finally through:
$this->renderLayout();
That's what your problem is. Try to organize the data in an array encode into Json format.
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
And return.
This extension can help you work with AJAX in Magento: Magento Ajax Scroll Catalog It loads product lists through Ajax/JSON in category pages, search results and advanced search results pages.
You can use
http://github.com/hws47a/VF_EasyAjax
Allows frontend developers send ajax requests for every page and get Json response. You don't need to work with app/code section of Manento and change any PHP code. Make all what you need works via Ajax using only layout xmls, theme templates and javascript.
In the Json response frontend developers can receive: