78

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

あるいは、それが不可能な場合、すべての製品を取得し、それらをフィルタリングして特定の属性を持つ製品だけをリストするにはどうすればよいでしょうか?

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

4

6 に答える 6

7

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

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;
}
于 2010-01-27T22:34:04.710 に答える
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>";
}
于 2012-09-05T17:36:44.920 に答える
3

TEXT製品リスト ページの管理者からフロント エンドに追加された属性を取得します。

ありがとうアニタ・モーリヤ

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

方法 1

の上list.phtml

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

For each product load by SKU and GET ATTRIBUTE INSIDE 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 "";}
?>

方法 2

Mage/Catalog/Block/Product/List.phtmlOVER RIDE を「ローカルフォルダ」にセット

つまり、コピー元

Mage/Catalog/Block/Product/List.phtml

と貼り付け

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

以下の太字で示す 2 行を追加して機能を変更します。

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;

}

そして、あなたはそれを見て幸せになるでしょう.... !!

于 2010-09-06T10:41:12.703 に答える
2

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

<?php $_product = $this->getProduct(); ?>
<?php echo $_product->getData('price_screen_tab_name');?>
于 2015-06-09T09:09:10.327 に答える
0

行を追加しました

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

app/code/core/Mage/Catalog/Block/Product/List.php 行 95

機能中_getProductCollection()

そしてそれを呼び出します

アプリ/デザイン/フロントエンド/デフォルト/ハロープレス/テンプレート/カタログ/製品/list.phtml

コードを書くことで

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

現在、Magento 1.4.x で動作しています。

于 2010-06-04T22:20:15.617 に答える