カスタムカスタマーグリッドのIDの代わりに属性テキストを取得する
-
12-12-2019 - |
質問
AdminHTMLカスタマーグリッド内の顧客のカスタム属性を表示したい。
私は新しいモジュールを作成することによってこれを実行し、setCollection
のMage_Adminhtml_Block_Customer_Grid
メソッドを拡張しました
public function setCollection($collection)
{
$collection->addAttributeToSelect('x_customer_category');
parent::setCollection($collection);
}
.
および私は観察者を介して列をView に追加します。
/**
* Adds column to admin customers grid
*
* @param Varien_Event_Observer $observer
*
* @return Mscg_TemplateOverwrite_Model_Customer_Observer
*/
public function addCustomerGroupToGrid(Varien_Event_Observer $observer)
{
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/customer_grid') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$block->addColumnAfter('x_customer_category', array(
'header' => 'x_customer_category',
'type' => 'text',
'index' => 'x_customer_category',
), 'email');
}
}
.
しかし、これはオプション単位の属性であるため、顧客の選択された値のIDを取得するだけですが、特定のAttributのエンティティ値ではなく、TextValueを必要とします。
どうすればいいですか?
解決
text
からoptions
に列の種類を変更し、この形式のoptions
に値を含むkey => value
要素を追加する必要があります。
このようなもの:
$block->addColumnAfter('x_customer_category', array(
'header' => 'x_customer_category',
'type' => 'options',
'index' => 'x_customer_category',
'options' => array(
'id1' => 'label 1',
'id2' => 'label 2',
),
), 'email');
.
可能な値が動的である場合は、まずすべての値を持つ配列を構築する必要があります。すべての可能な値を取得し、options
要素に取得する配列を割り当てる方法を知っておく必要があります。
属性が標準のSELECT属性の場合は、次のように構築できます。
$attribute = Mage::getModel('eav/config')->getAttribute('customer', 'x_customer_category');
$options = $attribute->getSource()->getAllOptions(false);
$values = array();
foreach ($options as $option){
$values[$option['value']] = $option['label'];
}
.
次のように列を追加します。
$block->addColumnAfter('x_customer_category', array(
'header' => 'x_customer_category',
'type' => 'options',
'index' => 'x_customer_category',
'options' => $values,
), 'email');
. 所属していません magento.stackexchange