カスタム整数属性で製品コレクションをソートします
-
16-10-2019 - |
質問
モジュールにcustom_stock_status属性を「int」バックエンドタイプとリソースに4つのオプションを作成しました。
array(
4 => 'In Stock',
3 => 'Soon',
2 => 'Out Of Stock',
1 => 'Discontinued',
);
この属性の作成に使用するコード:
class Jetkharid_Catalog_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup
{
public function getDefaultEntities()
{
return array(
'catalog_product' => array(
'entity_model' => 'catalog/product',
'attribute_model' => 'catalog/resource_eav_attribute',
'table' => 'catalog/product',
'additional_attribute_table' => 'catalog/eav_attribute',
'entity_attribute_collection' => 'catalog/product_attribute_collection',
'attributes' => array(
'jetkharid_stock_status' => array(
'type' => 'int',
'label' => 'Stock Status',
'input' => 'select',
'source' => 'jetkharid_catalog/product_attribute_source_stock_status',
'required' => true,
'is_configurable' => false,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'group' => 'General Information',
'searchable' => true,
'used_in_product_listing' => true,
'user_defined' => false,
'default' => 4,
),
),
),
);
}
}
そして、これがこの属性オプションの私のソースです:
class Jetkharid_Catalog_Model_Product_Attribute_Source_Stock_Status extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
public function getAllOptions()
{
if (!$this->_options) {
$this->_options = array(
array(
'value' => 4,
'label' => Mage::helper('jetkharid_catalog')->__('In Stock')
),
array(
'value' => 3,
'label' => Mage::helper('jetkharid_catalog')->__('Soon'),
),
array(
'value' => 2,
'label' => Mage::helper('jetkharid_catalog')->__('Out Of Stock'),
),
array(
'value' => 1,
'label' => Mage::helper('jetkharid_catalog')->__('Discontinued'),
)
);
}
return $this->_options;
}
}
今、私はこの属性で私のコレクションをソートしたいのですが、それは機能していないようです!
どうすればいいですか?!
みんなありがとう。
解決
カスタムソース属性については、メソッドを実装する必要があります addValueSortToCollection
それを機能させるために。
このメソッドは呼び出されます Mage_Catalog_Model_Resource_Product_Collection::addAttributeToSort
特にこのコードを見てください:
$attrInstance = $this->getEntity()->getAttribute($attribute);
if ($attrInstance && $attrInstance->usesSource()) {
$attrInstance->getSource()
->addValueSortToCollection($this, $dir);
return $this;
}
このクラスのメソッドを例として使用します Mage_Catalog_Model_Product_Status
.
これは、製品をステータスで並べ替えるときに使用されます。
私はそれが長い間混乱していることを知っていますが、あなたは基本的にそのクラスと同じコードを使用できると思います。 (わかりません)。
他のヒント
属性に移動します。編集と設定の属性を選択します 製品リストで使用されます はい。
知らせ新しい属性を追加した後に問題を抱えることができます。新しい属性のデフォルト値は、既存の製品に設定されていません。それを行うには、製品グリッドに移動して大量アクションを選択して、更新属性を選択し、すべての製品に新しい属性の価値を設定します。
所属していません magento.stackexchange