プログラムでMagentoでフィルタラブル属性とオプションを取得する方法
-
13-12-2019 - |
質問
カテゴリのフィルタラブル属性とそのオプションは、次のコード
を使用していました。$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel("catalog/category")->load($category_id);
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach($attributes as $attribute){
if($attribute->getAttributeCode() == 'price'){
$filterBlockName = 'catalog/layer_filter_price';
}elseif($attribute->getBackendType() == 'decimal'){
$filterBlockName = 'catalog/layer_filter_decimal';
}else{
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
$count = count($result->getItems());
if($count > 0){
$attribute->getFrontendLabel();
$attribute->getAttributeCode();
}
if($count > 0){
foreach($result->getItems() as $option) {
$option->getLabel();
$option->getValue();
}
}
}
.
今すぐ適切な出力を得ています。たとえば、次の出力
に入っています。brand - HP,Dell
processer - i3,i5,i7
.
現在の問題は、プロセッサI7と呼ばれるフィルタを適用した場合、Dellと呼ばれる製品が1つしかありません.1つのDell製品が正しく表示されますが、フィルタラブル属性が次のように表示されます。
brand - Dell
processer - i3,i5,i7
.
私がgetしているのはHPとDellです。これはプログラムで私がしています。Magentoカテゴリのリストページで階層化されたナビゲーション作品が必要です。
解決
私は次のコードで自分自身をやり、ここに他の人に役立つかもしれないコードを共有するだけです
$categoryId = '5'; // replace with your category id here
$category = Mage::getModel("catalog/category")->load($categoryId);
$layer = Mage::getModel("catalog/layer");
$layer->setCurrentCategory($category);
$setIds = Mage::getModel('eav/entity_attribute_set')
->load($attrSetName, 'attribute_set_name')
->getAttributeSetId();
$attributes = Mage::getResourceModel('catalog/product_attribute_collection');
$attributes->setItemObjectClass('catalog/resource_eav_attribute')
->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');
$attributes->addFieldToFilter('additional_table.is_filterable', array('gt' => 0));
$attributes->load();
foreach ($attributes as $attribute) {
$filter_attr = array();
$filter_attr['title'] = $attribute->getFrontendLabel();
$filter_attr['code'] = $attribute->getAttributeCode();
if ($attribute->getAttributeCode() == 'price') {
$filterBlockName = 'catalog/layer_filter_price';
}elseif ($attribute->getBackendType() == 'decimal') {
$filterBlockName = 'catalog/layer_filter_decimal';
}else {
$filterBlockName = 'catalog/layer_filter_attribute';
}
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
$i=0;
foreach($result->getItems() as $option) {
$attr_option = array();
if($attribute->getAttributeCode() == 'price') {
$attr_option['label'] = str_replace(array('<span class="price">','</span>'),'',$option->getLabel());
} else {
$attr_option['label'] = $option->getLabel();
}
$attr_option['value'] = $option->getValue();
$attr_option['count'] = $option->getCount();
$i++;
$filter_attr['options'][] = $attr_option;
}
if($i!=0){
$filter_attributes[] = $filter_attr;
}
}
. 所属していません magento.stackexchange