tags:

views:

68

answers:

3

Hi,

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

+1  A: 

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

greg0ire
no, my issue is different, i want to display it from custom php file. so when i tried to load toolbar.phtml using this way it does not working. $magento_block = Mage::getSingleton('core/layout'); $productsHtml2 = $magento_block->createBlock('catalog/product_list_toolbar'); $productsHtml2 ->setTemplate('catalog/product/list/toolbar.phtml'); echo $productsHtml2 ->toHTML(); it returns fatal error. Fatal error: Call to a member function getSize() on a non-object in D:\wamp\www\wc2\app\design\frontend\enterprise\espresso\template\catalog\product\list\toolbar.phtml on line 34
Ela
ok now look into toolbar.phtml. Do you see which variable is a "non-object"? It is the return value of $this->getCollection(). You must find a way to set this collection. Look at Vinai's code, he writes a call to setCollection(), try to do something similar!
greg0ire
+1  A: 

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.

Vinai
That one is working, with the normal product display. But i want to display it from custom php file. Even i tried to load the toolbar.phtml like this. $layout = Mage::getSingleton(‘core/layout’);$toolbar = $layout->createBlock(‘catalog/product_list_toolbar’);//Render toolbar htmlecho $toolbar->toHtml();This is not working in 1.4.1 but worked in 1.3. I have to use pagination toolbar. But it does not working.
Ela
hi vinai, i did your suggestion but unfortunately it doesnot work. The main think, am working on EE 1.9, AND I DID THIS. $collection = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); $listBlock = Mage::getSingleton('core/layout')->createBlock('catalog/product_list'); $listBlock->setCollection($collection)->setTemplate('catalog/product/list/list.phtml'); var_dump($listBlock->getCollection()); (RETURNS NULL) //echo $listBlock ->toHTML(); (THROUGHS FATAL ERROR).
Ela
Mage::getModel('catalog/product')->loadByAttribute() does not return a collection, but a Mage_Catalog_Model_Product. I have edited the post above and added a controller action to make things more clear.
Vinai
wow, you are awesome. thanks a lot. It sworking perfectly. I did this. $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();
Ela
A: 

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.

Ela