1

I've managed to get customer collection using class \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory

but it returns me default details not with Created Customer Attributes.

Any idea how to get Customer Collection with it's Attributes ?

1

Get Customer Attribute values by Group and Customer Id

Create Helper file Data.php

Path: {VendorName}/{ModuleName}/Helper/Data.php

<?php

namespace {VendorName}\{ModuleName}\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) {
        $this->customerRepositoryInterface = $customerRepositoryInterface;
        parent::__construct($context);
    }

    public function getCustomerDataObject($customerId) {
        $customer = $this->customerRepositoryInterface->getById($customerId);
        return $customerAttributeData = $customer->__toArray();
    }
}

?>

Create Block file index.php

Path: {VendorName}/{ModuleName}/Block/Index/index.php

<?php
namespace {VendorName}\{ModuleName}\Block\Index;

class Index extends \Magento\Framework\View\Element\Template {

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
        array $data = []
    ) {
        $this->customerFactory = $customerFactory;
        parent::__construct($context, $data);
    }

    public function getCustomer($customergroupId)
    {
        $customerCollection = $this->customerFactory->create();
        $customerCollection->addFieldToFilter('group_id',$customergroupId);
        return $customerCollection;
    }
}

?>

Create phtml file index.phtml

Path: {VendorName}/{ModuleName}/view/frontend/templates/index.phtml

<?php

    $customerHelper = $this->helper(\{VendorName}\{ModuleName}\Helper\Data::class);

    $groupId = 4;
    $customerCollection = $block->getCustomer($groupId);

    foreach ($customerCollection as $customer) {

        $customerId = $customer->getId();
        $customerAttributeData = $customerHelper->getCustomerDataObject($customerId);
        echo "<pre>";
        print_r($customerAttributeData); //Get All Attribute Values of Customer

        echo $customer->getFirstname().' '.$customer->getLastname();

        if (isset($customerAttributeData['custom_attributes']['{attribute_code}'])) {
        $attributeCode = $customerAttributeData['custom_attributes']['{attribute_code}']['value'];
            if (!empty($attributeCode)) {
                echo '<div class="field customer-sale-leader">';
                echo $attributeCode;
                echo '</div>';
            }
        }

    }
?>

Follow above steps It's work for you.

Thanks!

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.