Magento flat product table - retrieving "Most Viewed" product names
I currently show a collection of the "Most Viewed" products as a list in my footer. I get the collection using the following code:
$_viewed_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->addViewsCount()
->setOrder('views_count', 'desc')
$_viewed_productCollection->load();
This works fine until I enable flat products, 开发者_如何学编程and then it can no longer retrieve the product names or prices. It can however still get the url, the sku etc, which really confuses me. I printed out the array of what can be returned, and it seems there’s no name data there at all, only the following:
Array (
[views] => 29
[entity_id] => 18
[entity_type_id] => 10
[attribute_set_id] => 38
[type_id] => simple
[sku] => sw810i
[created_at] => 2007-08-23 15:47:44
[updated_at] => 2008-08-08 14:50:56
[has_options] => 0
[required_options] => 0
[is_salable] => 1
[stock_item] => Varien_Object Object
( [_data:protected] => Array
( [is_in_stock] => 1 )
[_origData:protected] =>
[_idFieldName:protected] =>
[_isDeleted:protected] => )
[gift_message_available] => 2
)
Is there anyway to get a product name using it's SKU? Something like this perhaps:
<?php $sku = $_product->getData('sku'); echo $this->htmlEscape($_product->getName($sku)) ?>
I think this is actually a bug, as why would you not be able to get a products name? So, I've submitted it as such:
http://www.magentocommerce.com/bug-tracking/issue?issue=9812
I did come up with a workaround for this, if anyone needs it:
<?php foreach ($_productCollection as $_product): ?>
<?php $sku = $_product->getData('sku'); $_currentproduct = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $this->htmlEscape($_currentproduct->getName()) ?></a></li>
<?php endforeach; ?>
Basically you’re just using the sku, which you can get straight from the product collection, to load the name separately.
精彩评论