Magento - Loading ajax products in the current template

I am creating a magento extension in which I need to show all products in tree order on the left bar, and then click on each category, it will load the products of the category by sending an ajax request. I used a block to display the category tree.

I am new to magento, so I decided to send an ajax request to my controller, get all the product data as JSONwell, and create HTML on the front panel to display the products.

I took the HTML version of the base theme from 2 column left barthe product list and used it in the ajax method. I used as

function loadCategoryProducts(categoryId) {
jQuery.ajax({
    url: '/myModule/index/loadcategoryproduct',
    type: 'post',
    data: {categoryId: categoryId},
    success: function(products) {
        products = jQuery.parseJSON(products);
        if (products != '') {
            var html = '';
            html += '<div class="page-title category-title">';
             // blah blah blah blah blah to draw html from ajax request
            }
            html += '</ul>';
            html += '</div>' // end of category-products product
            jQuery('.col-main').html(html);
        }
    }
});

}

And my controller code

public function loadcategoryproductAction() {
    $id = $this->getRequest()->getParam('categoryId');
    if ($id) {
        $_category = Mage::getModel('catalog/category')->load($id);
        $product = Mage::getModel('catalog/product');

        //load the category 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' => $_product->getName(),
                'url' => $_product->getProductUrl(),
                'description' => nl2br($_product->getShortDescription()),
                'price' => $_product->getFormatedPrice(),
                'image' => $_product->getImageUrl());
        }

        $data = json_encode($json_products);

        echo $data;
        die;
    }
}

, , , HTML CSS . . , magento ?

, , ? ajax , , ? , . googled, . . , . , .

+4
1

, . . . Magento list.phtml . ,

  • .
  • list.phtml
  • Magento, , It is Magento: -)

,

 $id = $this->getRequest()->getParam('categoryId');
 $category = Mage::getModel('catalog/category') ->setStoreId(Mage::app()->getStore()->getId()) ->load($id);
 Mage::unregister('current_category');
 Mage::register('current_category', $category);
 echo $this->getLayout() ->createBlock('catalog/product_list') ->setTemplate('catalog/product/list.phtml') ->setCategoryId($id) ->toHtml();
 die;

HTML- , HTML- success function ajax.

success : function(products) {
  jQuery('col-main').html(products)
}

.: -)

+2

Source: https://habr.com/ru/post/1547689/


All Articles