magento获取商品信息

I was recently working on a site where a “featured” product was required on the homepage. Rather than just making this a category, or a specific product, I wanted to give as much flexibilty as possible, and also allow them some extra functionality.

So, I created a new attribute, “homepage_product” – a boolean. This attribute can be assigned to any product. So, what if there are 10 products selected? I only have room for one. I needed to get a random product from the list of products that had this attribute selected.

I guess I could have read all the products into an array, and then used rand() to grab a random product from that array, but that seemed to provide an over-head, and seemed too complicated for something that should surely be able to be done in Magento.

So, I came up with this:

// get products tagged with homepage_product
$collection = Mage::getModel('catalog/product')->getCollection()
        ->addFieldToFilter(array(
                array('attribute'=>'homepage_product','eq'=>'1'),
        ))
        ->addStoreFilter();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$numProducts = 1;
$collection->setPage(1, $numProducts)->load();
 
foreach($collection as $homepageProduct) {
    $homepageProductInformation = Mage::getModel('catalog/product')->load($homepageProduct->getEntityId());
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值