0

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

0

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);
}
  • A deal will be seen only when it matches the start date and current date and will expire only when the current date is less than end date – Jordan Aug 2 '14 at 4:50
  • Yes, see where dates are filtered in the collection. This would filter products that have a special_from_date (start date) that is less than or equal to today and has a special_to_date (end date) that is greater than or equal to today. – pspahn Aug 2 '14 at 10:25
  • I am no using any collection. Could you pls post that in simple code – Jordan Aug 2 '14 at 10:48
  • I shared this because it is something I've already written and thought it might be useful to you. I don't know what "simple code" is so I cannot help you there. – pspahn Aug 2 '14 at 10:52
  • Could you pls let me know the difference between addFieldToFilter('start_date',array('lteq'=>$todayDate)) and addFieldToFilter('start_date', array('date' => true, 'to' => $todayDate)) ?? – Jordan Aug 2 '14 at 11:14

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.