3

I am displaying a custom product collection in a block that extends the category block. I am using Magento 1.9 and the default template.

My problem is that the pager is not working correctly. It displays the correct number of products in the collection, but the limit does not work even if I append it to the url as a parameter. The other tool bar elements (sorting and grid/list toggle) do work.

An example of this problem is that if there are 18 products in the collection, the pager will say "Items 1 to 18 of 18 total / Show 12 per page".

I think the root of the problem is somewhere in my collection block. I tried the $pager->setCollection($collection) method as discussed in the answer for this post and tried that with no discernible change in results.

How do I get the pager to work? Do I need to create a new block that extends the default pager and is used in this instance (seems like a sloppy idea), or is there a native solution?

layout.xml:

<?xml version="1.0" ?>
<layout>
<customer_items_index translate="label">
    <label>Customer My Account Items</label>
    <reference name="content">
        <block type="myitems/customer_items" name="customer_items" template="catalog/category/view.phtml">
            <block type="myitems/customer_list" name="customer_items_list" template="catalog/product/list.phtml">
                <block type="catalog/product_list_toolbar" name="customer_list_toolbar" template="catalog/product/list/toolbar.phtml">
                    <block type="page/html_pager" name="customer_list_toolbar_pager"/>
                </block>
            </block>
        </block>
    </reference>
    <reference name="left">
        <block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/>
    </reference>
 </customer_items_index>
 <customer_account translate="label">
    <reference name="customer_account_navigation">
        <action method="addLink"><name>items</name><path>customer/items/</path><label>My Items</label></action>
    </reference>
 </customer_account>

List.php (the collection block's code):

class Rdc_MyItems_Block_Customer_List extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection() {
    $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
    $productIds = Mage::helper('myitems')->getAllCustomerItems($customerId); 

    $_productCollection = Mage::getModel('catalog/product')->getCollection()
       ->addAttributeToSelect('*')
       ->addAttributeToFilter('entity_id', array(
        'in' => $productIds,
        ))
        ->load();

    return $_productCollection;
}

protected function _prepareLayout()
{
    parent::_prepareLayout();
    $collection = $this->_getProductCollection();
    $pager = $this->getLayout()->createBlock('page/html_pager', 'sales.order.history.pager')->setCollection($collection);
    $this->setChild('pager', $pager);
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}
}

Edit:

I am also extending the catalog/category block and think my problem might be related to that. The "category" I am creating is an empty category with only the name and description set. The collection of products is not created until the product_list block. It's looking likely that I can get rid of my custom category class and simplify my problem:

Items.php:

class Rdc_MyItems_Block_Customer_Items extends Mage_Catalog_Block_Category_View
{

    public function getProductListHtml()
    {
        return $this->getChildHtml('customer_items_list');
    }

    public function getCurrentCategory() 
    {
        $category = Mage::getModel('catalog/category')
                    ->setName("My Items")
                    ->setDescription("You bought these things.");
        return $category;
    }   
}
0

this will help i think problem with loading the collection

 parent::_prepareLayout();
        $collection = $this->_getProductCollection();
        $pager = $this->getLayout()->createBlock('page/html_pager', 'sales.order.history.pager')->setCollection($collection);
        $this->setChild('pager', $pager);
    $this->getCollection()->load();

            return $this;
  • Your code may be part of the solution, but it does not currently work for me. – Reflexorozy Jun 2 '15 at 16:52
0

The pager uses the catalog/layer in retrieving the items to display on a given catalog page. To get the pager to work, I needed to make a subsclass of Mage_Catalog_Model_Layer and update the code in both List.php and in the layout xml file.

newly added Layer.php:

<?php

class MyCompany_MyItems_Model_Layer extends Mage_Catalog_Model_Layer
{

public function getProductCollection()
{
    $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
    $productIds = Mage::helper('myitems')->getAllCustomerItems($customerId); 

    $collection = Mage::getResourceModel('catalog/product_collection')
       ->addAttributeToSelect('*')
       ->addAttributeToFilter('entity_id', array(
        'in' => $productIds,
        ));

    return $collection;
}

/**
 * Retrieve current category model
 * If no category found in registry, the root will be taken
 *
 * @return Mage_Catalog_Model_Category
 */
public function getCurrentCategory()
{
    $category = $this->getData('current_category');
    if (is_null($category)) {
        $category = Mage::getModel('catalog/category');
    }

    return $category;
}

/**
 * Change current category object
 *
 * @param mixed $category
 * @return Mage_Catalog_Model_Layer
 */
public function setCurrentCategory($category)
{
    return $this;
}

}

updated List.php:

<?php 

class Sc_MyItems_Block_Customer_List extends Mage_Catalog_Block_Product_List
{

public function getLayer()
{
    $layer = Mage::registry('current_layer');
    if ($layer) {
        return $layer;
    }
    return Mage::getSingleton('myitems/layer');
}

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $layer = $this->getLayer();
        $this->_productCollection = $layer->getProductCollection();
    }

    return $this->_productCollection;
}
}

updated layout.xml:

<?xml version="1.0" ?>
<layout>
<customer_items_index translate="label">
    <label>Customer My Account Items</label>
    <reference name="content">
        <block type="myitems/customer_items" name="customer_items" template="catalog/category/view.phtml">
            <block type="myitems/customer_list" name="customer_items_list" template="catalog/product/list.phtml">
                <block type="core/text_list" name="product_list.name.after" as="name.after" />
                <block type="core/text_list" name="product_list.after" as="after" />
                <block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                    <block type="page/html_pager" name="product_list_toolbar_pager"/>
                </block>
                <action method="setToolbarBlockName">
                    <name>product_list_toolbar</name>
                </action>
            </block>
        </block>
    </reference>
    <reference name="left">
        <block type="catalog/layer_view" name="catalog.layer.filter.view" template="catalog/layer/view.phtml"/>

    </reference>
 </customer_items_index>

 <customer_account translate="label">
    <reference name="customer_account_navigation">
        <action method="addLink"><name>items</name><path>customer/items/</path><label>My Items</label></action>
    </reference>
 </customer_account>
</layout>                                                                               

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.