{"id":11951,"date":"2021-01-11T10:36:41","date_gmt":"2021-01-11T05:06:41","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11951"},"modified":"2021-01-16T14:54:40","modified_gmt":"2021-01-16T09:24:40","slug":"magento-2-get-customer-order-collection-customer-id","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-get-customer-order-collection-customer-id\/","title":{"rendered":"Magento 2 &#8211; How to Get Customer Order Collection by Customer Id?"},"content":{"rendered":"<p>In this post I am going to explain how to get customer order collection by customer id in Magento 2.<\/p>\n<p>Using below code snippet you can get customer order data by passing customer id.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">protected $_orderCollectionFactory;\r\n    \r\npublic function __construct(\r\n    \\Magento\\Framework\\View\\Element\\Template\\Context $context,\r\n    \\Magento\\Sales\\Model\\ResourceModel\\Order\\CollectionFactory $orderCollectionFactory,\r\n    array $data = []\r\n) {\r\n    $this-&gt;_orderCollectionFactory = $orderCollectionFactory;\r\n    parent::__construct($context, $data);\r\n}\r\n\r\npublic function getCustomerOrderCollection($customerId) \r\n{\r\n    $collection = array();\r\n    if($customerId &gt; 0) {\r\n        $collection = $this-&gt;_orderCollectionFactory-&gt;create()-&gt;addFieldToFilter('customer_id', $customerId);\r\n    }\r\n    return $collection;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Customer Id\r\n$customerId = 1;\r\n\r\n$orders = $block-&gt;getCustomerOrderCollection($customerId);\r\n\r\n\/\/print all orders\r\necho \"&lt;pre&gt;\";\r\nprint_r($orders-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nforeach ($orders as $order) {\r\n    $orderId = $order-&gt;getId();\r\n    $subtotal = $order-&gt;getSubtotal();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$customerId = 1;\r\n\r\n$orderCollectionFactory = $objectManager-&gt;get('Magento\\Sales\\Model\\ResourceModel\\Order\\CollectionFactory');\r\n$orders = $orderCollectionFactory-&gt;create()-&gt;addFieldToFilter('customer_id', $customerId);\r\n\r\n\/\/print all orders\r\necho \"&lt;pre&gt;\";\r\nprint_r($orders-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\nforeach ($orders as $order) {\r\n    $orderId = $order-&gt;getId();\r\n    $subtotal = $order-&gt;getSubtotal();\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1782855562\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get customer order collection by customer id in Magento 2. Using below code snippet you can get customer order data by passing customer id. 1. Using Dependency Injection Add below code snippet in Block class. &nbsp; protected $_orderCollectionFactory; public function __construct( \\Magento\\Framework\\View\\Element\\Template\\Context $context, \\Magento\\Sales\\Model\\ResourceModel\\Order\\CollectionFactory $orderCollectionFactory,&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/magento-2-get-customer-order-collection-customer-id\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":12266,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[729,9,10,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11951"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11951"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11951\/revisions"}],"predecessor-version":[{"id":12264,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11951\/revisions\/12264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/12266"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}