Right now in nopcommerce if we choose payment gateway and actually dont pay, the order is placed and our inventory is reduced, I understand that this is how it works but I want to cleanup such orders.
How can I cancel all the orders after 24 hours if not paid?
which file I can modify or how can I add a scheduled job which runs say at midnight.
With little hint I will be able to customize this code.
Hi How can I cancel all the orders after 24 hours if not paid? Thanks
Whenever I want to make changes to the program I always look for existing code that does something similar
The page http://yourwebsite.com/Admin/Order/List allows you to search for orders that are pending payment When you look behind that page the routine that is called is _orderService.SearchOrders(...)
So you can use this routine inside a Schedule Task to search the orders Then you need to cancel / delete each of the orders found
You can copy any of the existing Scheduled Task routines e.g. DeleteGuestsTask and insert the above search
In simple terms
var orders = _orderService.SearchOrders(..psIds = PaymentStatus.Pending..) foreach (var order in orders) { _orderProcessingService.CancelOrder(order, true); _orderProcessingService.DeleteOrder(order); }
1. _orderService.SearchOrders(psIds: PaymentStatus.Pending) search order takes a list of status identifiers, so I have just passed new List() containing 10 (PaymentStatus.Pending), ideally it should just accepts enum and should return that orders
2. Do I need to insert Task in ScheduleTask task table as well like DeletGuestTask (CodeFirstInstallationService.cs).
3. new ScheduleTask { Name = "Delete guests", Seconds = 600, Type = "Nop.Services.Customers.DeletePendingOrderTask, Nop.Services", Enabled = true, StopOnError = false, },
this 600 mean it will run every 10 min? so if I want to run it every 24 hours should I just say 24*60*60 ?and it will automatically run every 24 hour ?
2. Do I need to insert Task in ScheduleTask task table as well like DeletGuestTask (CodeFirstInstallationService.cs).
Yes manualy enter a record in the table - CodeFirstInstallationService only runs for fresh install
lodha13 wrote:
3. ScheduleTask - Seconds = 600 this 600 mean it will run every 10 min? so if I want to run it every 24 hours should I just say 24*60*60 ?and it will automatically run every 24 hour ?
var statuses = new List<int>(); statuses.Add((int)PaymentStatus.Pending); var orders = _orderService.SearchOrders(psIds: statuses); foreach (var order in orders) { _orderProcessingService.CancelOrder(order, true); _orderProcessingService.DeleteOrder(order); //_emailSender.SendEmail() }
i have put this code, but as per the table its failing as last success is NULL but last run is some time.
Any idea why its failing.
Also I do not see proper logs in our log folder, is it going somewhere else ?
var statuses = new List<int>(); statuses.Add((int)PaymentStatus.Pending); var orders = _orderService.SearchOrders(psIds: statuses); foreach (var order in orders) { _orderProcessingService.CancelOrder(order, true); _orderProcessingService.DeleteOrder(order); //_emailSender.SendEmail() }
I tried this code and it worked ok for me ?
lodha13 wrote:
i have put this code, but as per the table its failing as last success is NULL but last run is some time.
Sorry I cant understand what you mean - as per what table ? what is Null ?
lodha13 wrote:
Also I do not see proper logs in our log folder, is it going somewhere else ?
Are you running in debugger ? Does the program crash with error ?
Have you set stdoutLogEnabled="True" in web.config
I mean in ScheduleTask table we are schedule of each task and its last run time and last success. So I see timestamp but last success is null meaning the process has failed.
Do you mean the code I put has worked for you?
I don't know what's the failure is but definitely it has failed as last run is null in table.
No it doesn't crash but silently fails as stoponerror is false.
I mean in ScheduleTask table we are schedule of each task and its last run time and last success. So I see timestamp but last success is null meaning the process has failed. Do you mean the code I put has worked for you?
Ok I understand - I ran this code in a test to check there was no issue - worked fine
So not sure what problem is - for me I would check as two things. 1. Comment out any new code in task - just make sure task code is working and runs each time - no error 2. Then add in your code and check again
check the logs table and found below error, doesnt say why it cannot cancel order.
Nop.Core.NopException: Cannot do cancel for order. at Nop.Services.Orders.OrderProcessingService.CancelOrder(Order order, Boolean notifyCustomer) in D:\Abhishek\Work\MGactive\MGAcive\nopCommerce_3.90_Source\Libraries\Nop.Services\Orders\OrderProcessingService.cs:line 2178 at Nop.Services.Orders.DeletePendingOrderTask.Execute() in D:\Abhishek\Work\MGactive\MGAcive\nopCommerce_3.90_Source\Libraries\Nop.Services\Orders\DeletePendingOrderTask.cs:line 37 at Nop.Services.Tasks.Task.Execute(Boolean throwException, Boolean dispose, Boolean ensureRunOnOneWebFarmInstance) in D:\Abhishek\Work\MGactive\MGAcive\nopCommerce_3.90_Source\Libraries\Nop.Services\Tasks\Task.cs:line 163
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.