Site icon MageComp Blog

How to Delete a Specified Order Status Comment in Magento 2?

How to Delete a Specified Order Status Comment in Magento 2

Hello Magento Friends,

In today’s blog, we will learn How to Delete a Specified Order Status Comment in Magento 2?

In Magento 2, an order status comment is a note or message that can be added to an order to provide additional information or updates about its status. These comments can serve as a communication tool between different persons involved in order processing, helping to track the history and progress of the order.

Admin can create custom order status, which needs explanation. Here comes the need for adding order status comments in Magento 2.

If you have unnecessary order status comments, it is advisable to delete them. You can delete specified order status comments in Magento 2 using the below method.

Steps to Delete a Specified Order Status Comment in Magento 2:

Step 1: Create a root script to Delete a specified order status comment at the below file path

magento_root_directory\Deletecomment.php

Add the code as follows

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

try {
	$orderID = 71; // Replace with the desired order ID
	$commentID = 22; // Replace with the desired comment ID

	$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderID);
	$commentsCollection = $order->getStatusHistoryCollection();

	foreach ($commentsCollection as $comment) {
	    if ($comment->getId() == $commentID) {
		$comment->delete();
		break;
	    }
	}

	$order->save();

	// Output a success message
	echo 'Order status comments deleted successfully...!';
}
catch (\Exception $e){
    echo "****** Exception Throw ************* \n";
    echo $e->getMessage() . " \n";
}

Output:

Before running script:

After running script:

Conclusion:

This way, you can delete order status comments in Magento 2. Share the article with your friends and stay updated with us for more Magento 2 solutions.

Happy Coding!

Exit mobile version