我正在使用Magento 1.7.0.2,我试图在随机product_list中显示我的产品的自定义图像。
首先,我将属性添加到我的所有集合中,并设置一个占位符图像。在数据库中,我搜索了*used_in_product_listing*并将其设置为1。
这是我的Product_List_Random Class:class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$categoryID = $this->getCategoryId();
if($categoryID) {
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID);
$collection = $category->getProductCollection();
} else {
$collection = Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->addAttributeToSelect('*');
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
$collection->setPage(1, $numProducts)->load();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
在这里,我试图添加$collection->addAttributeToSelect('*');
以获得前端图像的url。
这是我到目前为止在前端:
尝试的$_productCollection=$this->getLoadedProductCollection();
foreach ($_productCollection as $_product):
echo $this->helper('catalog/image')->init($_product, 'specialprice_image')->resize(158, 270);
echo $_product->getResource()->getAttribute('specialprice_image')->getFrontend()->getValue($_product);
echo $_product->getAttributeText('specialprice_image');
$product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
echo $product->getAttributeText('specialprice_image');
endforeach;
助手给了我占位符url。其他的方法都给不了我什么。
我希望有人能帮我解决这个问题。
编辑Mage_Catalog_Block_Product_List_Random class后,现在可以工作了
我只是删除了两行代码:
$collection->addAttributeToSelect('*');
$collection->addStoreFilter();
完整代码:
class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List
{
protected function _getProductCollection()
{
if (is_null($this->_productCollection)) {
$categoryID = $this->getCategoryId();
if($categoryID) {
$category = new Mage_Catalog_Model_Category();
$category->load($categoryID);
$collection = $category->getProductCollection();
} else {
$collection = Mage::getResourceModel('catalog/product_collection');
}
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
$collection->setPage(1, $numProducts)->load();
$this->_productCollection = $collection;
}
return $this->_productCollection;
}
}
在前端,我使用这个代码:
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'specialprice_image')->resize(158, 270); ?>" width="158" height="270" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
</a>