2015-04-30 86 views
0
<?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>'; 
} 

?> 

我試圖根據產品名稱獲取產品集合,但我只獲得產品URL。我正在嘗試獲取其他屬性,如名稱。我的目的是創建一個搜索頁面。magento產品按名稱收集

+0

的問題是,你添加過濾器之前加載您的收藏。只需從getCollection()中移除 - > load()即可。 –

回答

1

或許這可能幫助:

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

     } 
+0

謝謝@manashvi birla工程 –

+0

高興地幫助你:) –

0

也許你可以嘗試像

$ _category =法師:: getModel( '目錄/類')的一些東西 - > loadByAttribute( '姓名', '有些名稱');
$ _product = Mage :: getModel('catalog/product') - > loadByAttribute('name','some name xyz');

它可能會幫助你

1

你需要選擇這些屬性。

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

     } 

檢查here瞭解更多信息

+0

這將返回一個空值,我需要添加任何額外的代碼 –

+0

空值的意思是空的?我只是檢查了它提供的所有信息的代碼? –

+0

是的空實際上我引用此鏈接 [檢查](http://magento.stackexchange.com/questions/11082/filtering-results-using-like) –

0

你可以試試這個

$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 
+0

結果爲空 –