문제

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.

도움이 되었습니까?

해결책

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top