Php 在Magento主页中显示随机产品

Php 在Magento主页中显示随机产品,php,magento,Php,Magento,我在我的产品上添加了“是/否”属性,允许我在首页上显示推荐的产品(属性设置为“是”)。现在,我想在我的主页上显示1个随机产品。这是我现在的代码: $show_num_items = 2; $show_index_array = array(); $index_iterator = 1; $_productCollection = Mage::getResourceModel('reports/product_collection') ->addAttributeToSel

我在我的产品上添加了“是/否”属性,允许我在首页上显示推荐的产品(属性设置为“是”)。现在,我想在我的主页上显示1个随机产品。这是我现在的代码:

    $show_num_items = 2;
$show_index_array = array();
$index_iterator = 1;

$_productCollection = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'configurable');

if($_productCollection->getSize()):
    $show_index_array = range(0,($_productCollection->getSize()-1));
    shuffle($show_index_array);
    $show_index_array = array_slice($show_index_array, 0, $show_num_items);

    foreach($_productCollection as $_product):
        $_recommended = $_product->getData('recommend_product');
        if($_recommended == 1): 
            if(in_array($index_iterator, $show_index_array)): ?>
                <div><img src="<?php echo $this->helper('catalog/image')->init($_product, 'image'); ?>" />      
                </div>
                <dl>
                    <dt><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></dt>
                    <dd><?php echo $_product->getResource()->getAttribute('short_description')->getFrontend()->getValue($_product); ?></dd>
                    <dd><a href="<?php echo $_product->getProductUrl(); ?>">Read More</a></dd>
                </dl>
            <?php endif;
                $index_iterator++;
        endif;
    endforeach;
endif;              
$show\u num\u items=2;
$show_index_array=array();
$index_迭代器=1;
$\u productCollection=Mage::getResourceModel('报告/产品\u集合')
->addAttributeToSelect(“*”)
->addAttributeToFilter('type_id','configurable');
如果($\u productCollection->getSize()):
$show_index_array=range(0,($_productCollection->getSize()-1));
洗牌($show\u index\u数组);
$show_index_array=array_slice($show_index_array,0,$show_num_items);
foreach($\u productCollection作为$\u product):
$\推荐=$\产品->获取数据(“推荐\产品”);
如果($\u建议==1):
如果(在数组中($index\u迭代器,$show\u index\u数组)):?>
helper('catalog/image')->init($_product,'image');?>" />      

尝试使用
$show\u num\u items=1;
$index\u iterator=0;
,然后添加
->addAttributeToFilter(“推荐产品”,1)
到您的收藏

实际上,这是我最初做的,但没有显示任何产品,因此我尝试更改值,但没有注意到您正在测试这些产品是否在收藏加载后被推荐。这样,随机选择的产品可能不会被推荐,因此无法显示任何内容。我已编辑了我的相应地回答