Magento – Show Deal Only if Starting from Today’s Date

date

I am working on a daily deal module. The module lists all the deals that are active and have start and end date.

For ex:

Deal One:
Start Date : 31 JULY
End Date : 5 AUG

Deal Two:
Start Date : 3 AUG
End Date : 5 AUG

Here you can see that Deal One has started yesterday, while Deal Two will start on 3 AUG

The date format displayed in array is like below for a deal

[start_date] => 2014-03-06 00:00:00
[end_date] => 2014-05-19 00:00:00

How can I show a deal that will start only from today's date and remain active till its end date, i.e. if a deal has future start date, it won't appear in deal list and will be only shown when its start date matches with today's or current date ?

Multiple deals will be shown on deal listing page.

Will it conflict if I change my system date and time ?

Please Help.

Thanks

Jordan

Best Answer

Here's a method I've used for something quite similar (in this case, using the "Special Price" date range for the product).

I've extended from Mage_Catalog_Block_Product_List

protected function _saleList()
{
parent::_construct();
$this->setTemplate('saleitems.phtml');

// This should get the time localized to your timezone setting
$timestamp = Mage::getModel('core/date')->timestamp(time());
$this->_today = date('m/d/y h:i:s', $timestamp);

// I've set these as class properties, but you could just use a plain old variable if you like. I don't use these here but use them for listing upcoming and expired items.
$this->_tomorrow = date('m/d/y', mktime(0, 0, 0, date('m'), date('d') + 1, date('y')));
$this->_oneWeek = date('m/d/y', mktime(0, 0, 0, date('m'), date('d') + 7, date('y')));
$this->_yesterday = date('m/d/y', mktime(0, 0, 0, date('m'), date('d') - 1, date('y')));


// Here's the important bit. Note the `from` and `to` arguments passed in the array when filtering the dates. `to` is essentially a <=, and `from` is >= and they are used for dates.
$collection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
    ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $this->_today))
    ->addAttributeToFilter('special_to_date', array('date' => true, 'from' => $this->_today))
    ->addAttributeToSort('special_to_date', 'asc');

Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

// In your template, you can call $this->getSaleItems() and then iterate over each
$this->setSaleItems($collection);
}
Related Topic