1

I have a product collection from Monday to Sunday (using attributes date_from and date_to). I want to show products from current week but I need to exculde the current date products.

I have

->addFieldToFilter('date_from',array('gteq',$thisweekmonday))
->addFieldToFilter('date_to',array('lteq',$nextsunday));

In this collection I want to exclude the current day, how can I do it?

2

You can combine multiple conditions in addFieldToFilter() which will combined with OR.

Additionally, I'd suggest you to use the date comparison operators instead of gteq and lteq.

For the date_from attribute, the following will filter all products that have date_from between monday and yesterday OR between tomorrow and sunday (if that's what you want):

->addFieldToFilter(array(
    ['attribute' => 'date_from', 'condition' => array(
        'date' => true, 'from' => 'this week monday 0:00', 'to' => 'yesterday 23:59')],
    ['attribute' => 'date_from', 'condition' => array(
        'date' => true, 'from' => 'tomorrow 0:00', 'to' => 'this week sunday 23:59')]);

"this week monday 0:00" will be parsed with strtotime(), so it is a valid value. The times 0:00 and 23:59 are necessary because there is a bug in Varien_Db_Adapter_Pdo_Mysql::_prepareSqlDateCondition that treats date equal to datetime

If you want to learn more about the conditions that you can use in addAttributeToFilter() and addFieldToFilter(), take a look at Varien_Db_Adapter_Pdo_Mysql::prepareSqlCondition() - the core code is often the best documentation source.

I also explained addFieldToFilter in a bit more detail here: addFilter vs addFieldToFilter

0

Instead of 'gteq' use 'gt'. It would exclude the current date for your if the period your are trying to fetch the collection for is in the future.

  • gt will work if today is monday what if today is tuesday.then gt last monday will give current date also – Pradeep Sanku Sep 8 '14 at 19:11
0

I did not get a proper solution,

So, I suggest to divide the collection into two collections:

Step1: 1st collection from "this week Monday" date to yesterday

Step2: and other collection from tomorrow to "next week Sunday"

Step3: union two collection using union()

Example:

$collection1=Mage::getResourceModel('catalog/product_collection');

$collection1->addAttributeToFilter('created_at', array(
    'from' => $thisweekmonday,
    'to' => $yesterday,
    'date' => true, 
    ));

$collection2=Mage::getResourceModel('catalog/product_collection');  
$collection2->addAttributeToFilter('created_at', array(
    'from' => $tomorrow,
    'date' => true, 
    )); 



$collection1Select = $collection1->getSelect();
$collection2Select= $collection2->getSelect();

$mergedCollection = $collection1Select->union(array($collection2Select));

Your final collection is $mergedCollection

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.