2009-08-26 13 views
76

ブロックコードで、特定の値を持つ属性を持つ製品のリストをプログラムで取得しようとしています。Magento - 特定の属性値を持つ製品を取得する

また、そうでない場合は、すべての商品をどのように取得し、特定の属性を持つ商品を一覧表示するにはどうすればよいですか?

標準ブール値フィルタANDまたはORを使用して、私の製品のサブセットに一致する検索を実行するにはどうすればよいですか?

答えて

160

ほとんどのMagentoモデルには、モデルの複数のインスタンスを取得するために使用できる対応するCollectionオブジェクトがあります。

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

製品を行い、製品のコレクションをインスタンス化するためには、MagentoのEAVスタイルのモデルですので、あなたが返すようにしたい任意の追加属性に追加する必要があります。

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

//fetch name and orig_price into data 
$collection->addAttributeToSelect('name'); 
$collection->addAttributeToSelect('orig_price');  

コレクションにフィルタを設定する構文は複数あります。私は常に下の冗長なものを使用しますが、Magentoソースを調べて、フィルタリング方法を使用できる追加の方法を調べることをお勧めします。

以下は、これは一つのこと、または別に等しい名前でフィルタリングしますが(よ​​りもより大きく、小さい)値の範囲によって

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

//filter for products whose orig_price is greater than (gt) 100 
$collection->addFieldToFilter(array(
    array('attribute'=>'orig_price','gt'=>'100'), 
)); 

//AND filter for products whose orig_price is less than (lt) 130 
$collection->addFieldToFilter(array(
    array('attribute'=>'orig_price','lt'=>'130'), 
)); 

をフィルタリングする方法を示しています。サポートされている短い条件分岐(EQ、LTなど)の完全なリストは、最後に、すべてのMagentoのコレクションは(基本コレクションクラスを反復処理することができるlib/Varien/Data/Collection/Db.php

_getConditionSql方法に見出すことができる

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

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B 
$collection->addFieldToFilter(array(
    array('attribute'=>'name','eq'=>'Widget A'), 
    array('attribute'=>'name','eq'=>'Widget B'),   
)); 

イテレータインタフェースを実装しています)。これは、フィルターが設定された後に製品をつかむ方法です。

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

//filter for products who name is equal (eq) to Widget A, or equal (eq) to Widget B 
$collection->addFieldToFilter(array(
    array('name'=>'orig_price','eq'=>'Widget A'), 
    array('name'=>'orig_price','eq'=>'Widget B'),  
)); 

foreach ($collection as $product) { 
    //var_dump($product); 
    var_dump($product->getData()); 
} 
+5

非常に詳しい回答です。ありがとう! –

+0

詳細な回答ありがとうございました。あなたは正しい道に沿って私を決めました。あなたのサンプルコードから結果のvar_dumpを行った。私が扱っている属性は複数の選択項目であるため、テキスト比較が機能しないように結果に数字のIDが表示されます。例えば。array( 'attribute' => 'cw_category'、 'eq' =>)この配列には配列がありません(配列は( array( '属性' => 'cw_category'、 'eq' => 'Aero' 'トラック')、 配列( '属性' => 'cw_category'、 'eq' => 'ツーリング') )); 返されます 'cw_category' =>文字列 '、536,535,534'(長さ= 12) – Christian

+0

