In my project, I have a url like this localhost/oldedge/catalog/category/view/id/37 which I am trying to pass as ajax request. So, to do this, I overrided the viewAction() in CategoryController file from my new module.

Can someone suggest me what to write in the new viewAction method so that I can get the response as just the list of products related to that passed id's category? (i.e 37, if I understand correctly)

I am not good in both ajax and PHP.

old "viewAction() method"

public function viewAction()
    if ($category = $this->_initCatagory()) {
        $design = Mage::getSingleton('catalog/design');
        $settings = $design->getDesignSettings($category);

        // apply custom design
        if ($settings->getCustomDesign()) {


        $update = $this->getLayout()->getUpdate();

        if (!$category->hasChildren()) {

        $update->addHandle('CATEGORY_' . $category->getId());

        // apply custom layout update once layout is loaded
        if ($layoutUpdates = $settings->getLayoutUpdates()) {
            if (is_array($layoutUpdates)) {
                foreach($layoutUpdates as $layoutUpdate) {

        // apply custom layout (page) template once the blocks are generated
        if ($settings->getPageLayout()) {

        if ($root = $this->getLayout()->getBlock('root')) {
            $root->addBodyClass('categorypath-' . $category->getUrlPath())
                ->addBodyClass('category-' . $category->getUrlKey());

    elseif (!$this->getResponse()->isRedirect()) {

New "viewAction() method"

public function viewAction()
    if ($category = $this->_initCatagory()) {
       //Here I am trying to send response as only the list of products related to that category.

Right now, what I am trying to show is a alert box with the response from php.


    jQuery('#nav a').on('click',function(){
        var url = jQuery(this).attr('data-href');
        //ajax call here
            url: url,
            success: function(response) {
                result = response;

The above jquery code is showing response as complete html page if I try the old "viewAction" method from CategoryController.php.

  • overriding controllers is not recommended but I did it just to know how to do ajax calls. I will change that later.
    – Mr_Green
    Oct 22 '13 at 7:48

Okay, the short answer is

$response = array();
$products = $category->getProductCollection()->addAttributeToSelect('*')->addPriceData();
foreach ($products as $product) {
    $response[] = array(
        'id' => $product->getId(),
        'media' => $product->getMediaGalleryImages(),
        'name' => $product->getName(),
        'image' => $product->getImage(),
        'thumbnail' => $product->getThumbnail(),
        'small_image' => $product->getSmallImage(),
        'short_desc' => $product->getShortDescription(),
        'price' => $product->getPrice(),
        'special_price' => $product->getSpecialPrice(),
        'final_price' => $product->getFinalPrice(),
        // and so on for all you need...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.