Ajax In Magento

空扰寡人 提交于 2020-01-12 11:36:55

问题


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

The Controller

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:

  1. in a ajax call i send the ID of the category to the controller
  2. the controller then arrays the products of the selected the category
  3. the array of products is sent/styled to the template file which is then sent back

if anyone can please help me i would be truly greatful

Thank you


回答1:


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.




回答2:


There is this one module that may help you in working in Ajax http://www.magentocommerce.com/magento-connect/ajaxify-8411.html




回答3:


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.




回答4:


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:

  • All messages that were added to session
  • Some blocks from current page layout
  • Any block that should be added to special layout xml


来源:https://stackoverflow.com/questions/7448617/ajax-in-magento

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!