首页 文章

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

提问于
浏览
0

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

这是我在网格中的代码

$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

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

相关问题