"Set Product as New to Date" (1 lecteur(s)) (1) Invité(s)
 | | |
|
SUJET: "Set Product as New to Date"
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 3
|
Par contre, je ne suis pas à l'aise encore avec les requêtes à la sauce Magento (Zend ?), chose que j'aurais fait en quelques secondes en SQL "classique".
Comment faire pour ordonner les résultats de façon aléatoire ?
Je crois qu'il existe une méthode "shuffle" mais seulement pour les données de type tableau.
Voici le bout de mon code, que je voudrais trier de façon aléatoire :
| Code : |
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
$todayDate = $product->getResource()->formatDate(time());
$products = $product->setStoreId($storeId)->getCollection()
->addAttributeToFilter('special_price', array('gt' => 0), 'left')
->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate), 'left')
->addAttributeToFilter(array(
array('attribute' => 'special_to_date', 'date' => true, 'from' => $todayDate),
array('attribute' => 'special_to_date', 'is' => new Zend_Db_Expr('null'))
), '', 'left')
->addAttributeToSelect(array('name', 'price', 'small_image', 'thumbnail'), 'inner')
->addAttributeToSelect(array('special_price', 'special_from_date', 'special_to_date'), 'left');
if ($category = Mage::registry('current_category')) {
$products = $products->addCategoryFilter($category, false, true);
}
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
$products->setPageSize($this->_page_size)->setCurPage(1);
$this->setProductCollection($products);
|
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 3
|
|
Je me réponds, ça peut servir :
$products->getSelect()->order('rand()');
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 0
|
Tu utilises un random car sinon les produits qui ont l'attribut "new" remonte en haut de la pile ou je me trompe ??
 Je crois que j'ai la même problématique et je ne sais pas trop comment faire.
Je ne veux pas utiliser de tri par prix ou alphabétique, donc un tri en fonction des dates de mise en ligne me va tres bien, mais j'aimerais éviter que les produits auxquels on attribut le flag "new" se retouvent automatiquement en haut de la liste...
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 8
|
|
Bonjour,
Comment faire pour utiliser correctement et simplement le filtre catégorie ?
De façon à ce que les produits qui sont définis dans special_price_from / to ne s'affichent que si ils appartiennent à la catégorie dans laquelle le visiteur se situe. Il faut très certainement utiliser le code suivant : addCategoryFilter() mais comment faut-il l'utiliser au sein de la page new.php ? (dont je me suis servi comme base pour en élaborer une autre)
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 109
|
Tu l'appliques simplement à ta collection ?
Mage_Catalog_Block_Product_New :
| Code : |
$catID = $this->getCurrentCategory()->getID()
|
| Code : |
->addCategoryFilter(Mage::getModel('catalog/category')->load($catID))
|
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 8
|
Alors, je viens de mettre le code que tu m'as indiqué dans ma page Promotions.php (duplication de New.php on est bien d'accord !) et ça me met une erreur dans mon fichier .phtml qui monte mon bloc :
Erreur :
| Code : |
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /var/www/vhosts/nomdedomaine.com/httpdocs/boutique/app/code/local/Lusodev/Catalog/Block/Product/Promotions.php on line 37
|
Lusodev_Catalog_Block_Product_Promotions :
| Code : |
class Lusodev_Catalog_Block_Product_Promotions extends Mage_Catalog_Block_Product_Abstract
{
protected $_productsCount = null;
$catID = $this->getCurrentCategory()->getID();
const DEFAULT_PRODUCTS_COUNT = 5;
protected function _beforeToHtml()
{
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = $this->_addProductAttributesAndPrices(Mage::getResourceModel('catalog/product_collection'))
->addStoreFilter()
->addCategoryFilter(Mage::getModel('catalog/category')->load($catID))
->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();
}
public function setProductsCount($count)
{
$this->_productsCount = $count;
return $this;
}
public function getProductsCount()
{
if (null === $this->_productsCount) {
$this->_productsCount = self::DEFAULT_PRODUCTS_COUNT;
}
return $this->_productsCount;
}
}
|
Et mon fichier Promotions.phtml :
| Code : |
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="bloc_promotions">
<h3><?php echo $this->__('Promotion(s)') ?></h3>
<div class="middle">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
<?php if ($i>5): continue; endif; ?>
<div class="col-12-1">
<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(100, 100); ?>" width="100" height="100" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" border="0" /></a>
<p class="a-center"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('<strong>See</strong> this product') ?></a></p>
</div>
<div class="col-12-2">
<h4><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h4>
<?php if ($_product->getShortDescription()):?><?php endif;?>
<?php echo $this->getPriceHtml($_product, true) ?>
</div>
<div class="divider"> </div>
<?php $i++; endforeach; ?>
<?php for($i;$i%5!=0;$i++): ?>
<!--Vide-->
<?php endfor ?>
<p class="a-center"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('See <strong>all promotions</strong>') ?></a></p>
</div>
<div class="footer"> </div>
</div>
<?php endif; ?>
|
|
|
|
|
Dernière édition: 06/02/09 à 09:44 Par Johan.
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 8
|
À priori c'est le
| Code : |
$catID = $this->getCurrentCategory()->getId();
|
Qui ne fonctionne pas. Car lorsque je donne uen valeur fixe à $catID ça fonctionne. On est proche !!! Une idée !?
|
|
|
|
|
|
|
Re:"Set Product as New to Date" Il y a 3 Années
|
Karma: 109
|
getCurrentCategory() dépend du block Navigation... normal donc.
| Code : |
public function getCurrentCategory()
{
if (Mage::getSingleton('catalog/layer')) {
return Mage::getSingleton('catalog/layer')->getCurrentCategory();
}
return false;
}
|
Réglé désormais ? ;)
|
|
|
|
Dernière édition: 06/02/09 à 13:43 Par Gabriiiel.
|
|
 | | |
|