属性の管理者オプション値で製品収集をフィルタリングする方法は?
-
16-10-2019 - |
質問
私は現在、製品構成者を開発しています。投稿を介してカスタム属性の管理者オプション値をコントローラーアクションに送信するフォームを送信しています。現在のストアの属性オプションラベルがすべて正常に機能している場合、製品コレクションは正しくフィルタリングされ、構成可能な製品に関連付けられたシンプルな製品が返されます。悲しいことに、デフォルトのスウェーデンのストア値にはあまりにも多くの特殊文字が含まれているため、フォームとURL生成の管理者値を使用しています。
私の質問は、管理者オプション値を使用する場合、またはより具体的に使用する場合、コレクションから適切な製品を取得するにはどうすればよいですか。
$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');