Jump to content

Auto Remove Incomplete/Unpaid Orders


Ian Mead
Go to solution Solved by leemahoney3,

Recommended Posts

  • Solution

I assume you only want this to happen on Pending unpaid orders? If so, the order needs to be cancelled before deleted. The below hook should work

<?php

use Carbon\Carbon;
use WHMCS\Order\Order;

add_hook('DailyCronJob', 1, static function () {
    $daysBeforeDeletion = 14;

    Order::where('status', 'Pending')->each(function (Order $order) use ($daysBeforeDeletion, &$orders) {
        if ($order->isPaid) {
            return;
        }

        if ($order->date->addDays($daysBeforeDeletion)->toDateString() > Carbon::now()->toDateString()) {
            return;
        }

        $cancelResult = localAPI('CancelOrder', ['orderid' => $order->id]);
        if ($cancelResult['result'] === 'success') {
            localAPI('DeleteOrder', ['orderid' => $order->id]);
        }
    });
});

Update the $daysBeforeDeletion variable to how many days you wish to keep the order after its order date before its deleted. This will run once a day on your WHMCS cron job.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated