1.Add new property in CustomerOrderListModel.OrderDetailsModel
public bool CanCancelOrder { get; set; }
2.Add new link in view(~/Views/Order/CustomerOrder.cshtml) so that customer can delete the order
@if (order.CanCancelOrder)
{
<input type="button" value="Cancel" class="button-2 cancel-details-button" />
}
3. Now add new method in OrderProcessingService.cs and its implementation in IOrderProcessingService
public virtual bool CanCancelOrderForCustomer(Order order)
{
if (order == null)
throw new ArgumentNullException("order");
if (order.OrderStatus == OrderStatus.Cancelled || order.OrderStatus == OrderStatus.Complete)
return false;
//if (order.PaymentStatus == PaymentStatus.Paid)
// return false;
//can add more rule like customer can cancel its product only if it is with 10 days.
return true;
}
4. Now In OrderController.cs in PrepareCustomerOrderListModel declare your property and call the method created in OrderProcessingService
protected virtual CustomerOrderListModel PrepareCustomerOrderListModel()
{
var model = new CustomerOrderListModel();
var orders = _orderService.SearchOrders(storeId: _storeContext.CurrentStore.Id,
customerId: _workContext.CurrentCustomer.Id);
foreach (var order in orders)
{
var orderModel = new CustomerOrderListModel.OrderDetailsModel
{
Id = order.Id,
CreatedOn = _dateTimeHelper.ConvertToUserTime(order.CreatedOnUtc, DateTimeKind.Utc),
OrderStatusEnum = order.OrderStatus,
OrderStatus = order.OrderStatus.GetLocalizedEnum(_localizationService, _workContext),
PaymentStatus = order.PaymentStatus.GetLocalizedEnum(_localizationService, _workContext),
ShippingStatus = order.ShippingStatus.GetLocalizedEnum(_localizationService, _workContext),
IsReturnRequestAllowed = _orderProcessingService.IsReturnRequestAllowed(order),
CanCancelOrder = _orderProcessingService.CanCancelOrderForCustomer(order),
CanReturnOrder = _orderProcessingService.CanReturnOrder(order)
};
var orderTotalInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderTotal, order.CurrencyRate);
orderModel.OrderTotal = _priceFormatter.FormatPrice(orderTotalInCustomerCurrency, true, order.CustomerCurrencyCode, false, _workContext.WorkingLanguage);
model.Orders.Add(orderModel);
}
var recurringPayments = _orderService.SearchRecurringPayments(_storeContext.CurrentStore.Id,
_workContext.CurrentCustomer.Id);
foreach (var recurringPayment in recurringPayments)
{
var recurringPaymentModel = new CustomerOrderListModel.RecurringOrderModel
{
Id = recurringPayment.Id,
StartDate = _dateTimeHelper.ConvertToUserTime(recurringPayment.StartDateUtc, DateTimeKind.Utc).ToString(),
CycleInfo = string.Format("{0} {1}", recurringPayment.CycleLength, recurringPayment.CyclePeriod.GetLocalizedEnum(_localizationService, _workContext)),
NextPayment = recurringPayment.NextPaymentDate.HasValue ? _dateTimeHelper.ConvertToUserTime(recurringPayment.NextPaymentDate.Value, DateTimeKind.Utc).ToString() : "",
TotalCycles = recurringPayment.TotalCycles,
CyclesRemaining = recurringPayment.CyclesRemaining,
InitialOrderId = recurringPayment.InitialOrder.Id,
CanCancel = _orderProcessingService.CanCancelRecurringPayment(_workContext.CurrentCustomer, recurringPayment),
};
model.RecurringOrders.Add(recurringPaymentModel);
}
return model;
}
5. Write an action method for canceling any product in OrderController.cs
public ActionResult Cancel(int orderId)
{
var order = _orderService.GetOrderById(orderId);
if (order == null || order.Deleted || _workContext.CurrentCustomer.Id != order.CustomerId)
return new HttpUnauthorizedResult();
//Add some more validation check
if(_orderProcessingService.CanCancelOrder(order))
{
_orderProcessingService.CancelOrder(order, true);
}
return RedirectToRoute("CustomerOrders");
}
6. Register new route in RouteProvider under RegisterRoutes
routes.MapLocalizedRoute("CancelOrder",
"cancelorder/{orderId}",
new { controller = "Order", action = "Cancel" },
new { orderId = @"\d+" },
new[] { "Nop.Web.Controllers" });
7. In your view(~/Views/Order/CustomerOrder.cshtml) define the cancel button click event
@if (order.CanCancelOrder)
{
<input type="button" value="Cancel" class="button-2 cancel-details-button" onclick="setLocation('@Url.RouteUrl("CancelOrder", new { orderId = order.Id })')" />
}
Thats it !!!!!!
This was coded in 3.60