I am creating a magento extension in which I need to show all products in tree order on the left bar, and then click on each category, it will load the products of the category by sending an ajax request. I used a block to display the category tree.
I am new to magento, so I decided to send an ajax request to my controller, get all the product data as JSONwell, and create HTML on the front panel to display the products.
I took the HTML version of the base theme from 2 column left barthe product list and used it in the ajax method. I used as
function loadCategoryProducts(categoryId) {
jQuery.ajax({
url: '/myModule/index/loadcategoryproduct',
type: 'post',
data: {categoryId: categoryId},
success: function(products) {
products = jQuery.parseJSON(products);
if (products != '') {
var html = '';
html += '<div class="page-title category-title">';
}
html += '</ul>';
html += '</div>'
jQuery('.col-main').html(html);
}
}
});
}
And my controller code
public function loadcategoryproductAction() {
$id = $this->getRequest()->getParam('categoryId');
if ($id) {
$_category = Mage::getModel('catalog/category')->load($id);
$product = Mage::getModel('catalog/product');
$_productCollection = $product->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($_category)
->load();
$json_products = array();
foreach ($_productCollection as $_product) {
$_product->getData();
$json_products[] = array(
'name' => $_product->getName(),
'url' => $_product->getProductUrl(),
'description' => nl2br($_product->getShortDescription()),
'price' => $_product->getFormatedPrice(),
'image' => $_product->getImageUrl());
}
$data = json_encode($json_products);
echo $data;
die;
}
}
, , , HTML CSS . . , magento ?
, , ? ajax , , ? , . googled, . . , . , .