1

My head is about to pop. I'm trying to figure out how to delete commerce orders programmatically. I created a rest export with the order IDs I want to delete. I've parsed that and put the IDs into an array. Now I want to loop through that array and delete the orders.

json parsing, and retrieving order_id work fine.

I'm so close but I can't figure out how to properly reference commerce_order_delete or commerce_cart_commerce_order_delete?

function afc_custom_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
          if ($entity->id() == 803) {
            $url = 'my url';
            $jsonString = file_get_contents($url);
            $jsonDecoded = json_decode($jsonString, true);
            $object = new stdClass();
            foreach($jsonDecoded as $row){
            if ($row['order_id'] == 121) {
                $object->order_id = $row['order_id'];
                // $order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($row['order_id']);
                commerce_cart_commerce_order_delete($object);
            }

            }
          }
    }

Error:

TypeError: Argument 1 passed to commerce_cart_commerce_order_delete() must be an instance of Drupal\commerce_order\Entity\OrderInterface, instance of stdClass given,

UPDATE #2 New Code

use Drupal\commerce_order\Entity\OrderInterface;

  if ($entity->id() == 803) {
    $url = 'https://myurl.com/pull-ghosts';
    $jsonString = file_get_contents($url);
    $jsonDecoded = json_decode($jsonString, true);

    $object = new OrderInterface;

    foreach($jsonDecoded as $row){
      if ($row['order_id'] == 121) {
        $object->setOrderNumber($row['order_id']);
        commerce_cart_commerce_order_delete($object);
        print 'deleted';
      }
    }
  }

SAME ERROR:

error: Cannot instantiate interface Drupal\commerce_order\Entity\OrderInterface in afc_custom_node_view()
3
  • 1
    There is no such thing as commerce_order_delete(). commerce_cart_commerce_order_delete() is a hook implementation and should never be invoked manually. Commerce entities are deleted like any other Drupal entity. $order->delete(); or $order_storage->delete([$order]); Feb 7, 2018 at 23:36
  • 1
    Also, you don't use new with a PHP interface. new OrderInterface is going to give you an error message.
    – apaderno
    Feb 8, 2018 at 9:15
  • You have the order id, so why not load the order object with that? Something like Order::load($id); Actually dunno the base class of the Interface.
    – DiDebru
    Feb 8, 2018 at 10:39

3 Answers 3

3

Loading it like a normal entity did the trick.

How to load and delete commerce order programmatically

$ord_id=121;
$order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($ord_id);
$order->delete();

Here is the entire snippet on my ultimate goal which was deleting abandoned orders in the cart. How to remove commerce orders automatically

How to delete ghost commerce registrations in cart

Custom cron to delete commerce orders programmatically

Load a view programmatically loop through values and delete orders

/**
 * Implements hook_cron().
 */
 function my_module_cron() {
   //load a view showing orders still in cart
   $view = \Drupal\views\Views::getView('commerce_carts');
   //the name of your display, i chose a simple rest display with the order_id field
   $view->setDisplay('rest_export_1');
   $view->execute();

   foreach($view->result as $row){
     //load the order
     $order = \Drupal::entityTypeManager()->getStorage('commerce_order')->load($row->order_id);
     $oid = $order->id();
     //delete the order
     $order->delete();
     //log to dblog
     \Drupal::logger('afc_commerce_registration')->notice('Clearing ghost registration, deleted: @oid.',
       array(
           '@oid' => $oid,
       ));
   }
 }

You can add the Created date field to the view, and test time values before deleting. In my case, it doesn't matter.

Thanks for the help everyone

1

For one, in your updated code, you cannot instantiate an Interface in PHP. This is throwing the error. If you were to need a new instance, you would instead use the Order class which implements the OrderInterface Interface.

As Orders are Entities, you should be able to simply load the Order and delete it.

Example:

use Drupal\commerce_order\Entity\Order;

$my_order = Order::load(122); // Or, your Order ID
$my_order->delete();

This should delete the Entity, which is your Order in this case.

-2

You need your object to be of the class Drupal\commerce_order\Entity\OrderInterface instead of an empty class.

Use this instead: (Update)

use Drupal\commerce_order\Entity\OrderInterface;

$object = new OrderInterface;

foreach($jsonDecoded as $row){
  if ($row['order_id'] == 121) {
    $object->setOrderNumber($row['order_id']);
    commerce_cart_commerce_order_delete($object);
    print 'deleted';
  }
}
2
  • This seems like exactly the right path, but getting an error. Update on original post: error: Cannot instantiate interface Drupal\commerce_order\Entity\OrderInterface in afc_custom_node_view() Feb 7, 2018 at 22:33
  • Same Error: Cannot instantiate interface Drupal\commerce_order\Entity\OrderInterface in Feb 7, 2018 at 23:19

Your Answer

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

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