Get the date of last purchase of product, by customerMagento - Expected Delivery date on Product PageMagento Customer Grid - Last Order DateProduct last modified date & who modified itCustomer last logged in - Weird Date plus duplicateCustomer grid “last logged in” column not showing online usersHow can we get the date of when product came in stock in Magento 2?show last updated date time field in address magento adminProgramatically get special price products that are active by checking the end date?Magento 2 - Last inserted customerhow to get customer last visited date in magento2?

Why are examinees often not allowed to leave during the start and end of an exam?

My mom helped me cosign a car and now she wants to take it

SQL Server Ignoring Instance name when using port number of different instance

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

Lenovo Legion PXI-E61 Media Test Failure, Check Cable. Exiting PXE ROM. Then restarts and works fine

usage of y" not just for locations?

Advantages of using bra-ket notation

What prevents a US state from colonizing a smaller state?

How do I tell my girlfriend she's been buying me books by the wrong author for the last nine months?

Why doesn't SpaceX land boosters in Africa?

What is the point of using the kunai?

Why should I allow multiple IP addresses on a website for a single session?

Could citing a database like libgen get one into trouble?

Avoiding repetition when using the "snprintf idiom" to write text

Why can't i use !(single pattern) in zsh even after i turn on kshglob?

GFCI versus circuit breaker

Does friction always oppose motion?

Sentences with no verb, but an ablative

Which are more efficient in putting out wildfires: planes or helicopters?

Does the Grothendieck group of finitely generated modules form a commutative ring where the multiplication structure is induced from tensor product?

Did the Shuttle payload bay have illumination?

Cannot overlay, because ListPlot does not draw same X range despite the same PlotRange

What is the meaning of "it" in "as luck would have it"?

2019 2-letters 33-length list



Get the date of last purchase of product, by customer


Magento - Expected Delivery date on Product PageMagento Customer Grid - Last Order DateProduct last modified date & who modified itCustomer last logged in - Weird Date plus duplicateCustomer grid “last logged in” column not showing online usersHow can we get the date of when product came in stock in Magento 2?show last updated date time field in address magento adminProgramatically get special price products that are active by checking the end date?Magento 2 - Last inserted customerhow to get customer last visited date in magento2?













1















How can I get the last date that a customer has purchased a product?



Thank you










