0

i have created custom customer registration form , how to display customer group in dropdown menu please help me..

  • Dropdown menu where? – Dominic Xigen Aug 9 at 19:30
  • I want to add dropdown menu in My custom form frontendside @Dominic Xigen – Magento Aug 10 at 3:20
2

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? – Magento Aug 12 at 10:15
  • You can add a condition by $customerGroup->getCode() – Kishor Thummar Aug 12 at 10:30
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.