0

I'm writing search functionality in root magento file.

<?php
 require_once('app/Mage.php');
 umask(0);
 Mage::app('default');
 $collection = Mage::getModel('catalog/product')->getCollection();  
 $searchKeyword = 'kalmari123 abc'
$collection->addAttributeToFilter(
array(
    array('attribute'=>'sku','like'=>"%$searchKeyword%"),
    array('attribute'=>'name','like'=>"%$searchKeyword%")
));
foreach ($collection as $_product)
  {
    echo $_product->getSku();
  }

kalmari123 is the sku for product. It's giving correct result if $searchKeyword = 'kalmari123'; but it shows nothing if I'm using $searchKeyword = 'kalmari123 abc'. How can I make it work? I want to search if any of the given keyword is matching.

1 Answer 1

1

Try using REGEXP

$collection = Mage::getModel('catalog/product')->getCollection();
$searchKeyword = 'kalmari123 abc';

$searchKeyword = explode(' ', $searchKeyword);
$searchKeyword = implode('|', $searchKeyword);

$collection->addAttributeToFilter(
    array(
        array('attribute'=>'sku','regexp'=>"$searchKeyword"),
        array('attribute'=>'name','regexp'=>"$searchKeyword")
    ));

foreach ($collection as $_product) {
    echo $_product->getSku();
}

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.