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

Php 如何缩小返回的对象数量,然后随机显示这些对象,php,magento,magento-1.5,Php,Magento,Magento 1.5,我有一个php文件featured.phtml,它加载特定类别的产品 <?php $_productCollection=$this->getLoadedProductCollection() ?> <?php if(!$_productCollection->count()): ?> <p class="note-msg"><?php echo $this->__('There are no products

我有一个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; ?>


    要显示简单的随机产品,可以使用以下代码。 通过使用此代码,您可以显示随机产品。但如果您想显示随机产品特定类别,则还可以添加该类别的属性过滤器

    $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。您只需要为产品名称、图像、价格等编写代码

    希望它能帮助你

    谢谢

    可能的副本