Home > Back-end >  Resend order confirmation emails for some orders in Shopware 6
Resend order confirmation emails for some orders in Shopware 6

Time:07-27

Because of an error which is now fixed, our customers did not received order confirmation emails for a while.

Is there a way to resent the order emails for some orders once?

We know about the plugin, but as this is supposed be needed only once, it's kind of overkill to install, test and deploy a plugin.

Are there some code snippets or a bin/console command to accomplish this task? Or is it even possible via the admin panel?

CodePudding user response:

It is possible to define a flowbuilder flow to send order confirmation mails, but you'd to define an appropriate trigger action of course, that you can trigger yourself for the affected orders. After the oneshot command you could disable/delete the flow. Not sure if this fits your needs...

CodePudding user response:

You could write your own CLI command to re-fire the event when an order is placed.

// $this->orderRepostitory - DI service `order.repository`
// $this->orderConverter - DI service `Shopware\Core\Checkout\Cart\Order\OrderConverter`
// $this->eventDispatcher - DI service `event_dispatcher`

$criteria = new Criteria();
$criteria
    ->addAssociation('orderCustomer.customer')
    ->addAssociation('orderCustomer.salutation')
    ->addAssociation('deliveries.shippingMethod')
    ->addAssociation('deliveries.shippingOrderAddress.country')
    ->addAssociation('deliveries.shippingOrderAddress.countryState')
    ->addAssociation('transactions.paymentMethod')
    ->addAssociation('lineItems.cover')
    ->addAssociation('currency')
    ->addAssociation('addresses.country')
    ->addAssociation('addresses.countryState')
    ->getAssociation('transactions')->addSorting(new FieldSorting('createdAt'));

// add a filter for the orders for which no mail has been sent before
$criteria->addFilter(new RangeFilter('createdAt', [RangeFilter::LTE => (new \DateTime('2020-01-01'))->format(\DATE_ATOM)]));

$orderEntities = $this->orderRepository->search($criteria, $context->getContext())->getEntities();

foreach ($orderEntities as $orderEntity) {
    $salesChannelContext = $this->orderConverter->assembleSalesChannelContext($orderEntity, Context::createDefaultContext());
    $event = new CheckoutOrderPlacedEvent(
        $salesChannelContext->getContext(),
        $orderEntity,
        $salesChannelContext->getSalesChannel()->getId()
    );
    $this->eventDispatcher->dispatch($event);
}

This should in theory also trigger the mails to be sent once more. Obviously try this with a test order before mass dispatching events like this. Also you should probably do this in smaller batches to avoid memory issues.

  • Related