i have created custom customer registration form , how to display customer group in dropdown menu please help me..
2 Answers
Please create module and add below code in Block file.
<?php
namespace Vendor\CustomerGroup\Block;
class CustomerGroup extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory,
array $data = []
) {
$this->groupCollectionFactory = $groupCollectionFactory;
parent::__construct($context, $data);
}
/**
* Retrieve customer group collection
*
* @return GroupCollection
*/
public function getCustomerGroupCollection()
{
if (!$this->hasData('customer_group_collection')) {
$collection = $this->groupCollectionFactory->create();
$this->setData('customer_group_collection', $collection);
}
return $this->getData('customer_group_collection');
}
And then you can get customer group data in template file
<?php
$customerGroupCollection = $block->getCustomerGroupCollection();
foreach($customerGroupCollection as $customerGroup) {
echo 'ID '.$customerGroup->getId();
echo 'Code '.$customerGroup->getCode();
echo 'Tax class Id '.$customerGroup->getTaxClassId();
echo "<br>";
}
Hope it will work for you
-
Yes it is helpful there is any way to display only wholesaler, distributor, retailor group onlydisplay? Commented Aug 12, 2019 at 10:15
-
You can add a condition by $customerGroup->getCode() Commented Aug 12, 2019 at 10:30
Please try with below code :
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupOptions = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();
?>
<select>
<?php foreach($groupOptions as $groupOption) { ?>
<option value="<?php echo $groupOption->getId(); ?>"><?php echo $groupOption->getCode(); ?></optinon>
<?php } ?>
</select>