PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 10, 2022

[FIXED] How to add OR condition in Magento collection for created_at and updated_at date?

 November 10, 2022     magento, magento-1.7     No comments   

Issue

I have following code

$collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('category_ids')
            ->addAttributeToSelect('manufacturer')
            ->addAttributeToSelect('price')
            ->addAttributeToSelect('special_price')
            ->addAttributeToSelect('regularprice')          
            ->addAttributeToSelect('tier_price_for_bundle') 
            ->addAttributeToSelect('special_from_date') 
            ->addAttributeToSelect('special_to_date')   
            ->addAttributeToFilter('type_id', array('in' => array('simple')))
            ->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
            ->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

I want to add OR condition for updated_at and created_at in following

->addAttributeToFilter('updated_at', array('from'=>date("Y-m-d", time()-86400)));           
->addAttributeToFilter('created_at', array('from'=>date("Y-m-d", time()-86400)));

So that whether a product is created or updated my collection should load.

How can I do this?


Solution

To add an OR filter for ONE attribute on EAV collections: the attribute code is the first argument and the conditions are the second.

$col->addAttributeToFilter(‘name’, array(array(‘like’ => ‘M%’), array(‘like’ => ‘%O’))); (get items whose name starts with M OR ends with O)

To add an OR filter for DIFFERENT attributes on EAV collections: only pass one argument, an array of conditions with attributes.

$col->addAttributeToFilter(array(array(‘attribute’ => ‘weight’, ‘in’ => array(1,3)), array(‘attribute’ => ‘name’, ‘like’ => ‘M%’)));

This gets all items whose weight is 1 or 3 OR whose name starts with M.



Answered By - Slimshadddyyy
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing