Get customers number of orders

162 views Asked by At

How can I get customer number of orders on customer order page on public store. I have seen that already exist on admin page bu is there any setting that should be activated as this can be shown on public store.

1

There are 1 answers

0
DavidG On BEST ANSWER

Assuming the controller you are using has the _storeContext, _workContext and _orderService variables injected, this will do it:

var orderCount = _orderService.SearchOrders(
    storeId: _storeContext.CurrentStore.Id,
    customerId: _workContext.CurrentCustomer.Id)
    .Count();

If any of those variables don't exist, manually add them to your controller. Toy need to add the private variables:

private readonly IStoreContext _storeContext;
private readonly IOrderService _orderService;
private readonly IWorkContext _workContext;

Extend the constructor of the controller and add in the code to save the injected values:

public YourController(/* other parameters */
    IOrderService orderService, 
    IWorkContext workContext,
    IStoreContext storeContext)
{
    //snip
    this._orderService = orderService;
    this._shipmentService = shipmentService;
    this._workContext = workContext;
    this._storeContext = storeContext;
}