English 中文(简体)
• 如何从Magento的一个类别中列出4种随机产品
原标题:How to list 4 random products from a categoy in Magento

The question is pretty simple, the code below shows the items in one category. Those categories have 4 products. However my plan is to put in those categories 20 products, and make the following code to choose randomly 4 products of those categories.

我赞同我唯一需要改变的路线是:

$_productCollection=$this->getLoadedProductCollection();



<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?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 // List mode ?>

    <?php $_iterator = 0; ?>

    <script type="text/javascript">decorateList( products-list ,  none-recursive )</script>



    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); $_columnCount=4; ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="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->stripTags($this->getImageLabel($_product,  small_image ), null, true) ?>" class="product-image"><img src="<?php echo $this->helper( catalog/image )->init($_product,  small_image )->resize(170); ?>" width="170" height="170" alt="<?php echo $this->stripTags($this->getImageLabel($_product,  small_image ), null, true) ?>" /></a>
                 <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(),  name ) ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product,  short ) ?>
                <?php endif; ?>

                <div class="actions">
                    <div style="clear:both;"></div>
                    <div>
                        <?php $Deal = $_product->getResource()->getAttribute( deal )->getFrontend()->getValue($_product);?>
                                <?php $onSale = $_product->getResource()->getAttribute( on_sale )->getFrontend()->getValue($_product);?>
                                <?php $hotItem = $_product->getResource()->getAttribute( hot_item )->getFrontend()->getValue($_product);?>
                                <?php $freeShip = $_product->getResource()->getAttribute( free_shipping )->getFrontend()->getValue($_product);?>
                                <?php if($Deal ==  Yes ){ ?>
                                    <img width="91px" height="21px" alt="Deal" title="Deal" src="<?php echo $this->getSkinUrl( images/icon-deal.gif ) ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($onSale ==  Yes ){ ?>
                                    <img width="91px" height="21px" alt="On Sale" title="On Sale" src="<?php echo $this->getSkinUrl( images/icon-sale.gif ) ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($hotItem ==  Yes ){ ?>
                                    <img width="91px" height="21px" alt="Hot Item" title="Hot Item" src="<?php echo $this->getSkinUrl( images/icon-hot.gif ) ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($freeShip ==  Yes ){ ?>
                                    <img alt="Free Shipping" width="91px" height="21px" title="Free Shipping" src="<?php echo $this->getSkinUrl( images/icon-freeship.gif ) ?>" oncontextmenu="return false" >
                                <?php }?>
                    </div>
                </div>
                <?php echo $this->getPriceHtml($_product, true) ?>



                <div class="actions">
                    <?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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__( Out of stock ) ?></span></p>
                    <?php endif; ?>
                    <?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>


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


</div>
<?php endif; ?>
最佳回答

为此:

<?php

// $_productCollection = $this->getLoadedProductCollection();
 $_helper = $this->helper( catalog/output );
 $_category = Mage::getModel( catalog/category )->load($this->getCategoryId());
 $_productCollection = Mage::getResourceModel( reports/product_collection )
                       ->addAttributeToSelect( * )
                       ->addCategoryFilter($_category)
                       ->setVisibility(array(2,3,4));
 $_productCollection->getSelect()->order(new Zend_Db_Expr( RAND() ));                  
 $_productCollection->setPage(1, 4);

?>
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签