如何缩小返回的对象数量,然后随机显示这些对象


how to narrow the amount of objects returned and then randomly display these

我有一个php文件featured.phtml,它从特定类别的加载产品

<?php $_productCollection=$this->getLoadedProductCollection() ?>
    <?php if(!$_productCollection->count()): ?>
        <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
    <?php else: ?>
    <?php // Grid Mode ?>
        <?php $_collectionSize = $_productCollection->count() ?>
        <?php $_columnCount = 6; ?>
        <?php $i=0; foreach ($_productCollection as $_product): ?>
            <?php if ($i++%$_columnCount==0): ?>
                <ul class="featured-products-grid">
            <?php endif ?>
                    <li class="hreview-aggregate hproduct item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->getImageLabel($_product, 'small_image') ?>" class="url home-product-image"><img class="photo fn" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a>
                        <h2 class="item fn home-product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_product->getName() ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h2>
                        <?php if($_product->getRatingSummary()): ?>
                            <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                        <?php endif; ?>
                        <?php echo $this->getPriceHtml($_product, true) ?>
                        <div class="actions">
                            <?php if($_product->isSaleable()): ?>
                                <button id="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span class="ui-button-text"><?php echo $this->__('Add to Cart') ?></span></button>
                            <?php else: ?>
                                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                            <?php endif; ?>
                        </div>
                    </li>
            <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
                </ul>
            <?php endif ?>
        <?php endforeach ?>
    <?php endif; ?>

我在我的主页上称这个代码为:

{{block type="catalog/product_list"category_id="14"template="catalog/product/featureed.phtml"}}

因此,基本上它查看category_id=14,并将所有产品加载到featured.phtml模板中

我的问题:

如何修改featured.phtml,使其只随机选择5个产品并按随机顺序显示?

感谢您的任何建议

要显示简单的随机乘积,可以使用以下代码。通过使用此代码,您可以显示随机乘积。但如果你想显示随机的产品特定类别,那么你也可以添加这个类别的属性过滤器。

$collection = Mage::getResourceModel('catalog/product_collection');
    Mage::getModel('catalog/layer')->prepareProductCollection($collection);
    $collection->getSelect()->order('rand()');
    $collection->addStoreFilter();
    $this->setProductCollection($collection);
    return parent::_beforeToHtml();
    //below code to display output
    if (($_products = $this->getProductCollection())):
        echo $_product->getSku(); // just to display sku
    endif;

在上面的代码中,这将只显示产品sku。你只需要写产品名称,图像,价格等代码。

希望它能帮助你。

感谢

相关文章: