{"id":902,"date":"2020-03-25T07:51:51","date_gmt":"2020-03-25T07:51:51","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/25\/get-customer-data-from-attribute-value-in-magento-2\/"},"modified":"2025-05-22T14:40:43","modified_gmt":"2025-05-22T09:10:43","slug":"get-customer-data-from-attribute-value-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-customer-data-from-attribute-value-in-magento-2\/","title":{"rendered":"How To Get Customer Data From Attribute Value In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 is a preferable choice for E-commerce stores, owing to the flexibility and customization options it offers. One such example is posted here that allows to&nbsp;<em><strong>get customer data from attribute value in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As online stores evolve, offering advanced features is a must. One such similar situation was the case in a client\u2019s store where I needed to get the customer\u2019s data from the attribute value even when it is not stored in the session, i.e., the customer is not logged in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, you can use the below solution to build advanced functionalities where you need to get the customers\u2019&nbsp; data when it is not stored in the session, but only using the attribute value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the below code, if the attribute value is unique per customer, one customer data is obtained, else the collection of customers\u2019 data is obtained for that attribute value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Get Customer Data From Attribute Value In Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Customer\\Model\\ResourceModel\\Customer\\CollectionFactory;\nprotected $customerFactory;\npublic function __construct(CollectionFactory $customerFactory)\n{\n    $this->customerFactory = $customerFactory;\n}\npublic function getCustomerByAttirbute($val)\n{\n    try {\n        $customer = $this->customerFactory->create()\n            ->addAttributeToSelect(\"*\")\n            ->addAttributeToFilter(\"attribute_code\", [\"eq\" => $val])\n            ->getFirstItem();\n        return $customer;\n    } catch (\\Exception $e) {\n        $this->_logger->info(\"Error\" . $e->getMessage());\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with the Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 is a preferable choice for E-commerce stores, owing to the flexibility and customization options it offers. One such example is posted here that&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-902","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=902"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/902\/revisions"}],"predecessor-version":[{"id":15010,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/902\/revisions\/15010"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}