简体   繁体   English

magento产品名称集合

[英]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. 我正在尝试根据产品名称获取产品集合,但仅获得产品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'); $ _category = Mage :: getModel('catalog / category')-> loadByAttribute('name','Some Name');
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'some name xyz'); $ _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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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