I am trying to programmatically create the shipment and I succeeded in this operation.
To do this I create a new CLI Command which I obviously run by command line.
I have only 1 storeview and the default locale is Italian.
The problem is about sending the email to the customer because it is always sent in English.
I try different ways to send the shipment email and the last is using the class
Magento\Sales\Model\Order\Email\Sender\ShipmentSender
in this way
$this->shipmentSender->send($shipment);
after had set
$this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
I also tried the class
Magento\Shipping\Model\ShipmentNotifier
in this way
$this->shipmentNotifier->notify($shipment);
I also try to embed the code inside a statement of EnvironmentEmulation using
Magento\Store\Model\App\Emulation
using the following code
$this->emulation->startEnvironmentEmulation($order->getStoreId());
// send shipping confirmation e-mail to customer
$this->shipmentNotifier->notify($shipment);
$this->emulation->stopEnvironmentEmulation();
I don't know if this is a bug, as many other I found or if I'm wrong.
Note: if i create and send the shipment from backend the email is in the correct language.
EDIT:
I try executing the code by external php script using ObjectManager with the following code:
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$orderId = "000000009";
$order = $obj->get('Magento\Sales\Model\OrderRepository')->get($orderId);
$notifier = $obj->get('Magento\Sales\Model\Order\Shipment\NotifierInterface');
foreach($order->getShipmentsCollection() as $ship) {
$notifier->notify($order,$ship);
}
?>
…but I get the same result: the shipment email is sent in english and not i italian.
Please, help me!
Thank you.
Best Answer
Looks like this was a bug in Magento prior to version 2.3.3-p1 and has been fixed since: https://github.com/magento/magento2/issues/23333
Apparently in this commit PR :https://github.com/magento/magento2/pull/23338
Even though the PR was merged in June 2019, Magento 2.3.3-p1 was only released in January 2020.