1

I'm currently trying to filter some collection in core but I dont get why there doesn't seem to work so after couple days, I ask you all :

The problem lies here :

 ->addAttributeToFilter(array('attribute'=>'customer_firstname', 'like' => "{$data}/%")) 

Seems I can't get a filter with a variable alone. I wish I could get all firstanme with the piece "Do" in it.

$data = "Do"; $collection = Mage::getModel('sales/order') ->getCollection() ->addAttributeToFilter('store_id', array('in' => array($storeids)))/*Display all orders which have same website than current one*/ ->addAttributeToFilter(array('attribute'=>'customer_firstname', 'like' => "{$data}/%")) ->setPageSize(15) ->setCurPage(1) ->setOrder('created_at', 'DESC'); $this->setCollection($collection); 

Thanks a lot in advance

2
  • Try to use addFieldToFilter() instead. addAttributeToFilter() should be used for catalog/product. Commented Aug 27, 2015 at 15:54
  • @adrien54 addAttributeToFilter is aliased to addFieldToFilter Commented Aug 27, 2015 at 19:42

1 Answer 1

1

To filer by customer firstname with a like condition you should be able to do it as follows.

$data = 'James'; /** @var Mage_Sales_Model_Resource_Order_Collection $collection */ $collection = Mage::getModel('sales/order')->getCollection(); $collection->addAttributeToFilter('customer_firstname', array('like' => "%{$data}%")); 

I would try reformatting your your addAttributeToFilter to look more like that.

Sign up to request clarification or add additional context in comments.

1 Comment

devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-8.html Have a read through here. Specifically "Filtering collections"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.