通过特定属性下拉值加载产品集合
-
13-12-2019 - |
题
我正在尝试用自定义选项加载特定的虚拟产品,而是我继续获得空数组。
我的属性是'part_idifier'中的属性集'boat_part',它是一个有多个选项的下拉类型属性。我试图通过的选项是“看”。
这是我的代码:
$attribute = 'msh_boat_part_identifier';
$boatLookProducts = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter($attribute, array('like'=>'Look'))
->load()
->getItems();
foreach($boatLookProducts as $boatLookProduct)
{
$return[] = array(
"id" => (int)$boatLookProduct->getEntityId(),
"name" => $boatLookProduct->getName(),
"type" => "profile",
"shortDesc" => $boatLookProduct->getShortDescription(),
"desc" => $boatLookProduct->getDescription(),
"active" => ($boatLookProduct->getStatus() === "2" ? false : true)
);
}
echo Mage::helper('pb')->ConvertToJson($return);
.
在我的查询中为集合,如果将数组更改为
->addAttributeToFilter($attribute, array('like'=>'%%'))
.
它将成功返回所有概率,但我无法获得我想要的特定概况!
解决方案
作为part_identifier
是一个下拉属性,您无法通过选项标签过滤集合。
为了通过选项过滤集合,您需要获得该选项标签的ID(实际选项值)。
然后,您可以通过该选项ID过滤集合:
// use your own attribute code here
$attributeCode = 'your_attribute_here';
$attributeOption = 'Look';
$attributeDetails = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode);
$options = $attributeDetails->getSource()->getAllOptions(false);
$selectedOptionId = false;
foreach ($options as $option){
// print_r($option) and find all the elements
echo $option['value'];
echo $option['label'];
if ($option['label'] == $attributeOption) {
$selectedOptionId = $option['value'];
}
}
if ($selectedOptionId) {
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter($attributeCode, array('eq' => $selectedOptionId))
}
. 其他提示
这是迟到的,也许在询问时甚至没有有效,但我发现了更简洁的方式:
$attributeOptionId = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'your_attribute_code')
->getSource()
->getOptionId('Option Text Value/Label');
.
然后您将其在收集滤波器中使用它,如惯常