Magento - 특정 속성 값을 가진 제품 검색
블록 코드에서 특정 값을 가진 속성을 가진 제품 목록을 프로그래밍 방식으로 검색하려고 합니다.
그렇지 않으면 어떻게 모든 제품을 검색한 다음 특정 속성을 가진 제품을 나열하도록 필터링할 수 있습니까?
필터를 한 검색 AND
★★★★★★★★★★★★★★★★★」OR
내 제품의 서브셋과 일치시킬 수 있을까요?
거의 모든 Magento 모델에는 모델의 여러 인스턴스를 가져오는 데 사용할 수 있는 해당 컬렉션 개체가 있습니다.
제품 컬렉션을 인스턴스화하려면 다음 작업을 수행합니다.
$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 소스를 검사하는 것이 좋습니다.
다음으로 값의 범위(AND보다 큼)로 필터링하는 방법을 나타냅니다.
$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'),
));
한편, 이것은, 이것과 같은 이름 또는 다른 이름으로 필터링 됩니다.
$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'),
));
있는 조건 등)의 는, 「」(eq,lt 등)을 ._getConditionSql
in the method in the method in 。lib/Varien/Data/Collection/Db.php
마지막으로 모든 Magento 컬렉션을 반복할 수 있습니다(기본 컬렉션 클래스는 반복기 인터페이스에 구현됩니다).필터가 설정되면 제품을 이렇게 잡을 수 있습니다.
$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'),
));
foreach ($collection as $product) {
//var_dump($product);
var_dump($product->getData());
}
이것은 같은 문제를 안고 있는 다른 사람들을 돕기 위한 저의 원래 질문의 후속 조치입니다.아트리뷰트로 필터링 할 필요가 있는 경우는, 수동으로 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;
}
$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>";
}
방법TEXT
page되었습니다.
고마워 아니타 모리아
나는 두 가지 방법이 있다는 것을 알았다."na_author"라는 제품 속성이 백엔드에서 텍스트 필드로 추가되었다고 가정합니다.
방법 1
을 하다.list.phtml
<?php $i=0; foreach ($_productCollection as $_product): ?>
SKU에 의한 각 제품 로드 및 포어치 내 GET 속성
<?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.phtml
오버라이드하여 '로컬 폴더'로 설정
즉, 복사원
Mage/Catalog/Block/Product/List.phtml
붙여넣기 대상
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;
}
보고 기뻐할 거야…!
create Attribute name은 " 입니다.price_screen_tab_name
". 이 간단한 수식을 사용하여 액세스합니다.
<?php $_product = $this->getProduct(); ?>
<?php echo $_product->getData('price_screen_tab_name');?>
라인을 추가했습니다.
$this->_productCollection->addAttributeToSelect('releasedate');
에
앱/코드/코어/메이지/카탈로그/블록/제품/목록.php 회선 95
기능하여_getProductCollection()
그리고 나서 그것을 호출한다.
app/design/frontend/default/hellopress/hellopress/hellopress/hellopress/hphtml
코드 작성에 의한
<div><?php echo $this->__('Release Date: %s', $this->dateFormat($_product->getReleasedate())) ?>
</div>
현재 Magento 1.4.x에서 동작하고 있습니다.
언급URL : https://stackoverflow.com/questions/1332742/magento-retrieve-products-with-a-specific-attribute-value
'it-source' 카테고리의 다른 글
긴 텍스트 열이 있는 테이블을 JSON 배열로 변환 (0) | 2022.10.30 |
---|---|
.rar, .zip 파일 MIME 유형 (0) | 2022.10.30 |
HTML 버튼을 사용하여 JavaScript 함수 호출 (0) | 2022.10.30 |
어레이에서 변경 가능한 목록을 작성하시겠습니까? (0) | 2022.10.30 |
MySQL/MariaDB의 이진 열에서 UUID 문자열을 포맷하는 방법 (0) | 2022.10.21 |