0

i have written custom code to display all product by their category id now i want to add name filter which searches and show name matching with product

1: my category code works fine 2: when i add like filter to sort name i get null result please help me find out issue i had used addAttributeToFilter which does not seems working

$category_id = $_GET['category_id']; 
$catagory_model = Mage::getModel('catalog/category')->load($category_id); 
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product



$collection->addAttributeToSelect(array('id','name','url','small_image','price')); //add product attribute to be fetched

if($_GET['keyword']!=''){
    $needle= $_GET['keyword'];
    $collection->addAttributeToFilter('name', array(
        array('like' => '% '.$needle.' %')
    ));
}

$collection->addStoreFilter();   

1 Answer 1

3
if($_GET['keyword']!=''){
$needle= $_GET['keyword'];
$collection->addAttributeToFilter('name', array(
    array('like' => '% '.$needle.' %')
));

You need to remove the empty space in the '%' quotes.

if($_GET['keyword']!=''){
$needle= $_GET['keyword'];
$collection->addAttributeToFilter('name', array(
    array('like' => '%'.$needle.'%')
));

Your Answer

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

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