We found that, sometimes we run out of stock/inventory due to (pending payment) orders.

so to automatically cancel pending orders.

we have magento

  • I urge caution when canceling pending payment orders, especially when PayPal is involved and even more-so when the PayPal Adaptive API is used. I've encountered certain cases where a canceled order was later paid for shortly after. It's advisable to check the status of any tokens / keys that might be used for payment prior to canceling an order. I believe that the Pay Key used by Adaptive payments is valid for up to 3 hours after being created -- meaning that a valid payment could still be potentially processed even after the user navigates away from the payment page. Commented Sep 21, 2016 at 0:19

3 Answers 3


For Cancel pending orders automatically you can refer following links.

1) https://www.magentocommerce.com/magento-connect/magento-pending-orders-automatic-cancelation-with-stockreleaser-by-soon.html


Hope these will help you.

  • thank you for the reply, unfortunately both of them are very old and from what I read none work on the new version
    – Hamad
    Commented Sep 14, 2016 at 13:36

You can easily filter pending orders and mass cancel them or even better create an external script running with cron that makes an order collection check dates time anything you want then cancel them.


Below code worked for me.

 private function cancelPendingPayments() {

    $fromDate = date('Y-m-d H:i:s', strtotime('-2 hour'));
    $toDate = date('Y-m-d H:i:s', strtotime(now()));
    $orderCollection = Mage::getResourceModel('sales/order_collection');

    $orderCollection->addFieldToFilter('status', 'pending_payment')
        ->addFieldToFilter('updated_at',  array( 'from' => $fromDate, 'to' => $toDate, 'date' => true ))

    if( count( $orderCollection ) < 1 )  {

    foreach( $orderCollection as $order ) {

        $difference = $this->getDifference($order);
        if( ( $difference >= 30 ) && ( $difference < 45 ) ) {

            $emailVariables = $this->pendingPaymentsVariables( $order );

            $this->prepareEmail( Mage::getStoreConfig('clearbasket_settings/email_template/pending_payment_notify'),
                                 $emailVariables, $emailVariables['orderdetails']['customername'], $emailVariables['orderdetails']['customeremail'] );
           $history = $order->addStatusHistoryComment('Notification email sent - Cron Jobs',false);

        if( $difference > 60 ) {

             if(!$order->canCancel()) {
                  Mage::log('Order cannot be canceled anymore.',null,"order.log");
              else {
            $order->setData('state', "canceled");
            $order->setData('state', Mage_Sales_Model_Order::STATE_CANCELED); 
            $order->setActionFlag(Mage_Sales_Model_Order::ACTION_FLAG_CANCEL, true);
            Mage::log('Order cancel after : '.$difference, null, "order.log");
            $history = $order->addStatusHistoryComment('Order marked as cancelled due to pending payment - Cron Jobs.', false);



Please refer the below code link for notification emails and cron jobs. https://www.pearlbells.co.uk/cancel-pending-payments-magento/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.