Magento 2:获得客户群的客户模块

时间:2018-05-04 04:31:22

标签: magento magento2

我想如何在我的客户模块中显示客户群,例如"未登录 - 一般"或者"没有登录 - 一般 - 批发 - 零售商",

这是我在网格中的代码

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

这是我的代码CustomerGroups

命名空间Rendy \ ModuleWarehouse \ Block \ Adminhtml \ Warehouse \ Renderer; 使用Magento \ Framework \ DataObject; class CustomerGroups extends \ Magento \ Backend \ Block \ Widget \ Grid \ Column \ Renderer \ AbstractRenderer {     protected $ _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'));
}

}

谢谢

1 个答案:

答案 0 :(得分:1)

解决这个问题了吗?您可以在构造中使用\ 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();
}