1

I created a custom form that search for specific product attribute my form look like this enter image description here

$location = $this->getRequest()->getParam('location');
$specialization = $this->getRequest()->getParam('specialization');
$name = $this->getRequest()->getParam('name');
$attrSetName = "Group Deal";
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
    ->load($attrSetName, 'attribute_set_name')
    ->getAttributeSetId();
$_productCollection=$this->getLoadedProductCollection();
$_productCollection->clear()
    ->addFieldToFilter('attribute_set_id', $attributeSetId)
    ->addAttributeToFilter('type_id', array('eq' => 'virtual'));
    //Default Count//
    $count = $_productCollection->getSize();

<?php  if(!empty($location)): ?>
    <?php $_productCollection->clear()->addAttributeToFilter('contact_city', array('like' =>$location.'%' )); ?>
<?php endif;?>
<?php  if($specialization>0): ?>
    <?php $_productCollection->clear()->addAttributeToFilter('doctor_specialization',$specialization);?>
<?php endif;?>
<?php  if(!empty($name)): ?>
    <?php $_productCollection->clear()->addAttributeToFilter('name', array('like' =>'%'.$name.'%' )); ?>
<?php endif;?>

The above line of code is how i return my results. How ever when i search with 2 search parameters like name and location im not getting accurate results, Is there any way to improve the search query? Any help would be appreciated very much.

1 Answer 1

1

By default addAttributeToFilter add AND condition. You need OR condition on collection use like below

$collection->addAttributeToFilter(
    array(
        array('attribute'=> 'someattribute','like' => 'value'),
        array('attribute'=> 'otherattribute','like' => 'value'),
        array('attribute'=> 'anotherattribute','like' => 'value'),
    )
);

see also in Magento Wiki

2
  • How can i add pagination to the result list? everytime a go to the next page the query for a custom attribute returns a different result
    – Elland
    Commented Mar 7, 2016 at 3:08
  • add pager block and set your collection to pager Commented Mar 7, 2016 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.