1

ページネーションに使用できるように、コレクションで setPageSize() を試みています。

setPageSize にどのような整数を入れても、すべての製品が返されます。

コード:

    <?php

class Rik_Featured_Block_Featured extends
Mage_Core_Block_Template {

    private $_itemPerPage = 2;
    public $_category_id = '' ;
    private $_currentPage = '';

    public function __construct() {
        $custom_var_code = Mage::getModel('core/variable')->loadByCode('homepage_firstrow_prod');
        if(isset($custom_var_code)) echo $this->_category_id = $custom_var_code->getValue('text') ;
        echo $page_var = $this->getRequest()->getParam('p');
        if(isset($page_var)&&$page_var!=0){           
            $this->_currentPage = $page_var;
        }
        if(isset($page_var)&&$page_var == '0'){           
            $this->_currentPage = '1';
        }


    }


    /****************** Here is my setcurpage and setpagesize************************/

        public function allProducts() {
        $collection = $this->_getCollection();
        echo 'col is'.$collection->count();
        /*Setting current page and page size */
        $collection->setCurPage(1);
        $collection->setPageSize($this->_itemPerPage);
        return $collection;
    }

        private function _getCollection() {

        $category = Mage::getModel('catalog/category')->load($this->_category_id);

        if($category->getUrlPath() != NULL) {

            $collection = $category->getProductCollection();


            //Mage::helper('catalog/product')->setSkipSaleableCheck(true);
            $collection->addWebsiteFilter();
            $collection->addUrlRewrite($this->_category_id);
            $collection->addMinimalPrice()->addFinalPrice()->addTaxPercents();
            Mage::getSingleton('catalog/product_visibility')
                    ->addVisibleInCatalogFilterToCollection($collection);
            Mage::getSingleton('catalog/product_status')
                    ->addVisibleFilterToCollection($collection);
            $collection->addAttributeToSelect(array('entity_id', 'sku', 'name', 'short_description', 
                'description', 'price', 'thumbnail', 'image', 'url_path', 'type_of'));

           return $collection;
        }
        else { echo 'cat does not exit';}
      }




    public function totalPages() {       

        $_collections_count = $this->_getCollection()->count();
        return $number_of_pages = ceil($_collections_count / $this->_itemPerPage);
    }



}

また、このブロックで使用している方法で __constructor を使用することをお勧めしますか?

ありがとうございました。

4

1 に答える 1