Delete Specific order status history comment in Magento 2 used to remove unnecessary comments for the order.
Remove Order Comment, First Loads a specified order status comment object by comment id, pass responses object to the delete method of the Sales module OrderStatusHistoryRepositoryInterface API.
Check the given snippet code to remove a status comment for the order,
<?php
namespace Jesadiya\DeleteStatusComment\Model;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Psr\Log\LoggerInterface;
class DeleteStatusComment
{
/** @var OrderStatusHistoryRepositoryInterface */
private $orderStatusRepository;
/** @var LoggerInterface */
private $logger;
public function __construct(
OrderStatusHistoryRepositoryInterface $orderStatusRepository,
LoggerInterface $logger
) {
$this->orderStatusRepository = $orderStatusRepository;
$this->logger = $logger;
}
/**
* add comment to the order history
*
* @param int $commentId
* @return OrderStatusHistoryInterface|null
*/
public function removeStatusFromOrder(int $commentId)
{
$result = false;
try {
$orderStatusCommentObject = $this->orderStatusRepository->get($commentId);
//delete specific comment
$result = $this->orderStatusRepository->delete($orderStatusCommentObject);
} catch (NoSuchEntityException $exception) {
$this->logger->error($exception->getMessage());
}
return $result;
}
}
Call method with a required parameter, to delete order status comment
$statusCommentId = 5; //valid order status comment id $result = $this->removeStatusFromOrder($statusCommentId);
Output: boolean