share|improve this question


























    1















    How can I get the last date that a customer has purchased a product?



    Thank you










    share|improve this question
























      1












      1








      1








      How can I get the last date that a customer has purchased a product?



      Thank you










      share|improve this question














      How can I get the last date that a customer has purchased a product?



      Thank you







      magento2 product customer






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 24 '18 at 9:31









      BawsiBawsi

      988 bronze badges




      988 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Use this code to get customer's last order and then any order info:



          use MagentoSalesModelOrderRepository;
          use MagentoFrameworkApiFilterBuilder;
          use MagentoFrameworkApiSearchCriteriaBuilder;
          use MagentoFrameworkApiSortOrderBuilder;

          public function __construct(
          OrderRepository $orderRepository,
          SearchCriteriaBuilder $searchCriteriaBuilder,
          FilterBuilder $filterBuilder,
          SortOrderBuilder $sortOrderBuilder
          )
          $this->orderRepository = $orderRepository;
          $this->searchCriteriaBuilder = $searchCriteriaBuilder;
          $this->filterBuilder = $filterBuilder;
          $this->sortOrderBuilder = $sortOrderBuilder;


          /**
          * @param int $customerId
          * @return OrderSearchResultInterface
          */
          public function getCustomerOrders(int $customerId): OrderSearchResultInterface

          $this->searchCriteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
          $this->searchCriteriaBuilder->addFilter(OrderInterface::STATUS, ['canceled', 'holded'], 'nin');
          $searchCriteria = $this->searchCriteriaBuilder->create();
          $searchCriteria->setSortOrders(
          [
          $this->sortOrderBuilder
          ->setField(OrderInterface::CREATED_AT)
          ->setDirection(SortOrder::SORT_DESC)
          ->create()
          ]
          );
          $searchResults = $this->orderRepository->getList($searchCriteria);

          return $searchResults;


          public function getLastOrder()

          $result = null;
          $searchResults = $this->getCustomerOrders(<customer_id_here>);

          if ($searchResults->getTotalCount() > 0)
          $orders = $searchResults->getList();
          /** @var MagentoSalesModelOrder $lastOrder */
          $lastOrder = reset($orders);
          $result = $lastOrder;

          return $result;






          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f243484%2fget-the-date-of-last-purchase-of-product-by-customer%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Use this code to get customer's last order and then any order info:



            use MagentoSalesModelOrderRepository;
            use MagentoFrameworkApiFilterBuilder;
            use MagentoFrameworkApiSearchCriteriaBuilder;
            use MagentoFrameworkApiSortOrderBuilder;

            public function __construct(
            OrderRepository $orderRepository,
            SearchCriteriaBuilder $searchCriteriaBuilder,
            FilterBuilder $filterBuilder,
            SortOrderBuilder $sortOrderBuilder
            )
            $this->orderRepository = $orderRepository;
            $this->searchCriteriaBuilder = $searchCriteriaBuilder;
            $this->filterBuilder = $filterBuilder;
            $this->sortOrderBuilder = $sortOrderBuilder;


            /**
            * @param int $customerId
            * @return OrderSearchResultInterface
            */
            public function getCustomerOrders(int $customerId): OrderSearchResultInterface

            $this->searchCriteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
            $this->searchCriteriaBuilder->addFilter(OrderInterface::STATUS, ['canceled', 'holded'], 'nin');
            $searchCriteria = $this->searchCriteriaBuilder->create();
            $searchCriteria->setSortOrders(
            [
            $this->sortOrderBuilder
            ->setField(OrderInterface::CREATED_AT)
            ->setDirection(SortOrder::SORT_DESC)
            ->create()
            ]
            );
            $searchResults = $this->orderRepository->getList($searchCriteria);

            return $searchResults;


            public function getLastOrder()

            $result = null;
            $searchResults = $this->getCustomerOrders(<customer_id_here>);

            if ($searchResults->getTotalCount() > 0)
            $orders = $searchResults->getList();
            /** @var MagentoSalesModelOrder $lastOrder */
            $lastOrder = reset($orders);
            $result = $lastOrder;

            return $result;






            share|improve this answer



























              0














              Use this code to get customer's last order and then any order info:



              use MagentoSalesModelOrderRepository;
              use MagentoFrameworkApiFilterBuilder;
              use MagentoFrameworkApiSearchCriteriaBuilder;
              use MagentoFrameworkApiSortOrderBuilder;

              public function __construct(
              OrderRepository $orderRepository,
              SearchCriteriaBuilder $searchCriteriaBuilder,
              FilterBuilder $filterBuilder,
              SortOrderBuilder $sortOrderBuilder
              )
              $this->orderRepository = $orderRepository;
              $this->searchCriteriaBuilder = $searchCriteriaBuilder;
              $this->filterBuilder = $filterBuilder;
              $this->sortOrderBuilder = $sortOrderBuilder;


              /**
              * @param int $customerId
              * @return OrderSearchResultInterface
              */
              public function getCustomerOrders(int $customerId): OrderSearchResultInterface

              $this->searchCriteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
              $this->searchCriteriaBuilder->addFilter(OrderInterface::STATUS, ['canceled', 'holded'], 'nin');
              $searchCriteria = $this->searchCriteriaBuilder->create();
              $searchCriteria->setSortOrders(
              [
              $this->sortOrderBuilder
              ->setField(OrderInterface::CREATED_AT)
              ->setDirection(SortOrder::SORT_DESC)
              ->create()
              ]
              );
              $searchResults = $this->orderRepository->getList($searchCriteria);

              return $searchResults;


              public function getLastOrder()

              $result = null;
              $searchResults = $this->getCustomerOrders(<customer_id_here>);

              if ($searchResults->getTotalCount() > 0)
              $orders = $searchResults->getList();
              /** @var MagentoSalesModelOrder $lastOrder */
              $lastOrder = reset($orders);
              $result = $lastOrder;

              return $result;






              share|improve this answer

























                0












                0








                0







                Use this code to get customer's last order and then any order info:



                use MagentoSalesModelOrderRepository;
                use MagentoFrameworkApiFilterBuilder;
                use MagentoFrameworkApiSearchCriteriaBuilder;
                use MagentoFrameworkApiSortOrderBuilder;

                public function __construct(
                OrderRepository $orderRepository,
                SearchCriteriaBuilder $searchCriteriaBuilder,
                FilterBuilder $filterBuilder,
                SortOrderBuilder $sortOrderBuilder
                )
                $this->orderRepository = $orderRepository;
                $this->searchCriteriaBuilder = $searchCriteriaBuilder;
                $this->filterBuilder = $filterBuilder;
                $this->sortOrderBuilder = $sortOrderBuilder;


                /**
                * @param int $customerId
                * @return OrderSearchResultInterface
                */
                public function getCustomerOrders(int $customerId): OrderSearchResultInterface

                $this->searchCriteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
                $this->searchCriteriaBuilder->addFilter(OrderInterface::STATUS, ['canceled', 'holded'], 'nin');
                $searchCriteria = $this->searchCriteriaBuilder->create();
                $searchCriteria->setSortOrders(
                [
                $this->sortOrderBuilder
                ->setField(OrderInterface::CREATED_AT)
                ->setDirection(SortOrder::SORT_DESC)
                ->create()
                ]
                );
                $searchResults = $this->orderRepository->getList($searchCriteria);

                return $searchResults;


                public function getLastOrder()

                $result = null;
                $searchResults = $this->getCustomerOrders(<customer_id_here>);

                if ($searchResults->getTotalCount() > 0)
                $orders = $searchResults->getList();
                /** @var MagentoSalesModelOrder $lastOrder */
                $lastOrder = reset($orders);
                $result = $lastOrder;

                return $result;






                share|improve this answer













                Use this code to get customer's last order and then any order info:



                use MagentoSalesModelOrderRepository;
                use MagentoFrameworkApiFilterBuilder;
                use MagentoFrameworkApiSearchCriteriaBuilder;
                use MagentoFrameworkApiSortOrderBuilder;

                public function __construct(
                OrderRepository $orderRepository,
                SearchCriteriaBuilder $searchCriteriaBuilder,
                FilterBuilder $filterBuilder,
                SortOrderBuilder $sortOrderBuilder
                )
                $this->orderRepository = $orderRepository;
                $this->searchCriteriaBuilder = $searchCriteriaBuilder;
                $this->filterBuilder = $filterBuilder;
                $this->sortOrderBuilder = $sortOrderBuilder;


                /**
                * @param int $customerId
                * @return OrderSearchResultInterface
                */
                public function getCustomerOrders(int $customerId): OrderSearchResultInterface

                $this->searchCriteriaBuilder->addFilter(OrderInterface::CUSTOMER_ID, $customerId);
                $this->searchCriteriaBuilder->addFilter(OrderInterface::STATUS, ['canceled', 'holded'], 'nin');
                $searchCriteria = $this->searchCriteriaBuilder->create();
                $searchCriteria->setSortOrders(
                [
                $this->sortOrderBuilder
                ->setField(OrderInterface::CREATED_AT)
                ->setDirection(SortOrder::SORT_DESC)
                ->create()
                ]
                );
                $searchResults = $this->orderRepository->getList($searchCriteria);

                return $searchResults;


                public function getLastOrder()

                $result = null;
                $searchResults = $this->getCustomerOrders(<customer_id_here>);

                if ($searchResults->getTotalCount() > 0)
                $orders = $searchResults->getList();
                /** @var MagentoSalesModelOrder $lastOrder */
                $lastOrder = reset($orders);
                $result = $lastOrder;

                return $result;







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jun 24 at 9:22









                Sergey UskovSergey Uskov

                713 bronze badges




                713 bronze badges



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f243484%2fget-the-date-of-last-purchase-of-product-by-customer%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form