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

Allowing to use the same input for PromotionCoupon and GiftCards #92

Open
wants to merge 2 commits into
base: 0.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ $bundles = [

You will find the templates you need to override in the [test application](https://github.com/Setono/SyliusGiftCardPlugin/tree/master/tests/Application/templates).

If you enable the same input for Promotion and GiftCard (see [here](#using-same-input-for-promotion-and-giftcard)), remove all content of `SyliusShopBundle/Cart/Summary/_coupon.html.twig` since it is not needed.

### Extend `Product`, `Order`, `OrderRepository`, and `CustomerRepository`

**Extend `Product`**
Expand Down Expand Up @@ -283,3 +285,17 @@ setono_sylius_gift_card_shop_remove_gift_card_from_order:
The same applies for the `setono_sylius_gift_card_shop_partial_add_gift_card_to_order` route

You can also override or decorate the service `setono_sylius_gift_card.resolver.redirect_url` to define a more custom way of redirecting

## Using same input for Promotion and GiftCard

You can configure the shop to use same input for GiftCards and Promotion coupons. To do so, set this configuration :

```yaml
# config/packages/setono_sylius_gift_card.yaml
setono_sylius_gift_card:
cart:
use_same_input_for_promotion_and_gift_card: true
```

By doing so, the alias `setono_sylius_gift_card.controller.action.add_gift_card_to_order` will now point to `setono_sylius_gift_card.controller.action.add_gift_card_to_order.composed` instead of `setono_sylius_gift_card.controller.action.add_gift_card_to_order.simple`
and will use its mechanisms.
57 changes: 57 additions & 0 deletions src/Applicator/CouponCodeApplicator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Doctrine\ORM\EntityManagerInterface;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Sylius\Component\Core\Model\PromotionCouponInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\Component\Promotion\Action\PromotionApplicatorInterface;
use Sylius\Component\Promotion\Repository\PromotionCouponRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Webmozart\Assert\Assert;

final class CouponCodeApplicator implements CouponCodeApplicatorInterface
Roshyo marked this conversation as resolved.
Show resolved Hide resolved
{
/** @var PromotionCouponRepositoryInterface */
private $promotionCouponRepository;

/** @var OrderProcessorInterface */
private $orderProcessor;

/** @var EntityManagerInterface */
private $orderManager;

/** @var PromotionApplicatorInterface */
private $promotionApplicator;

public function __construct(
PromotionCouponRepositoryInterface $promotionCouponRepository,
OrderProcessorInterface $orderProcessor,
EntityManagerInterface $orderManager,
PromotionApplicatorInterface $promotionApplicator
) {
$this->promotionCouponRepository = $promotionCouponRepository;
$this->orderProcessor = $orderProcessor;
$this->orderManager = $orderManager;
$this->promotionApplicator = $promotionApplicator;
}

public function apply(OrderInterface $order, string $couponCode): void
{
$promotionCoupon = $this->promotionCouponRepository->findOneBy(['code' => $couponCode]);
if ($promotionCoupon instanceof PromotionCouponInterface) {
$order->setPromotionCoupon($promotionCoupon);
$promotion = $promotionCoupon->getPromotion();
Assert::notNull($promotion);
$this->promotionApplicator->apply($order, $promotion);
$this->orderProcessor->process($order);

$this->orderManager->flush();
} else {
throw new NotFoundHttpException('Impossible to find the coupon');
}
}
}
12 changes: 12 additions & 0 deletions src/Applicator/CouponCodeApplicatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Applicator;

use Setono\SyliusGiftCardPlugin\Model\OrderInterface;

interface CouponCodeApplicatorInterface
{
public function apply(OrderInterface $order, string $couponCode): void;
}
117 changes: 117 additions & 0 deletions src/Controller/Action/AddGiftCardOrPromotionCouponToOrderAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Setono\SyliusGiftCardPlugin\Applicator\CouponCodeApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Form\Type\AddGiftCardOrPromotionCouponToOrderType;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
use Setono\SyliusGiftCardPlugin\Resolver\RedirectUrlResolverInterface;
use Sylius\Component\Order\Context\CartContextInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Webmozart\Assert\Assert;

final class AddGiftCardOrPromotionCouponToOrderAction
{
/** @var ViewHandlerInterface */
private $viewHandler;

/** @var FormFactoryInterface */
private $formFactory;

/** @var CartContextInterface */
private $cartContext;

/** @var FlashBagInterface */
private $flashBag;

/** @var GiftCardApplicatorInterface */
private $giftCardApplicator;

/** @var RedirectUrlResolverInterface */
private $redirectRouteResolver;

/** @var CouponCodeApplicatorInterface */
private $couponCodeApplicator;

/** @var bool */
private $useSameInputForPromotionAndGiftCard;

public function __construct(
ViewHandlerInterface $viewHandler,
FormFactoryInterface $formFactory,
CartContextInterface $cartContext,
FlashBagInterface $flashBag,
GiftCardApplicatorInterface $giftCardApplicator,
RedirectUrlResolverInterface $redirectRouteResolver,
CouponCodeApplicatorInterface $couponCodeApplicator,
bool $useSameInputForPromotionAndGiftCard
) {
$this->viewHandler = $viewHandler;
$this->formFactory = $formFactory;
$this->cartContext = $cartContext;
$this->flashBag = $flashBag;
$this->giftCardApplicator = $giftCardApplicator;
$this->redirectRouteResolver = $redirectRouteResolver;
$this->couponCodeApplicator = $couponCodeApplicator;
$this->useSameInputForPromotionAndGiftCard = $useSameInputForPromotionAndGiftCard;
}

public function __invoke(Request $request): Response
{
/** @var OrderInterface|null $order */
$order = $this->cartContext->getCart();

if (null === $order) {
throw new NotFoundHttpException();
}

$addGiftCardOrPromotionCouponToOrderCommand = new AddGiftCardOrPromotionCouponToOrderCommand();
$form = $this->formFactory->create(AddGiftCardOrPromotionCouponToOrderType::class, $addGiftCardOrPromotionCouponToOrderCommand);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$giftCard = $addGiftCardOrPromotionCouponToOrderCommand->getGiftCard();
$promotionCoupon = $addGiftCardOrPromotionCouponToOrderCommand->getPromotionCoupon();
if (null !== $giftCard) {
$this->giftCardApplicator->apply($order, $giftCard);
} elseif (null !== $promotionCoupon) {
$couponCode = $promotionCoupon->getCode();
Assert::notNull($couponCode);
$this->couponCodeApplicator->apply($order, $couponCode);
}

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');

if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle(View::create([], Response::HTTP_CREATED));
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
}

if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle(View::create($form, Response::HTTP_BAD_REQUEST)->setData([
'errors' => $form->getErrors(true, true),
]));
}

$view = View::create()
->setData([
'form' => $form->createView(),
])
->setTemplate('@SetonoSyliusGiftCardPlugin/Shop/addGiftCardToOrder.html.twig')
;

return $this->viewHandler->handle($view);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Controller\Action;

use Setono\SyliusGiftCardPlugin\Model\GiftCardInterface;
use Sylius\Component\Core\Model\PromotionCouponInterface;

final class AddGiftCardOrPromotionCouponToOrderCommand
{
/** @var GiftCardInterface|null */
private $giftCard;

/** @var PromotionCouponInterface|null */
private $promotionCoupon;

public function getGiftCard(): ?GiftCardInterface
{
return $this->giftCard;
}

public function setGiftCard(?GiftCardInterface $giftCard): void
{
$this->giftCard = $giftCard;
}

public function getPromotionCoupon(): ?PromotionCouponInterface
{
return $this->promotionCoupon;
}

public function setPromotionCoupon(?PromotionCouponInterface $promotionCoupon): void
{
$this->promotionCoupon = $promotionCoupon;
}
}
53 changes: 44 additions & 9 deletions src/Controller/Action/AddGiftCardToOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Setono\SyliusGiftCardPlugin\Applicator\CouponCodeApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Applicator\GiftCardApplicatorInterface;
use Setono\SyliusGiftCardPlugin\Form\Type\AddGiftCardToOrderType;
use Setono\SyliusGiftCardPlugin\Model\OrderInterface;
Expand Down Expand Up @@ -39,20 +40,30 @@ final class AddGiftCardToOrderAction
/** @var RedirectUrlResolverInterface */
private $redirectRouteResolver;

/** @var CouponCodeApplicatorInterface */
private $couponCodeApplicator;

/** @var bool */
private $useSameInputForPromotionAndGiftCard;

public function __construct(
ViewHandlerInterface $viewHandler,
FormFactoryInterface $formFactory,
CartContextInterface $cartContext,
FlashBagInterface $flashBag,
GiftCardApplicatorInterface $giftCardApplicator,
RedirectUrlResolverInterface $redirectRouteResolver
RedirectUrlResolverInterface $redirectRouteResolver,
CouponCodeApplicatorInterface $couponCodeApplicator,
bool $useSameInputForPromotionAndGiftCard
) {
$this->viewHandler = $viewHandler;
$this->formFactory = $formFactory;
$this->cartContext = $cartContext;
$this->flashBag = $flashBag;
$this->giftCardApplicator = $giftCardApplicator;
$this->redirectRouteResolver = $redirectRouteResolver;
$this->couponCodeApplicator = $couponCodeApplicator;
$this->useSameInputForPromotionAndGiftCard = $useSameInputForPromotionAndGiftCard;
}

public function __invoke(Request $request): Response
Expand All @@ -68,18 +79,42 @@ public function __invoke(Request $request): Response
$form = $this->formFactory->create(AddGiftCardToOrderType::class, $addGiftCardToOrderCommand);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$giftCard = $addGiftCardToOrderCommand->getGiftCard();
Assert::notNull($giftCard);
$this->giftCardApplicator->apply($order, $giftCard);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$giftCard = $addGiftCardToOrderCommand->getGiftCard();
Assert::notNull($giftCard);
$this->giftCardApplicator->apply($order, $giftCard);

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');
$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');

if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle(View::create([], Response::HTTP_CREATED));
if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle(View::create([], Response::HTTP_CREATED));
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
if ($this->useSameInputForPromotionAndGiftCard) {
// Arrived here, the code applied is not a giftCard.
// Then check if it is a promotion and if so, apply it to order.
// Use NormData instead of Data since the form is invalid, Data is null
$promotionCouponCode = $form->get('giftCard')->getNormData();
if (null !== $promotionCouponCode) {
try {
$this->couponCodeApplicator->apply($order, $promotionCouponCode);

$this->flashBag->add('success', 'setono_sylius_gift_card.gift_card_added');

if ($request->isXmlHttpRequest()) {
return $this->viewHandler->handle(View::create([], Response::HTTP_CREATED));
}

return new RedirectResponse($this->redirectRouteResolver->getUrlToRedirectTo($request, 'sylius_shop_cart_summary'));
} catch (\Exception $exception) {
// Do not do anything here, let FOSRest handle the response
}
}
}
}

if ($request->isXmlHttpRequest()) {
Expand Down
34 changes: 34 additions & 0 deletions src/DependencyInjection/Compiler/DefineControllerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* This class will be used to create an alias to for `setono_sylius_gift_card.controller.action.add_gift_card_to_order`
* According to whether we want the same input for GiftCard and PromotionCoupon or not.
*/
final class DefineControllerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$useSameInput = (bool) $container->getParameter('setono_sylius_gift_card.cart.use_same_input_for_promotion_and_gift_card');

if ($useSameInput) {
$container->setAlias(
'setono_sylius_gift_card.controller.action.add_gift_card_to_order',
'setono_sylius_gift_card.controller.action.add_gift_card_to_order.composed'
);
} else {
$container->setAlias(
'setono_sylius_gift_card.controller.action.add_gift_card_to_order',
'setono_sylius_gift_card.controller.action.add_gift_card_to_order.simple'
);
}

$container->getAlias('setono_sylius_gift_card.controller.action.add_gift_card_to_order')->setPublic(true);
}
}
Loading