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

 

Hello magento friends todays in this blog post im share with you to How to Delete a Specified Order Status Comment in Magento 2.


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


In Magento 2, deleting a specified order status comment involves performing a custom code implementation. Keep in mind that working with database operations can be risky, so make sure to back up your database before proceeding. Additionally, it's essential to exercise caution when deleting data from the database directly.


To delete a specified order status comment in Magento 2, follow these steps:


  1. Identify the order and the specific comment you want to delete. You'll need the comment ID.
  2. Create a custom PHP script or use an existing Magento 2 module to execute the deletion operation.
  3. Connect to the Magento 2 database and locate the 'sales_order_status_history' table.
  4. Execute a database query to delete the comment with the specified comment ID.


Here's a sample PHP script you can use to delete a specified order status comment:



<?php

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';

// Replace 'Your_Order_Id' and 'Your_Comment_Id' with the actual order ID and comment ID you want to delete.

$orderIncrementId = 'Your_Order_Id';

$commentId = 'Your_Comment_Id';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();


// Load the order by increment ID

$order = $objectManager->get('Magento\Sales\Model\Order')->loadByIncrementId($orderIncrementId);


if ($order->getId()) {

    // Fetch the status history collection of the order

    $statusHistoryCollection = $order->getStatusHistories();


    foreach ($statusHistoryCollection as $statusHistory) {

        if ($statusHistory->getId() == $commentId) {

            // Delete the comment from the database

            $statusHistory->delete();

            break;

        }

    }


    // Save the order to reflect the changes

    $order->save();


    echo 'Order status comment with ID ' . $commentId . ' has been deleted successfully.';

} else {

    echo 'Order not found with increment ID ' . $orderIncrementId . '.';

}


Ensure you have the necessary permissions to execute this script and access the Magento 2 database. Also, as mentioned before, create a backup before executing any direct database operations.


Please note that manually deleting data from the database can lead to data integrity issues. A safer approach would be to create a custom module that provides a UI to manage order status comments and handles the deletion process securely through Magento 2 models and repositories. This way, you avoid direct database operations and adhere to Magento's coding standards.





Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post