-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing to use the same input for PromotionCoupon and GiftCards
Fixes #59
- Loading branch information
Showing
16 changed files
with
254 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/** @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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/DependencyInjection/Compiler/OverrideCouponFormPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGiftCardPlugin\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class OverrideCouponFormPass 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) { | ||
return; | ||
} | ||
|
||
$container->removeDefinition('setono_sylius_gift_card.form.type.promotion_coupon_to_code'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGiftCardPlugin\Form\Type; | ||
|
||
use Sylius\Bundle\PromotionBundle\Form\Type\PromotionCouponToCodeType as SyliusPromotionCouponToCodeType; | ||
use Sylius\Component\Promotion\Model\PromotionCouponInterface; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
final class PromotionCouponToCodeType extends AbstractType implements DataTransformerInterface | ||
{ | ||
/** @var SyliusPromotionCouponToCodeType */ | ||
private $decoratedForm; | ||
|
||
public function __construct(SyliusPromotionCouponToCodeType $decoratedForm) | ||
{ | ||
$this->decoratedForm = $decoratedForm; | ||
} | ||
|
||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$this->decoratedForm->buildForm($builder, $options); | ||
} | ||
|
||
public function transform($coupon): string | ||
{ | ||
return $this->decoratedForm->transform($coupon); | ||
} | ||
|
||
public function reverseTransform($code): ?PromotionCouponInterface | ||
{ | ||
return $this->decoratedForm->reverseTransform($code); | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$this->decoratedForm->configureOptions($resolver); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return HiddenType::class; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return $this->decoratedForm->getBlockPrefix(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Resources/views/templates/bundles/SyliusShopBundle/Cart/Summary/_coupon.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div id="sylius-coupon" {{ sylius_test_html_attribute('cart-promotion-coupon') }}> | ||
<div class="ui coupon action input"> | ||
{{ form_widget(form.promotionCoupon, sylius_test_form_attribute('cart-promotion-coupon-input')|sylius_merge_recursive({'attr': {'form': main_form, 'placeholder': 'sylius.ui.enter_your_code'|trans~'...'}})) }} | ||
<button type="submit" id="sylius-save" {{ sylius_test_html_attribute('apply-coupon-button') }} class="ui teal icon labeled button" form="{{ main_form }}"><i class="ticket icon"></i> {{ 'sylius.ui.apply_coupon'|trans }}</button> | ||
</div> | ||
<br> | ||
{{ form_errors(form.promotionCoupon) }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/Application/config/packages/setono_sylius_gift_card.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
imports: | ||
- { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/config.yaml" } | ||
- { resource: "@SetonoSyliusGiftCardPlugin/Resources/config/app/fixtures.yaml" } | ||
|
||
setono_sylius_gift_card: | ||
cart: | ||
use_same_input_for_promotion_and_gift_card: false |
8 changes: 8 additions & 0 deletions
8
tests/Application/templates/bundles/SyliusShopBundle/Cart/Summary/_coupon.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div id="sylius-coupon" {{ sylius_test_html_attribute('cart-promotion-coupon') }}> | ||
<div class="ui coupon action input"> | ||
{{ form_widget(form.promotionCoupon, sylius_test_form_attribute('cart-promotion-coupon-input')|sylius_merge_recursive({'attr': {'form': main_form, 'placeholder': 'sylius.ui.enter_your_code'|trans~'...'}})) }} | ||
<button type="submit" id="sylius-save" {{ sylius_test_html_attribute('apply-coupon-button') }} class="ui teal icon labeled button" form="{{ main_form }}"><i class="ticket icon"></i> {{ 'sylius.ui.apply_coupon'|trans }}</button> | ||
</div> | ||
<br> | ||
{{ form_errors(form.promotionCoupon) }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters