blog-banner

How to Delete an Order If The Order is in Cart for N Days

  • Drupal
  • Drupal 7
  • Drupal Planet
  • E-Commerce

In drupal commerce, the order will be created when a product is added to the cart and the order will exist until the order is deleted or the order status is changed to completed.

We have an e-commerce site in which most of the orders are idle in the shopping cart. So we decided to delete the order if the order is in the shopping cart for more than 'N' days or weeks. I have written a custom Drush command for deleting the order.

The following code is used to delete an order if it is in the cart for more than 25 days.

/**

* Implements hook_drush_command().

*/

function dt_admin_drush_command() {

 $items = array();

 // Name of the drush command.

 $items['order-delete'] = array(

   'description' => 'Delete the order if order is in cart for 3 months',

   'callback' => 'drush_order_delete',

 );

 return $items;

}




function drush_order_delete() {

//  Load all order which is in the cart.

 $orders = commerce_order_load_multiple(array(), array('status'=>'cart'), TRUE);

 foreach ($orders as $order) {

    // check order time.

   if ((REQUEST_TIME - $order->changed) >= (25 * 60 * 60 * 24)) {

     $order_ids[] = $order->order_id;

   }

 }

}

To delete the order which is the cart for more than 25 days, use the following Drush command:

drush order-delete

For more details about creating a custom Drush command, visit this blog.

Get awesome tech content in your inbox