Magento:自定义集合中缺少分页

时间:2014-09-28 13:54:29

标签: magento magento-1.7

我在展示自定义产品系列时遇到了分页问题。因为我对Magento不熟悉,请在我的理解中承担任何天真。

目标:我想在“产品”视图页面上显示同一类别的其他产品,并且还要利用我正在使用的无限滚动扩展程序。因此,用户将在产品页面加载时显示(例如)5个产品,但是当用户向下滚动时,将加载并显示5个产品的下一页。此扩展程序在我的类别列表页面上运行良好。

问题:能够通过以下代码在产品页面上显示产品,无限滚动也看起来有效,但分页不起作用。集合中的所有产品都会一次性加载,以便当用户向下滚动时,第2页上的无限滚动显示重复的首页产品列表。

如果有人能帮助我解决这个问题,我将非常感激。

以下是我写的: 的 /app/code/local/Mage/Catalog/Block/Product/OtherProducts.php

<?php

class Mage_Catalog_Block_Product_OtherProducts extends Mage_Catalog_Block_Product_List
{
   protected function _getProductCollection()
   {
      $aCategoryId = $this->getProduct()->getCategoryIds();
        if (!$aCategoryId) {
            echo "This product is not assigned to any categories.";
        }
        $iCategoryId = $aCategoryId[0];
        $oCategory = Mage::getModel('catalog/category')->load($iCategoryId);
        $oProductCollection = $oCategory
            ->getProductCollection()
            ->addCategoryFilter($oCategory)
            ->addAttributeToSelect('*')
            ->addFieldToFilter('entity_id', array('neq' => $this->getProduct()->getId()));


        return $oProductCollection;

   }
}
?>

/app/design/frontend/default//layout/catalog.xml ,就在产品视图页面的xml之后。

<reference name="content">
                   <block type="catalog/product_OtherProducts" name="product_OtherProducts" template="catalog/product/otherslist.phtml">
                      <action method="setColumnCount"><columns>5</columns></action>
                      <action method="setProductsCount"><count>5</count></action>
                      <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" />                  
                         <action method="setDefaultGridPerPage"><limit>10</limit></action>
                         <action method="addPagerLimit"><mode>grid</mode><limit>12</limit></action>
                         <action method="addPagerLimit"><mode>grid</mode><limit>24</limit></action>
                         <action method="addPagerLimit"><mode>grid</mode><limit>36</limit></action>
                         <action method="addPagerLimit"><mode>grid</mode><limit>48</limit></action>
                         <action method="addPagerLimit" translate="label"><mode>grid</mode><limit>all</limit><label>All</label></action>
                      </block>

                      <action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>6</count></action>
                      <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                   </block>
</reference>

/app/design/frontend/default/roadgods/template/catalog/product/otherslist.phtml

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">

    <?php 
    echo $this->getToolbarHtml();
    ?>
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(160,200); ?>" width="160" height="200" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>">
                    <?php echo Mage::helper('commonfun')->getProductShortName($_helper->productAttribute($_product, $_product->getName() , 'name'),0,60); ?></a></h2>
                  <?php /*?>  <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?><?php */?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->isSaleable()): ?>
                        <!--<p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>-->
                    <?php else: ?>
                       <!-- <p class="availability out-of-stock"><span><?php echo $this->__('Sold Out') ?></span></p>-->
                    <?php endif; ?>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <?php /*?><ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul><?php */?>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <div class="productslist_view">
          <div class="pro_cat">
        <ul class="products-grid productslist_v-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">

               <div class="pro2">

               <?php
                 $price = $_product->getPrice();
                 $specialprice = $_product->getFinalPrice();
                 if($specialprice==$price){ }
                 else { 
                    echo '<p class="yellow_price">';
                    $totalPercent = (($price - $specialprice) *100)/ $price;
                    echo round($totalPercent)."%";
                    echo  '<br /> Off</p>';
                 }
                ?>


             <div class="product_image">   
             <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->keepAspectRatio(TRUE)->keepFrame(TRUE)->resize(160,200); ?>" width="160" height="200" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>

             </div>

             <?php
             $sku_gift = $_product->getData('sku_of_product_gift');
             if(!$sku_gift==''):
                $prod_gift = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku_gift);
                $gift_name = $prod_gift->getName();
                $gift_image = Mage::helper('catalog/image')->init($prod_gift, 'small_image')->resize(30);
             ?>

             <div class="productgiftlist">
             <a href="<?php echo $_product->getProductUrl() ?>" title= "<?php echo $gift_name?>" style="color:white">
              <img src="<?php echo $gift_image ?>" style="float:left;margin:3px;"><?php echo " + Free ".$gift_name ?>
             </a>
             </div>

             <?php endif; ?>

               <p class="new_name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo Mage::helper('commonfun')->getProductShortName($_helper->productAttribute($_product, $_product->getName(), 'name'),0,60) ?></a></p>
              <?php /*?>  <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?><?php */?>
            <p style="margin:auto; width:100%; float:left; height:auto; "><?php echo $this->getPriceHtml($_product, true) ?> </p>
            <p style="margin:auto; width:100%; float:left; height:auto; ">

                    <?php if($_product->isSaleable()): ?>
                       <!-- <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>--></p>
                    <?php else: ?>
                      <p style="margin:auto; width:100%; float:left; height:auto; ">   <span><?php echo $this->__('Sold Out') ?></span></p>
                    <?php endif; ?>
                 <p style="margin:auto; width:100%; float:left; height:auto; ">     <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                           <!-- <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>-->
                        <?php endif; ?>
                    </ul>
               </p>


                </div>



            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        </div>
        </div>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

    <!--<div class="toolbar-bottom">
        <?php /*echo $this->getToolbarHtml()*/ ?>
    </div>-->
</div>
<?php endif; ?>

请有人帮我解决这个问题,非常感谢。

1 个答案:

答案 0 :(得分:0)

你应该将集合分配给$_productCollection,因为pager类从这个对象中获取集合,试试这段代码

<?php

class Mage_Catalog_Block_Product_OtherProducts extends Mage_Catalog_Block_Product_List
{

protected $_productCollection;

protected function _getProductCollection()
{
  $aCategoryId = $this->getProduct()->getCategoryIds();
    if (!$aCategoryId) {
        echo "This product is not assigned to any categories.";
    }
    $iCategoryId = $aCategoryId[0];
    $oCategory = Mage::getModel('catalog/category')->load($iCategoryId);
    $oProductCollection = $oCategory
        ->getProductCollection()
        ->addCategoryFilter($oCategory)
        ->addAttributeToSelect('*')
        ->addFieldToFilter('entity_id', array('neq' => $this->getProduct()->getId()));

   $this->_productCollection = $oProductCollection;

    return $this->_productCollection;

}
}
?>