Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento custom blocks

Tags:

magento

I am trying to show the popular product list using ajax in magento on the home page using ajax, I could do that for 5 or "N" no.of products, but what i want is the pagination toolbar to be added with the result set.

This is what i added to show the popular products,

// Magento layout
$magento_block = Mage::getSingleton('core/layout');
$productsHtml = $magento_block->createBlock('catalog/product');
$productsHtml->setTemplate('catalog/product/popular.phtml');
echo $productsHtml ->toHTML();

And under popular.phtml

<?php   

    $_productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addPriceData()
    ->addAttributeToSort('ordered_qty', 'DESC')
    ->addAttributeToSort('name', 'ASC')
    ->setPageSize($limit)
    ->setPage($p, $limit)       
    ->addAttributeToSelect(array('entity_id', 'entity_type_id', 'attribute_set_id', 'type_id', 'sku', 'category_ids', 'created_at', 'updated_at','has_options', 'sync', 'name', 'stock_status', 'wc_review_iwc_rating', 'wc_review_wa_rating', 'wc_review_bh_rating', 'small_image', 'status', 'pre_arrival', 'description', 'short_description', 'price', 'is_salable', 'stock_item', 'gift_message_available', 'featured'));

?>

So this gives me the popular products of specified page and limit, but i could not load the pagination toolbar(by directly adding the toolbar to popular.phtml or through create block layout function), Where am wrong? Could anybody tell me please.

Thanks

like image 415
Elamurugan Avatar asked Oct 24 '10 12:10

Elamurugan


3 Answers

Try creating a Mage_Catalog_Block_Product_List block and setting the collection of the popular products yourself.

$collection = Mage::getModel('catalog/product')->addAttributeToFilter('your popular products');
// do not load the collection yet, otherwise the toolbar will not work

$listBlock = Mage::getSingleton('core/layout')->createBlock('catalog/product_list');
$listBlock->setCollection($collection)->setTemplate('your/alternative/catalog/product/list.phtml');

The product list block always initializes a toolbar block itself. You can display the toolbar in the template by using <?php echo $this->getToolbarHtml() ?>

EDIT: Here is a working example of a sample frontend action in Magento 1.4.1.1:

public function productListAction()
{

    $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('*');

    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);

    $this->loadLayout();

    $listBlock = $this->getLayout()->createBlock('catalog/product_list')
            ->setTemplate('catalog/product/list.phtml')
            ->setCollection($collection);

    $this->getLayout()->getBlock('content')->append($listBlock);

    $this->renderLayout();
}

I hope that makes it clearer.

like image 113
Vinai Avatar answered Nov 17 '22 19:11

Vinai


For others reference this is what i added as per Vinai's code.

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
    Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);       
    $magento_block = Mage::getSingleton('core/layout');
    $productsHtml  = $magento_block->createBlock('catalog/product_list');
    $productsHtml ->setTemplate('catalog/product/list.phtml')->setCollection($collection);
    echo $productsHtml ->toHTML();

It renders the pagination toolbar perfectly.

like image 38
Elamurugan Avatar answered Nov 17 '22 17:11

Elamurugan


You should initialize the toolbar from your collection I guess. Have you seen this page?

like image 2
greg0ire Avatar answered Nov 17 '22 18:11

greg0ire