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

Posted

技术标签:

【中文标题】Magento:自定义集合中缺少分页【英文标题】:Magento : Pagination missing in custom collection 【发布时间】:2014-09-28 13:54:29 【问题描述】:

在显示自定义产品集合时,我遇到了分页问题。由于我是 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); ?>"    /></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); ?>"    /></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; ?>

谁能帮我解决这个问题,不胜感激。

【问题讨论】:

你有没有解决我在分页中遇到问题的方法。 magento.stackexchange.com/questions/316910/… 【参考方案1】:

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

<?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;



?>

【讨论】:

您好,感谢您的回答!我试过了,但这并不能解决问题。分页仍然不起作用,所有产品都加载到第一页本身。你心里还有其他嫌疑人吗?顺便说一句,如前所述,我是在产品视图页面上执行此操作的,这就是它不起作用的原因吗?

以上是关于Magento:自定义集合中缺少分页的主要内容,如果未能解决你的问题,请参考以下文章

Magento 自定义集合打破了分页

php 分页自定义查询

php 分页自定义查询

PHP wordpress分页自定义查询

swift 中心分页自定义布局

页面分页自定义插件