简体   繁体   中英

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.

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

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>';

        }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM