I am creating a console task to ship orders based on XML files (How to add a shipment (tracking code + change status to shipped) programmatically? ) which I import.
Currently I am using
$context = Context::createDefaultContext();
And create a shipment:
$this->deliveryRepository->update([
[
'id' => $deliveryId,
'trackingCodes' => [$trackingCode]
]
], $context);
$context->addExtension(
MailSendSubscriber::MAIL_CONFIG_EXTENSION,
new MailSendSubscriberConfig(false)
);
$this->orderService->orderDeliveryStateTransition(
$deliveryId,
'ship',
new ParameterBag(),
$context
);
This does not send out the shipment confirmation mail.
I am assuming the reason is, because I am in the default context.
Can I / should I recreate the context from the Storefront where the order was placed in? How does this work?
(maybe using \Shopware\Core\System\SalesChannel\Context\BaseContextFactory::create ? but this is tagged as @internal )
Or is it okay to use the default context here (see Shopware 6 get context in scheduled task) and is the problem something else?
EDIT:
$context = $this->baseContextFactory->create($order->getSalesChannelId())->getContext();
does work, but I still do not receive emails.
EDIT2:
The mail problem is unrelated - I am testing this with an Unit test and it does not seem to send out mails in general. This it out of scope of this question.