Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure plugin to send invoice when order is placed #179

Open
aleksmark opened this issue May 21, 2020 · 1 comment
Open

Configure plugin to send invoice when order is placed #179

aleksmark opened this issue May 21, 2020 · 1 comment
Labels
Help Wanted Issues needing help and clarification.

Comments

@aleksmark
Copy link

Is this possible to achieve this by overriding the default behavior of the state machine https://github.com/Sylius/InvoicingPlugin#extension-points

or I would need to override classes and extend the plugin myself?

@TELLO0815
Copy link

TELLO0815 commented Sep 1, 2020

I did like this:

service.yml

    app.listener.send_invoice_on_order:
        class: App\EventListener\SendInvoiceOnOrderListener
        arguments: ['@sylius_invoicing_plugin.creator.invoice', '@sylius_invoicing_plugin.custom_repository.invoice', '@sylius_invoicing_plugin.email.invoice_email_sender', '@sylius.repository.order']
        tags:
            - { name: messenger.message_handler, event: sylius_invoicing_plugin.event_bus }
<?php

declare(strict_types=1);

namespace App\EventListener;

use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\InvoicingPlugin\Creator\InvoiceCreatorInterface;
use Sylius\InvoicingPlugin\Email\InvoiceEmailSenderInterface;
use Sylius\InvoicingPlugin\Entity\InvoiceInterface;
use Sylius\InvoicingPlugin\Event\OrderPlaced;
use Sylius\InvoicingPlugin\Exception\InvoiceAlreadyGenerated;
use Sylius\InvoicingPlugin\Repository\InvoiceRepository;

final class SendInvoiceOnOrderListener
{
    /** @var InvoiceCreatorInterface */
    private $invoiceCreator;

    /** @var InvoiceRepository */
    private $invoiceRepository;

    /** @var OrderRepositoryInterface */
    private $orderRepository;

    /** @var InvoiceEmailSenderInterface */
    private $invoiceEmailSender;

    public function __construct(
        InvoiceCreatorInterface $invoiceCreator,
        InvoiceRepository $invoiceRepository,
        InvoiceEmailSenderInterface $invoiceEmailSender,
        OrderRepositoryInterface $orderRepository
    )
    {
        $this->invoiceCreator = $invoiceCreator;
        $this->invoiceRepository = $invoiceRepository;
        $this->invoiceEmailSender = $invoiceEmailSender;
        $this->orderRepository = $orderRepository;
    }

    public function __invoke(OrderPlaced $event): void
    {
        try {
            $this->invoiceCreator->__invoke($event->orderNumber(), $event->date());
        } catch (InvoiceAlreadyGenerated $exception) {
            return;
        }
        /** @var InvoiceInterface $invoice */
        $invoice = $this->invoiceRepository->findOneByOrderNumber($event->orderNumber());

        /** @var OrderInterface $order */
        $order = $this->orderRepository->findOneBy(['number' => $event->orderNumber()]);

        /** @var CustomerInterface $customer */
        $customer = $order->getCustomer();

        $this->invoiceEmailSender->sendInvoiceEmail($invoice, $customer->getEmail());

    }
}

@GSadee GSadee added the Help Wanted Issues needing help and clarification. label Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted Issues needing help and clarification.
Projects
None yet
Development

No branches or pull requests

3 participants