开发者

Ajax In Magento

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


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.


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