Comment afficher de NOUVEAUX produits par ligne à partir de catégories spécifiques dans Magento?
-
22-07-2019 - |
Question
Comme le titre l'indique, j'aime afficher les nouveaux produits issus de catégories spécifiques, séparés par des lignes dans la page d'accueil.
Rangée 1 - > Nouveaux produits de la catégorie 1
Rangée 2 - > Nouveaux produits de la catégorie 2
Rangée 3 - > Nouveaux produits de la catégorie 3
... etc
Dans mon CMS Admin Magento, sous la page d'accueil, ils seront appelés en tant que blocs distincts:
<block type="catalog/product_new" name="home.catalog.product.new_category_1" alias="product_new_category_1" template="catalog/product/new_category_1.phtml">
<block type="catalog/product_new" name="home.catalog.product.new_category_2" alias="product_new_category_2" template="catalog/product/new_category_2.phtml">
<block type="catalog/product_new" name="home.catalog.product.new_category_3" alias="product_new_category_3" template="catalog/product/new_category_3.phtml">
...etc
En gros, je & # 8217; je songe à dupliquer new.phtml
et à l'appeler new_category_1.phtml
, new_category_2.phtml
, etc. pour obtenir & # 8220; new & # 8221; produits de la catégorie 1 ou de la catégorie 2, respectivement.
J'ai joué avec Mage::getModel('catalog/category')->getCollection();
, getProductCollection
et getCatId
et je ne peux pas & # 8217; le faire fonctionner dans une copie de new.phml
(app/design/frontend/default/default/template/catalog/product/
).
Le code ci-dessous fonctionne mais ne charge pas " new " produits assignés dans la catégorie id assignée, il charge tous les produits de la catégorie.
<?php
$cat_id = 46; // category id
$category = Mage::getModel('catalog/category')->load($cat_id);
$products = $category->getProductCollection()->addCategoryFilter($category)->addAttributeToSelect('*');
if (($products=($_products = $this->getProductCollection()) && $_products->getSize())): ?>
<div class="hp-report">
<div class="head-alt">
<h2 class="title"><?php echo $this->__('New Products') ?></h2>
</div>
<table cellspacing="0" class="generic-product-grid" id="new-products-list-table">
<tr>
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i>=4): continue; endif; ?>
<td>
<p class="product-image">
<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(170) ?>" width="170" height="170" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
</p>
<p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $this->htmlEscape($_product->getName()) ?></a></p>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php echo $this->getPriceHtml($_product, true, '-new') ?>
<?php if($_product->isSaleable()): ?>
<a href="<?php echo $this->getAddToCartUrl($_product) ?>"><img src="<?php echo $this->getSkinUrl('images/btn_add_to_cart.gif') ?>" alt="<?php echo $this->__('Add to Cart') ?>" title="<?php echo $this->__('Add to Cart') ?>" /></a>
<?php else: ?>
<div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
<?php endif; ?>
<div class="clear"></div>
<ul class="add-to">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->getAddToWishlistUrl($_product) ?>" class="link-cart"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</td>
<?php $i++; endforeach; ?>
<?php for($i;$i%4!=0;$i++): ?>
<td> </td>
<?php endfor ?>
</tr>
</table>
<script type="text/javascript">decorateTable('new-products-list-table');</script>
</div>
<?php endif; ?>
Toutes les pensées ont été appréciées.
La solution
La requête de collection suivante devrait vous donner ce que vous voulez
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$products = $category->
getProductCollection()->
addCategoryFilter($category)->
addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->
addAttributeToSelect('*');
Un produit " nouveauté " est déterminé par deux attributs, news_from_date et news_to_date. Vous souhaitez donc ajouter deux attributs supplémentaires au filtre. Les appels de méthode spécifiques ci-dessus qui font cela sont
addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->
Ils sont tirés directement du bloc Nouveau produit à
/app/code/core/Mage/Catalog/Block/Product/New.php
Autres conseils
Millions merci Alan. Votre code répertoriait toujours tous les produits de la catégorie attribuée, mais un changement mineur l'a corrigé. Voici le code final à tous ceux qui pourraient être intéressés.
$_products = $category->
getProductCollection()->
addCategoryFilter($category)->
addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))->
addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')->
addAttributeToSelect('*');
if (($this->getProductCollection()) && $_products->getSize()): ?>
Existe-t-il un moyen de ne pas dupliquer le code de new.php tout en pouvant appliquer les filtres? Je ne suis pas un codeur en tant que tel, mais je suppose qu'un fichier intermédiaire prendra soin de cela.