-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Process gift card during cart processing
- Loading branch information
Showing
7 changed files
with
148 additions
and
0 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
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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'); | ||
} | ||
} |
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-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> |
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