如何按属性的管理选项值过滤产品集合?
-
16-10-2019 - |
题
我目前正在开发一个产品配置器。我正在提交一个表单,该表单通过 POST 将自定义属性的管理选项值发送到控制器操作。如果我使用当前商店的属性选项标签,则一切正常,产品集合会被正确过滤,并返回与可配置产品关联的简单产品。遗憾的是,我使用管理值来生成表单和 URL,因为默认的瑞典存储值包含太多特殊字符。
所以我的问题是:使用管理选项值时,如何从我的收藏中获取正确的产品,或者更具体地说,如何获取管理选项值的 ID,例如将其用于默认商店的值:
$id = Mage::getResourceModel('catalog/product')
->getAttribute($key)
->getSource()
->getOptionId($value);
我已经尝试通过 addStoreFilter(0) 进行过滤,但这也不起作用。我已经读到它可能与默认商店中使用的平面表有关,但与管理无关,但我对 Magento 相当缺乏经验,所以我不太明白使用平面表的区别以及它的真正含义。
这是我最后一个关于过滤的问题供参考:addFieldToFilter 未返回所选属性值的正确产品
这是我的 getProduct() 函数:
public function getProduct($attributes)
{
Mage::Log($attributes);
$productModel = Mage::getModel('catalog/product');
//Get Product Collection
$collection = $productModel->getCollection()
//Filter for Selected Product
$collection->addAttributeToSelect('status');
$collection->addAttributeToSelect('doorconfig_enable');
$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 1));
foreach ($attributes as $key => $value)
{
$collection->addAttributeToSelect($key);
$id = Mage::getResourceModel('catalog/product')
->getAttribute($key)
->getSource()
->getOptionId($value);
$collection->addAttributeToFilter($key,
array(
'eq' => $id
)
);
}
$selection = $collection->getSelect()->__toString();
Mage::Log($selection);
Mage::log($collection->getSize(),null,'custom.log');
$product = $collection->getFirstItem();
return $product;
}
解决方案
像所有 Magento 一样,答案就在源代码中 - 您所需要做的就是去挖掘(或跳到本文的结尾)。
首先,找到属性源的类
$source = Mage::getResourceModel('catalog/product')
->getAttribute('color')
->getSource();
var_dump(get_class($source));
exit;
在现代版本的 Magento 中,这应该指向类 Mage_Eav_Model_Entity_Attribute_Source_Table
, ,它位于
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php
class Mage_Eav_Model_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
//...
}
接下来,我们将查找 getOptionId
此类和/或其父类的方法。我们会在 Mage_Eav_Model_Entity_Attribute_Source_Abstract
班级
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Abstract.php
public function getOptionId($value)
{
foreach ($this->getAllOptions() as $option) {
if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
return $option['value'];
}
}
return null;
}
检查这个方法,我们可以看看是否有效 foreach
荷兰国际集团的选项列表 getAllOptions
方法。那么我们来看看这个方法的定义。
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$storeId = $this->getAttribute()->getStoreId();
if (!is_array($this->_options)) {
$this->_options = array();
}
if (!is_array($this->_optionsDefault)) {
$this->_optionsDefault = array();
}
if (!isset($this->_options[$storeId])) {
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($this->getAttribute()->getId())
->setStoreFilter($this->getAttribute()->getStoreId())
->load();
$this->_options[$storeId] = $collection->toOptionArray();
$this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
}
$options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
if ($withEmpty) {
array_unshift($options, array('label' => '', 'value' => ''));
}
return $options;
}
啊哈!现在我们正在做某事。这似乎是加载所有选项信息的方法。具体来说,我们感兴趣的是
$storeId = $this->getAttribute()->getStoreId();
因此,Magento 获取要加载的选项的商店 ID 从属性. 。这意味着您应该能够执行以下操作(替换 color
和 red
当然,用你自己的变量)
//get the attribute
$attribute = Mage::getResourceModel('catalog/product')
->getAttribute('color');
//set the store id on the attribute
$attribute->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
//get the source
$source = $attribute->getSource();
//get the id
$id = $source->getOptionId('red');