0

how to get customer collection using search functionality?

enter image description here

| improve this question | | | | |
  • This seems like a front controller build which is sending param of input to controller and based on ajax response populating data. Can you confirm if that is the case, and what have you tried so far? – Gagan Oct 30 '18 at 14:04
  • I have used Magento default pagination functionality. – Masud Shaikh Oct 30 '18 at 14:06
  • In the screenshot you are already getting data what else is needed? Do you mean you want the same functionality as in screenshot via search bar in header? – Gagan Oct 30 '18 at 14:07
  • I want to search for data by email id. – Masud Shaikh Oct 30 '18 at 14:09
  • Isn't that already being done in screenshot ,or do you mean you want more data, if that is the case please share the controller code, along with what data you want to get. – Gagan Oct 30 '18 at 14:10
1

app/code/Vendor/Modulename/view/frontend/templates/html/my-custom-form.phtml

<form action="<?= $block->getUrl("modulename/form/email"); ?>"
    <input type="email" name="email" value="" class="input-text" id="email" data-validate="{required:true, 'validate-email':true}">
    <button type="submit" class="action" id="submit"><span><?= $block->escapeHtml(__('Ok')) ?></span></button>
</form>

app/code/Vendor/Modulename/Controller/Form/Email.php

<?php

namespace Vendor\Modulename\Controller\Form;

use Magento\Framework\App\Action\Context;

class Email extends \Magento\Framework\App\Action\Action
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Framework\View\Result\PageFactory $pageFactory
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_resultPageFactory = $pageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        if ($this->getRequest()->isPost()) {
            $email = $this->getRequest()->getPost('email');
            $this->_coreRegistry->register('email', $email);
            return $this->_resultPageFactory->create();
        }
    }
}

app/code/Vendor/Modulename/Block/Email.php

<?php

namespace Vendor\Modulename\Block;

use Magento\Framework\View\Element\Template;

class Email extends Template
{
    protected $_coreRegistry;
    protected $_customerFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
        array $data = []
    ) {
        $this->_coreRegistry = $coreRegistry;
        $this->_customerFactory = $customerFactory;
        parent::__construct($context, $data);
    }

    public function getEmail()
    {
        return $this->_coreRegistry->registry('email');
    }

    public function getCustomerCollectionByEmail()
    {
        return $this->_customerFactory->create()->getCollection()
                    ->addAttributeToSelect("*")
                    ->addAttributeToFilter("email", $this->getEmail())
                    ->load();
    }
}

app/code/Vendor/Modulename/view/frontend/templates/html/my-custom-form.phtml

<?php 

    $customerCollectionByEmail= $block->getCustomerCollectionByEmail(); 
    foreach ($customerCollectionByEmail as $customer) {
        echo $customer->getEmail();
    }
?>

I don't tested it but it should work !

| improve this answer | | | | |
  • Hi, It is working fine. Thank you so much. – Masud Shaikh Oct 31 '18 at 10:46
  • 1
    You're welcome, good luck ! – PЯINCƏ Oct 31 '18 at 10:50

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.