【问题标题】:Magento 2 : get customer groups custome moduleMagento 2:获取客户组自定义模块
【发布时间】:2018-05-04 04:31:21
【问题描述】:

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

这是我在网格中的代码

$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; 类 CustomerGroups 扩展 \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer { 受保护的 $_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 magento2


    【解决方案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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2017-10-13
      • 2016-09-05
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多