特に掘り下げなくてもあなたを助けてくれることはありません(StackOverflow担当者は素晴らしいですが、あなたが追求する2つの道。まず、前述のように、可能なすべての比較演算子のリストをチェックアウトする_getConditionSql。同様の節または多分inで取得できます。次に、Mage_Eav_Model_Entity_Collection_AbstractでaddAttributeToFilterメソッドのPHPDocをチェックアウトすると、最初のパラメータの期待値の1つがMage_Eav_Model_Entity_Attribute_Interfaceになっています。それが正しいパスにつながる可能性があります。 –

7

これは、同じ問題を抱えている他の人を支援するための私の元々の質問です。属性でフィルタリングする必要がある場合は、idを手動で検索するのではなく、次のコードを使用して属性のすべてのid、valueのペアを取得できます。データは、属性名をキーとして配列として返されます。

function getAttributeOptions($attributeName) { 
    $product = Mage::getModel('catalog/product'); 
    $collection = Mage::getResourceModel('eav/entity_attribute_collection') 
       ->setEntityTypeFilter($product->getResource()->getTypeId()) 
       ->addFieldToFilter('attribute_code', $attributeName); 

    $_attribute = $collection->getFirstItem()->setEntity($product->getResource()); 
    $attribute_options = $_attribute->getSource()->getAllOptions(false); 
    foreach($attribute_options as $val) { 
     $attrList[$val['label']] = $val['value']; 
    } 

    return $attrList; 
} 

これは、属性セットIDで商品を取得するために使用できる関数です。前の関数を使用して取得されます。

function getProductsByAttributeSetId($attributeSetId) { 
    $products = Mage::getModel('catalog/product')->getCollection(); 
    $products->addAttributeToFilter('attribute_set_id',$attributeSetId); 

    $products->addAttributeToSelect('*'); 

    $products->load(); 
    foreach($products as $val) { 
    $productsArray[] = $val->getData(); 
    } 

    return $productsArray; 
} 
0

私は

アプリ/コード/コア/メイジ/カタログ/ブロック/製品/一覧にライン

$this->_productCollection->addAttributeToSelect('releasedate'); 

を追加しました。ライン上のPHP 95

機能で

_getProductCollection()

、その後

アプリ/設計/フロントエンド/デフォルト/ hellopress /テンプレート/カタログ/製品/ list.phtmlで

をそれを呼び出します

コードを書くことにより

<div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?> 
</div> 

Magento 1.4.xで動作しています

3

TEXT属性を管理者からフロントエンドに追加すると、製品リストのページに表示されます。

ありがとうございましたAnita Mourya

私は2つの方法があることを発見しました。バックエンドからテキストフィールドとして "na_author"という製品属性が追加されたとします。

METHOD 1

SKU BY各製品LOAD FOR list.phtml

<?php $i=0; foreach ($_productCollection as $_product): ?> 

の内側FOREACH

<?php 
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$_product->getSku()); 
$author = $product['na_author']; 
?> 

<?php 
if($author!=""){echo "<br /><span class='home_book_author'>By ".$author ."</span>";} else{echo "";} 
?> 

METHOD 2

Mage/Catalog/Block/Product/List.phtml OVER RIDE属性GETとに設定 "ローカルフォルダ '

すなわち

Mage/Catalog/Block/Product/List.phtml 

からコピーして、以下に太字で示される2行を追加することによって

app/code/local/Mage/Catalog/Block/Product/List.phtml 

変更する機能を貼り付けます。

protected function _getProductCollection() 
{ 
     if (is_null($this->_productCollection)) { 
      $layer = Mage::getSingleton('catalog/layer'); 
      /* @var $layer Mage_Catalog_Model_Layer */ 
      if ($this->getShowRootCategory()) { 
       $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId()); 
      } 

      // if this is a product view page 
      if (Mage::registry('product')) { 
       // get collection of categories this product is associated with 
       $categories = Mage::registry('product')->getCategoryCollection() 
        ->setPage(1, 1) 
        ->load(); 
       // if the product is associated with any category 
       if ($categories->count()) { 
        // show products from this category 
        $this->setCategoryId(current($categories->getIterator())); 
       } 
      } 

      $origCategory = null; 
      if ($this->getCategoryId()) { 
       $category = Mage::getModel('catalog/category')->load($this->getCategoryId()); 

       if ($category->getId()) { 
        $origCategory = $layer->getCurrentCategory(); 
        $layer->setCurrentCategory($category); 
       } 
      } 
      $this->_productCollection = $layer->getProductCollection(); 

      $this->prepareSortableFieldsByCategory($layer->getCurrentCategory()); 

      if ($origCategory) { 
       $layer->setCurrentCategory($origCategory); 
      } 
     } 
     **//CMI-PK added na_author to filter on product listing page// 
     $this->_productCollection->addAttributeToSelect('na_author');** 
     return $this->_productCollection; 

} 

あなたはそれを見ることを喜ぶでしょう.... !!

5
$attribute = Mage::getModel('eav/entity_attribute') 
       ->loadByCode('catalog_product', 'manufacturer'); 

$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter($attribute->getData('attribute_id')) 
      ->setStoreFilter(0, false); 

$preparedManufacturers = array();    
foreach($valuesCollection as $value) { 
    $preparedManufacturers[$value->getOptionId()] = $value->getValue(); 
} 


if (count($preparedManufacturers)) { 
    echo "<h2>Manufacturers</h2><ul>"; 
    foreach($preparedManufacturers as $optionId => $value) { 
     $products = Mage::getModel('catalog/product')->getCollection(); 
     $products->addAttributeToSelect('manufacturer'); 
     $products->addFieldToFilter(array(
      array('attribute'=>'manufacturer', 'eq'=> $optionId,   
     )); 

     echo "<li>" . $value . " - (" . $optionId . ") - (Products: ".count($products).")</li>"; 
    } 
    echo "</ul>"; 
} 
2

create属性名は "price_screen_tab_name"です。この単純な式を使用してアクセスします。

<?php $_product = $this->getProduct(); ?> 
<?php echo $_product->getData('price_screen_tab_name');?> 
関連する問題