diff --git a/config/packages/gift_card.yaml b/config/packages/gift_card.yaml index a3cd104..ad1755c 100644 --- a/config/packages/gift_card.yaml +++ b/config/packages/gift_card.yaml @@ -9,6 +9,14 @@ sylius_fixtures: options: amount: 5 +sylius_ui: + events: + sylius.shop.cart.coupon: + blocks: + gift_card_form: + template: 'Shop/Cart/Summary/giftCardCode.html.twig' + priority: 5 + services: App\GiftCard\: resource: '../../src/GiftCard/*' diff --git a/src/Entity/Order/Order.php b/src/Entity/Order/Order.php index 4d0dfff..64d0317 100644 --- a/src/Entity/Order/Order.php +++ b/src/Entity/Order/Order.php @@ -4,6 +4,7 @@ namespace App\Entity\Order; +use App\GiftCard\OrderProcessor\GiftCardOrderProcessor; use App\Packaging\OrderProcessor\PackagingOrderProcessor; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Core\Model\Order as BaseOrder; @@ -12,8 +13,26 @@ #[ORM\Table(name: 'sylius_order')] class Order extends BaseOrder { + #[ORM\Column(type: 'string', name: 'gift_card_code', nullable: true)] + private ?string $giftCardCode = null; + public function getPackagingTotal(): int { return $this->getAdjustmentsTotalRecursively(PackagingOrderProcessor::PACKAGING_ADJUSTMENT_TYPE); } + + public function getGiftCardTotal(): int + { + return $this->getAdjustmentsTotalRecursively(GiftCardOrderProcessor::GIFT_CARD_ADJUSTMENT); + } + + public function getGiftCardCode(): ?string + { + return $this->giftCardCode; + } + + public function setGiftCardCode(?string $giftCardCode): void + { + $this->giftCardCode = $giftCardCode; + } } diff --git a/src/GiftCard/Form/Extension/CartTypeExtension.php b/src/GiftCard/Form/Extension/CartTypeExtension.php new file mode 100644 index 0000000..3e1e184 --- /dev/null +++ b/src/GiftCard/Form/Extension/CartTypeExtension.php @@ -0,0 +1,29 @@ +add('giftCardCode', TextType::class, [ + 'label' => 'Gift card', + 'required' => false, + 'attr' => [ + 'placeholder' => 'Gift card code', + ], + ]); + } + + public static function getExtendedTypes(): iterable + { + yield CartType::class; + } +} diff --git a/src/GiftCard/OrderProcessor/GiftCardOrderProcessor.php b/src/GiftCard/OrderProcessor/GiftCardOrderProcessor.php new file mode 100644 index 0000000..e7b1500 --- /dev/null +++ b/src/GiftCard/OrderProcessor/GiftCardOrderProcessor.php @@ -0,0 +1,47 @@ +removeAdjustments(self::GIFT_CARD_ADJUSTMENT); + $giftCardCode = $order->getGiftCardCode(); + + if ($giftCardCode === null) { + return; + } + + /** @var GiftCard|null $giftCard */ + $giftCard = $this->giftCardRepository->findOneBy(['code' => $giftCardCode]); + if ($giftCard === null) { + return; + } + + $giftCardAdjustment = $this->adjustmentFactory->createWithData( + self::GIFT_CARD_ADJUSTMENT, + 'Gift card', + min($giftCard->getAmount(), $order->getTotal()) * -1, + ); + $order->addAdjustment($giftCardAdjustment); + } +} diff --git a/src/Migrations/Version20240514100514.php b/src/Migrations/Version20240514100514.php new file mode 100644 index 0000000..a859e55 --- /dev/null +++ b/src/Migrations/Version20240514100514.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE sylius_order ADD gift_card_code VARCHAR(255) DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE sylius_order DROP gift_card_code'); + } +} diff --git a/templates/Shop/Cart/Summary/giftCardCode.html.twig b/templates/Shop/Cart/Summary/giftCardCode.html.twig new file mode 100644 index 0000000..7135ec2 --- /dev/null +++ b/templates/Shop/Cart/Summary/giftCardCode.html.twig @@ -0,0 +1,8 @@ +
+
+ {{ form_widget(form.giftCardCode, {'attr': {'form': main_form }}) }} + +
+
+ {{ form_errors(form.giftCardCode) }} +
diff --git a/templates/bundles/SyliusShopBundle/Cart/Summary/_totals.html.twig b/templates/bundles/SyliusShopBundle/Cart/Summary/_totals.html.twig index 0f02442..58d00f0 100644 --- a/templates/bundles/SyliusShopBundle/Cart/Summary/_totals.html.twig +++ b/templates/bundles/SyliusShopBundle/Cart/Summary/_totals.html.twig @@ -55,6 +55,12 @@ {{ money.convertAndFormat(cart.packagingTotal) }} {% endif %} + {% if cart.giftCardTotal < 0 %} + + Gift cards + {{ money.convertAndFormat(cart.giftCardTotal) }} + + {% endif %} {{ 'sylius.ui.order_total'|trans }}: {{ money.convertAndFormat(cart.total) }}