How can I order latent test products in Magento?

I am trying to show the latest products in my magento store.

What is a recent product? It is determined by two criteria:

  • created_at
  • news_from_date

I would like to order my product indifferently between created_at and news_from_date, and not by one criterion, but the second after.

Any ideas?

I have already tried the following code:

->addAttributeToSort('created_at, news_from_date', 'desc')

OR

->addAttributeToSort('news_from_date', 'desc')
->addAttributeToSort('created_at', 'desc')

OR

->addAttributeToSort(array('created_at' => 'desc', 'news_from_date' => 'desc'))

OR

->addAttributeToSort(array('created_at', 'news_from_date'), 'desc'))
+3
source share
4 answers

There is no "OR" in the SQL order clauses, so you need to find a way to reduce the problem to one column. The following is untested, but should give you an idea.

// Make sure the correct attributes are available with names we choose.
$products->addAttributeToSelect('created_at, news_from_date');

// Choose the LATEST date to sort as it is the most RECENT.
$products->addOrder('MAX(created_at, news_from_date)', 'desc');
+1
source

/ created_at news_from_date.

: -

$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

$collection = Mage::getModel('catalog/product')
                ->getCollection()                   
                ->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')
                ->addAttributeToSort('news_from_date', 'desc')
                ->addAttributeToSort('created_at', 'desc');

.

0

I would recommend using news_from_date instead of the generated date. This gives you the flexibility to create content in advance, but it still displays in the correct order when you publish it.

0
source

it's better:

$_productCollection = $this->getLoadedProductCollection()->clear()->addAttributeToSort('created_at', 'DESC')->setPageSize(4);
0
source

All Articles