Magento - 使用产品ID数组添加分页/工具栏

时间:2014-07-03 14:45:02

标签: magento pagination toolbar

我有一个自定义视图,它在集合上使用一系列条件语句来缩小特定的产品ID集。然后,我需要能够使用分页和工具栏显示这些

// create product collection
$_category  = $this->getCurrentCategory();  
$collection = $_category->getProductCollection();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);

//  create empty array for product id's
$ids = array();

// if and else conditions on collection for each
foreach($collection as $_product){
    $cats = $_product->getCategoryIds();
    if (condition 1 == ...){
        if (condition 2 == ...){
        } 
        else if (condition 3 == ...) {
        }
    } 
}

// end up with final array of product ids
$ids

我如何获取这些产品ID并为其添加工具栏和分页(就像普通视图一样?)我已经简化了上面的代码,所以它更加清晰,因为我有很多不相关的代码希望它有意义

2 个答案:

答案 0 :(得分:0)

请参阅我对THREAD的回答。它清楚地解释了如何在视图页面中显示列表页面。在您的情况下,您可以更改块中的方法_getProductCollection()并将代码放在该方法中。

所以这个方法应该是这样的。

public function _getProductCollection()
{
    //Your filtering comes here
    // need to store all product ids into an array.
    //here taking a demo array. Put your product Id array instead of that.
    $productIds = array(140,141,142,143,144);
    $attributes=Mage::getSingleton('catalog/config')->getProductAttributes();
    $collection = Mage::getModel('catalog/product')
                        ->getCollection()               
                        ->addAttributeToFilter('entity_id', array('in' => $productIds))
                        ->addAttributeToSelect($attributes)
                        ->load();
    return $collection;
}

答案 1 :(得分:0)

 step 1: In app/code/local/Mage/catalog/product/newall.php

    create block file that extend to Mage_Catalog_Block_Product_List



<?php

class Mage_Catalog_Block_Product_Newall extends Mage_Catalog_Block_Product_List
{

   protected function _getProductCollection()
   {
   if (is_null($this->_productCollection)) {
      $todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

      $collection = Mage::getResourceModel('catalog/product_collection');
      $collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());

      $collection = $this->_addProductAttributesAndPrices($collection)
         ->addStoreFilter()
         ->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
         ->addAttributeToFilter('news_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
         ), 'left')
         ->addAttributeToSort('news_from_date', 'desc')
         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());

      $this->setProductCollection($collection);
    }
      return $collection;
   }  

}


Step 2 : In Layout File or any cms Page Add he following code

<reference name="content">
   <block type="catalog/product_newall" name="product_new" template="catalog/product/list.phtml">
    <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>
</reference>