Magento 2:获取客户组自定义模块

Magento 2:获取客户组自定义模块,magento,magento2,Magento,Magento2,如何在我的custome模块中显示客户群,如未登录-常规或未登录-常规-批发-零售商 这是我在网格中的代码 $this->addColumn( 'customer_group', [ 'header' => __('Customer Groups'), 'index' => 'customer_group', 'class' => 'customer_group',

如何在我的custome模块中显示客户群,如未登录-常规或未登录-常规-批发-零售商

这是我在网格中的代码

$this->addColumn(
        'customer_group',
        [
            'header' => __('Customer Groups'),
            'index' => 'customer_group',
            'class' => 'customer_group',
            'type' => 'options',
            'renderer' => 'Rendy\ModuleWarehouse\Block\Adminhtml\Warehouse\Renderer\CustomerGroups'
        ]
    );
这是我的代码CustomerGroup

名称空间Rendy\ModuleWarehouse\Block\Adminhtml\Warehouse\Renderer; 使用Magento\Framework\DataObject; 类CustomerGroups extends\Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer { 受保护的$\u customerGroup

public function __construct(
    \Magento\Backend\Block\Context $context,
    \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup,        
    array $data = []
) {
    $this->_customerGroup = $customerGroup;        
    parent::__construct($context, $data);
}
/**
 * Get customer groups
 * 
 * @return array
 */ 
public function render(DataObject $row) {
    $customerGroups = $this->_customerGroup->toOptionArray();
     array_unshift($customerGroups, array('value'=>'', 'label'=>'Any'));
}
}


谢谢

这个问题解决了吗?您可以在构造中使用\Magento\Customer\Model\GroupFactory:

public function __construct(
    \Magento\Backend\Block\Context $context,
    \Magento\Customer\Model\GroupFactory $groupFactory        
    array $data = []
) {
    $this->_groupFactory = $groupFactory;        
    parent::__construct($context, $data);
}
这很好,因为您可以在渲染函数中执行以下操作:

$collection = $this->groupFactory->create()->getCollection();
foreach ($collection as $group) {
  echo "group id ".$group->getId();
  echo "group code ".$group->getCode();
}