3

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()) {
            $design->applyCustomDesign($settings->getCustomDesign());
        }

        Mage::getSingleton('catalog/session')->setLastViewedCategoryId($category->getId());

        $update = $this->getLayout()->getUpdate();
        $update->addHandle('default');

        if (!$category->hasChildren()) {
            $update->addHandle('catalog_category_layered_nochildren');
        }

        $this->addActionLayoutHandles();
        $update->addHandle($category->getLayoutUpdateHandle());
        $update->addHandle('CATEGORY_' . $category->getId());
        $this->loadLayoutUpdates();


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

        $this->generateLayoutXml()->generateLayoutBlocks();
        // apply custom layout (page) template once the blocks are generated
        if ($settings->getPageLayout()) {
            $this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
        }

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

        $this->_initLayoutMessages('catalog/session');
        $this->_initLayoutMessages('checkout/session');
        $this->renderLayout();
    }
    elseif (!$this->getResponse()->isRedirect()) {
        $this->_forward('noRoute');
    }
}

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.

main.js

jQuery(function(){
    jQuery('#nav a').on('click',function(){
        var url = jQuery(this).attr('data-href');
        alert(url);
        //ajax call here
        jQuery.ajax({
            url: url,
            success: function(response) {
                result = response;
                alert(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
5

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...
    );
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));

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.