Aquest codi es troba extret de: ways to show products at the home page i millorat a Update code post Dan_w, joined: 2008-01-04.
La gestió de productes si es troben al home o no, es gestionarà com a atributs. Per crear els atributs anem:
Catalog → Attributes → Manage Attributes
Crearem un nou atribut:
Associarem aquest atribut a Catalog → Attributes → Manage Attribute Sets ⇒ default
Agreguem el fitxer:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category Mage
* @package Mage_Catalog
* @copyright Copyright (c) 2004-2007 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* New products block
*
* @category Mage
* @package Mage_Catalog
*/
class Mage_Catalog_Block_Product_Homepage extends Mage_Catalog_Block_Product_Abstract
{
public function __construct()
{
parent::__construct();
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
Mage::register('product', $product);
/* @var $product Mage_Catalog_Model_Product */
$todayDate = $product->getResource()->formatDate(time());
$products = $product->setStoreId($storeId)->getCollection()
->addAttributeToFilter('home_page_featured', array('yes'=>true))
->addAttributeToSelect(array('name', 'price', 'small_image','short_description'), 'inner')
->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left')
;
/* @var $products Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setOrder('hot_deals')->setPageSize(5)->setCurPage(1);
$this->setProductCollection($products);
}
}
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* @category default_default
* @package Mage
* @copyright Copyright (c) 2004-2007 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
?>
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<?php
$row = 0;
$j = 0;
?>
<div class="box best-selling">
<table border="0" cellspacing="0">
<tr class="odd">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php //if ($i>3): continue; endif; ?>
<td>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-img" /></a>
<div class="product-description">
<p><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
<p>
<?php echo $_product->getShortDescription(); ?>
<?php echo $this->getPriceHtml($_product, true) ?>
</p>
</div>
</td>
<?php
if($j==1){ // 2 columns
echo "</tr>";
if($row == 1) {
$row = 0;
echo '<tr class="odd">';
} else {
$row = 1;
echo '<tr class="even">';
}
$j = 0;
} else {
$j++;
} ?>
<?php $i++; endforeach; ?>
<?php endif; ?>
<?
if ($j == 1)
echo "<td> </td>";
?>
</tr>
</table>
</div>