I am looking for the Magento recommended best practice for adding a comment to an order. I can easily accomplish this using things such as $order->addCommentToStatusHistory($message)->save()
and similar actions. However I know that using a direct save
action is not recommended on models and you should instead us managers/repositories.
I have also found that there is a \Magento\Sales\Api\OrderStatusHistoryRepositoryInterface
now which provides the save
function. So what I'm trying to figure out is how to approach this properly. This is my best guess.
private $orderStatusHistoryRepository;
public function __construct(
\Magento\Sales\Api\OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
) {
$this->orderStatusHistoryRepository = $orderStatusHistoryRepository;
}
public function myFunction(\Magento\Sales\Api\Data\OrderInterface $order)
{
//... additional logic here
$history = $order->addCommentToStatusHistory($message);
$this->orderStatusHistoryRepository->save($history);
}
What I'm not certain about is if this is the proper approach AND should I also be saving the order using OrderRepositoryInterface
, or if that is unnecessary.
I have looked through a lot of questions and answers around this as well as looking through some tutorials and documentation. However all of these provide conflicting information and most of them recommend ways that I know are not Magento best practice.
If there is documentation out there that would answer my question and I have overlooked it please point me in the right direction.
This is for Magento 2.3.0.