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?
How can I get the last date that a customer has purchased a product?
Thank you
magento2 product customer
add a comment |
How can I get the last date that a customer has purchased a product?
Thank you
magento2 product customer
add a comment |
How can I get the last date that a customer has purchased a product?
Thank you
magento2 product customer
How can I get the last date that a customer has purchased a product?
Thank you
magento2 product customer
magento2 product customer
asked Sep 24 '18 at 9:31
BawsiBawsi
988 bronze badges
988 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
add a comment |
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;
add a comment |
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;
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;
answered Jun 24 at 9:22
Sergey UskovSergey Uskov
713 bronze badges
713 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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