1

I have a Featured Products block on my current theme and I'd like to make changes to it.

I would like tochange it into a Featured and New products. Yes, I did read How to check if a product is new

This is slightly different: I need to change this query to include products with a "New" status, based on the new from / to date.

$select = $read->select()
->from(array('cp'=>$categoryProductTable))
->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
->joinNatural(array('ea'=>$eavAttributeTable))
->where('pei.value=1')
->where('ea.attribute_code="featured"');

The other thing I would like to do is change the display of the product name to add the "New" label.

so go from

"super duper product" to "New! Super Duper Product"

I'm hoping there is an existing extension that does at least the product change. Failing that, I'm hoping there is a central place where I can modify the product display name for product grid to add "New!" if hte product is, indeed, new.

2

You can find the query to retrieve the new products in the class Mage_Catalog_Block_Product_New. Here is is just in case.

    $todayStartOfDayDate  = Mage::app()->getLocale()->date()
        ->setTime('00:00:00')
        ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

    $todayEndOfDayDate  = Mage::app()->getLocale()->date()
        ->setTime('23:59:59')
        ->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('news_from_date', array('or'=> array(
            0 => array('date' => true, 'to' => $todayEndOfDayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToFilter('news_to_date', array('or'=> array(
            0 => array('date' => true, 'from' => $todayStartOfDayDate),
            1 => array('is' => new Zend_Db_Expr('null')))
        ), 'left')
        ->addAttributeToFilter(
            array(
                array('attribute' => 'news_from_date', 'is'=>new Zend_Db_Expr('not null')),
                array('attribute' => 'news_to_date', 'is'=>new Zend_Db_Expr('not null'))
                )
          )
        ->addAttributeToSort('news_from_date', 'desc')
        ->setPageSize(5)//if you need a limit
        ->setCurPage(1)//if you need a limit.
    ;

Now you can add an other constraint to this collection to give you only the featured products if you need:

$collection->addAttributeToFilter('featuresd', 1);

And to change the product display name, you need to rewrite the product model and implement the method getName() that checks if a product is new or not.

public function getName() {
    $todayStartOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('00:00:00')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);

    $todayEndOfDayDate  = Mage::app()->getLocale()->date()
            ->setTime('23:59:59')
            ->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
    if (!$this->getNewsFromDate() && !$this->getNewsToDate()) {
         //the product is not new
         return $this->getData('name');
    }
    $isNew = false;
    if ((!$this->getNewsFromDate() || $this->getNewsFromDate() >= $todayStartOfDayDate) && 
    (!$this->getNewsToDate() || $this->getNewsToDate() <= $todayEndOfDayDate)) {
         //the product is new
         return Mage::helper('catalog')->__('New! ').$this->getData('name');
    }
    return $this->getData('name');
}
| improve this answer | |
  • this looks like what I was trying to acconplish. will try it in a few days. thanks for the detailed answer! – provence shop Sep 2 '14 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.