Magento : afficher des promotions en page d’accueil
Voici comment afficher simplement les produits en promotions (promos spéciales) sur la page d’accueil (ou tout autre page CMS magento).
Tout d’abord, créer un block PHP qui se chargera de sélectionner les produits en promotions spéciales. Bien sûr, il suffit d’adapter le code pour changer la sélection.
Créez le fichier « app/code/local/GeorgesAbitbol/Catalog/Block/Product/Promotions.php ».
<?php
/**
* Display Promotions PHP Block
*
* @author Georges@Bibtol <contact@georgesabitbol.net>
*/
class GeorgesAbitbol_Catalog_Block_Product_Promotions extends Mage_Catalog_Block_Product_Abstract
{
protected $_productsCount = null;
const DEFAULT_PRODUCTS_COUNT = 5;
/**
* Prepare collection with new products and applied page limits.
*
* return Mage_Catalog_Block_Product_New
*/
protected function _beforeToHtml()
{
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('special_from_date', 'desc')
->setPageSize($this->getProductsCount())
->setCurPage(1)
;
$this->setProductCollection($collection);
return parent::_beforeToHtml();
}
/**
* Set how much product should be displayed at once.
*
* @param $count
* @return Mage_Catalog_Block_Product_New
*/
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
/**
* Get how much products should be displayed at once.
*
* @return int
*/
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
}
Ensuite, le code suivant doit être contenu dans le fichier « app/design/frontend/default/default/template/georges_abitbol/promotions.phtml »
<?php
if (($oProducts = $this->getProductCollection()) && $oProducts->getSize()):
foreach ($aProducts as $oProduct): ?>
<a href="<?php echo $oProduct->getProductUrl() ?>" title="<?php echo $this->htmlEscape($oProduct->getName()) ?>">
<img class="product-image" src="<?php echo $this->helper('catalog/image')->init($oProduct, 'small_image')->resize(195, 195); ?>" width="195" height="195" alt="<?php echo $this->htmlEscape($oProduct->getName()) ?>" />
</a>
<h5><a class="product-name" href="<?php echo $oProduct->getProductUrl() ?>" title="<?php echo $this->htmlEscape($oProduct->getName()) ?>"><?php echo $this->htmlEscape($oProduct->getName()) ?></a></h5>
<?php echo $this->getPriceHtml($oProduct, true);
echo $this->getReviewsSummaryHtml($oProduct, 'short');
unset($aProduct);
unset($oProduct);
endforeach;
unset($oProducts);
endif;
?>Il faut maintenant déclarer le module. Pour cela, ajoutez le fichier « app/etc/modules/GeorgesAbitbol_Catalog.xml » avec le contenu suivant :
<?xml version="1.0"?>
<config>
<modules>
<GeorgesAbitbol_Catalog>
<active>true</active>
<codePool>local</codePool>
</GeorgesAbitbol_Catalog>
</modules>
</config>
Après avoir activer le module, il faut déclarer le rewrite du block qui permettra d’appeler le block depuis la gestion du CMS
Fichier « app/code/local/GeorgesAbitbol/Catalog/etc/config.xml » :
<?xml version="1.0"?>
<config>
<modules>
<GeorgesAbitbol_Catalog>
<version>1.0</version>
</GeorgesAbitbol_Catalog>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product_promotions>GeorgesAbitbol_Catalog_Block_Product_Promotions</product_promotions>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Ensuite, il ne vous reste plus qu’à ajouter cette ligne d’appel dans la page CMS de la home via l’administration
{{block type="catalog/product_promotions" template="georges_abitbol/promotions.phtml"}}
Trackbacks & Pingbacks
- condos in austin
- sewing machine reviews
- cctv
- indigestion
- melatonin
- dentysta w anglii
- top dating sites
- silver violin bow
- tablet android
- akcesoria kuchenne
- cheap wedding cakes
- pinoy tv
- compost pile
- drzwi wejsciowe
- plytki
- Rejestracja spolki ltd w Anglii
- Polski lekarz ginekolog w Londynie
- Biuro rachunkowe w Londynie
- reviews domain web hosting services
- medical technologist

Bonjour,
dans le fichier « promotions.phtml », vous utilisez une variable non définie au sein de votre foreach : $aProducts.
Je pense que c’est une erreur et qu’il s’agit en faite de la variable $oProducts, instanciée précédemment., pour ainsi boucler sur la collection de produits.
De ce fait, le unset($aProduct) en fin de boucle est inutile.
Cordialement,
Jordan.