Skip to content

Commit

Permalink
Process gift card during cart processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 15, 2024
1 parent b78f70c commit 053ae6d
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/packages/gift_card.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*'
Expand Down
19 changes: 19 additions & 0 deletions src/Entity/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
29 changes: 29 additions & 0 deletions src/GiftCard/Form/Extension/CartTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\Form\Extension;

use Sylius\Bundle\OrderBundle\Form\Type\CartType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class CartTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('giftCardCode', TextType::class, [
'label' => 'Gift card',
'required' => false,
'attr' => [
'placeholder' => 'Gift card code',
],
]);
}

public static function getExtendedTypes(): iterable
{
yield CartType::class;
}
}
47 changes: 47 additions & 0 deletions src/GiftCard/OrderProcessor/GiftCardOrderProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\GiftCard\OrderProcessor;

use App\Entity\Order\Order;
use App\GiftCard\Entity\GiftCard;
use Sylius\Component\Order\Factory\AdjustmentFactoryInterface;
use Sylius\Component\Order\Model\OrderInterface;
use Sylius\Component\Order\Processor\OrderProcessorInterface;
use Sylius\Resource\Doctrine\Persistence\RepositoryInterface;

final class GiftCardOrderProcessor implements OrderProcessorInterface
{
public const GIFT_CARD_ADJUSTMENT = 'gift_card';

public function __construct(
private readonly RepositoryInterface $giftCardRepository,
private readonly AdjustmentFactoryInterface $adjustmentFactory
) {
}

/** @param Order $order */
public function process(OrderInterface $order): void
{
$order->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);
}
}
31 changes: 31 additions & 0 deletions src/Migrations/Version20240514100514.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240514100514 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
8 changes: 8 additions & 0 deletions templates/Shop/Cart/Summary/giftCardCode.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="sylius-gift-card" style="margin-top: 1rem;">
<div class="ui coupon action input">
{{ form_widget(form.giftCardCode, {'attr': {'form': main_form }}) }}
<button type="submit" id="sylius-save" class="ui purple icon labeled button" form="{{ main_form }}"><i class="gift icon"></i> Add gift card</button>
</div>
<br>
{{ form_errors(form.giftCardCode) }}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
<td class="right aligned">{{ money.convertAndFormat(cart.packagingTotal) }}</td>
</tr>
{% endif %}
{% if cart.giftCardTotal < 0 %}
<tr>
<td>Gift cards</td>
<td class="right aligned">{{ money.convertAndFormat(cart.giftCardTotal) }}</td>
</tr>
{% endif %}
<tr class="ui large header">
<td>{{ 'sylius.ui.order_total'|trans }}:</td>
<td id="sylius-cart-grand-total" {{ sylius_test_html_attribute('cart-grand-total') }} class="right aligned">{{ money.convertAndFormat(cart.total) }}</td>
Expand Down

0 comments on commit 053ae6d

Please sign in to comment.