I would like to add some automated comment to the history of an invoice. How do I do that?
This code is not working:
$invoice->addComment ("Versand mit ".$in_carrier." unter der Nummer ".$in_paketnummer."\n",false,true); $invoice->save();
I don't know how you get invoice object.
use Magento\Framework\App\ObjectManager; use Magento\Sales\Api\InvoiceRepositoryInterface; $invoiceRepository = ObjectManager::getInstance() ->get(InvoiceRepositoryInterface::class); $invoice = $invoiceRepository->get(1); //id of invoice $invoice->addComment("testing comment",false,true);
More info you can find Magento\Sales\Controller\Adminhtml\Order\Invoice\AddComment
Hope it will help you.
If it will work then mark as a solution or give kudos.
In Magento 2, There are three files which you need to follow up for invoice comment,
Refer below all three files and check with your code,
Above all three files use Invoice comment so please refer above files.