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()
new
with a PHP interface.new OrderInterface
is going to give you an error message.