artur
artur

Reputation: 684

Get customers number of orders

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.

Upvotes: 0

Views: 163

Answers (1)

DavidG
DavidG

Reputation: 119156

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;
}

Upvotes: 1

Related Questions