如何获取 Magento 2 中的客户分组?我将会给你带来获取客户分组最简单的方法,请将这个话题中展示的代码段添加到你的块(block)类中。
/**
* Customer Group
*
* @var \Magento\Customer\Model\ResourceModel\Group\Collection
*/
protected $_customerGroup;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\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 getCustomerGroups() {
$customerGroups = $this->_customerGroup->toOptionArray();
array_unshift($customerGroups, array('value'=>'', 'label'=>'Any'));
return $customerGroups;
}
这就是获取 Magento 2 客户分组的全部内容。