Blok kodumda, belirli bir değere sahip bir özelliğe sahip ürünlerin listesini programlı olarak almaya çalışıyorum.
Alternatif olarak, eğer mümkün değilse, tüm ürünleri nasıl alır ve ardından ürünleri belirli bir öznitelikle listelemek için filtreler mi?
Standart boole filtreleri kullanarak nasıl arama yaparım? AND
veya OR
Ürünlerimin bir alt kümesini eşleştirmek için?
Hemen hemen tüm Magento Modellerinde, bir Modelin birden çok örneğini almak için kullanılabilecek bir Collection nesnesi vardır.
Bir Ürün koleksiyonunu başlatmak için aşağıdakileri yapın
$collection = Mage::getModel('catalog/product')->getCollection();
Ürünler bir Magento EAV stili Modelidir, bu nedenle geri dönmek istediğiniz ek özellikleri eklemeniz gerekir.
$collection = Mage::getModel('catalog/product')->getCollection();
//fetch name and orig_price into data
$collection->addAttributeToSelect('name');
$collection->addAttributeToSelect('orig_price');
Koleksiyonlarda filtreler ayarlamak için birden fazla sözdizimi var. Ben her zaman aşağıdaki ayrıntılı olanı kullanırım ancak filtreleme yöntemlerinin kullanabileceği ek yollar için Magento kaynağını incelemek isteyebilirsiniz.
Aşağıda, bir değerler aralığına nasıl filtre uygulanacağı gösterilmektedir (AND değerinden daha küçük)
$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'),
));
Bu, bir şeye eşit olan bir ada göre filtrelenecek olsa da.
$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'),
));
Desteklenen kısa koşulların tam bir listesi (eq, lt, vb.) _getConditionSql
yöntem lib/Varien/Data/Collection/Db.php
Son olarak, tüm Magento koleksiyonları yinelenebilir (temel toplama sınıfı yineleyici arabirimleri üzerinde uygular). Filtreler ayarlandıktan sonra ürünlerinizi bu şekilde alırsınız.
$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());
}
Bu, aynı soruna sahip başkalarına yardımcı olmak için orijinal sorumu takip etmektir. Bir özniteliğe göre filtrelemeniz gerekiyorsa, kimliği el ile aramak yerine, bir özniteliğin tüm kimlik ve değer çiftlerini almak için aşağıdaki kodu kullanabilirsiniz. Veri, anahtar olarak özellik adıyla bir dizi olarak döndürülür.
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;
}
İşte, kendi özellik kümesi kimliğine göre ürün almak için kullanabileceğiniz bir işlev. Önceki işlevi kullanarak alındı.
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;
}
$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>";
}
Almak TEXT
Ürün listeleme sayfasında yönetici tarafından ön uçlara eklenen özellikler.
Teşekkürler Anita Mourya
İki yöntem olduğunu buldum. Arka plandan "na_author" adlı ürün özelliklerinin metin alanı olarak eklendiğini varsayalım.
YÖNTEM 1
üzerinde list.phtml
<?php $i=0; foreach ($_productCollection as $_product): ?>
SKU TARAFINDAN HER ÜRÜN YÜKÜ İÇİNDE VE İÇERİK İÇERİĞİ
<?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 "";}
?>
YÖNTEM 2
Mage/Catalog/Block/Product/List.phtml
AŞIRI ve 'yerel klasör' olarak ayarlayın
diğer bir deyişle
Kopyala
Mage/Catalog/Block/Product/List.phtml
ve PASTE TO
app/code/local/Mage/Catalog/Block/Product/List.phtml
altta kalın olarak gösterilen 2 satır ekleyerek işlevi değiştirin.
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;
}
ve onu görmekten mutlu olacaksınız .... !!
özellik adı oluştur "price_screen_tab_name
". ve bu basit formülü kullanarak erişim.
<?php $_product = $this->getProduct(); ?>
<?php echo $_product->getData('price_screen_tab_name');?>
Hat ekledim
$this->_productCollection->addAttributeToSelect('releasedate');
içinde
App 95 üzerinde / kod / çekirdek / Mage / Katalog / Blok / Ürün / List.php
işlevde _getProductCollection()
ve sonra da
Uygulamanın / tasarım / kullanıcı arayüzü / default / hellopress / şablon / katalog / ürün / list.phtml
Kod yazarak
<div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?>
</div>
Şimdi Magento 1.4.x'te çalışıyor.