Siby Thomas
Siby Thomas

Reputation: 73

magento product collection by name

  <?php
$needle= $_GET["singleid"];

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

$_productCollection = $collection->addAttributeToFilter('name', array(
    array('like' => '% '.$needle.' %'), //spaces on each side
    array('like' => '% '.$needle), //space before and ends with $needle
    array('like' => $needle.' %') // starts with needle and space after
));
foreach ($_productCollection as $_product){
   echo $_product->getId().'</br>';
   echo $_product->getName().'</br>';
   **echo $_product->getProductUrl().'</br>';**//getting this only
   echo $_product->getPrice().'</br>';
}

?>

I am trying to get product collection based on product name but I get only product URL. I am trying get other attributes like name. My purpose is to create a search page.

Upvotes: 0

Views: 4557

Answers (4)

Manashvi Birla
Manashvi Birla

Reputation: 2843

Probably this might help :

$searchstring = 'Slim fit';
$productCollection = Mage::getModel('catalog/product')
                        ->getCollection() 
                        ->addAttributeToSelect('name')
                        ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'));

foreach ($productCollection as $_product){

            echo $_product->getId().'</br>';
            echo $_product->getName().'</br>';
            echo $_product->getProductUrl().'</br>';
            echo $_product->getPrice().'</br>';

        }

Upvotes: 1

Supravat Mondal
Supravat Mondal

Reputation: 2594

You can try this

$searchstring  = 'abc'  // search string 
$product_collection = Mage::getResourceModel('catalog/product_collection')
              ->addAttributeToSelect('*')
              ->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
              ->load();

 foreach ($product_collection as $product) {
     $ids[] = $product->getId();
 }
//return array of product ids

Upvotes: 0

rajatsaurastri
rajatsaurastri

Reputation: 653

Probably you could try some thing like

$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Some Name');
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'some name xyz');

It may help you

Upvotes: 0

Dushyant Joshi
Dushyant Joshi

Reputation: 3702

You need to select those attributes.

        $needle= $_GET["singleid"];         
        $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('name', array(
                array('like' => '% '.$needle.' %'), //spaces on each side
                array('like' => '% '.$needle), //space before and ends with $needle
                array('like' => $needle.' %') // starts with needle and space after
        ));
        $collection->addAttributeToSelect('name', 'entity_id', 'price');
        foreach ($collection as $_product){

            echo $_product->getId().'</br>';
            echo $_product->getName().'</br>';
            echo $_product->getProductUrl().'</br>';//getting this only
            echo $_product->getPrice().'</br>';

        }

Check here for more information

Upvotes: 1

Related Questions