From 997f7b95dbc75529f8cb6bb71c9717dbc4eba270 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Wed, 8 May 2024 13:55:35 +0200 Subject: [PATCH] Customise ProductVariant to generate gift card with given value --- src/Entity/Product/ProductVariant.php | 17 ++++++-- .../Extension/ProductVariantTypeExtension.php | 27 ++++++++++++ src/Migrations/Version20240508115429.php | 31 +++++++++++++ .../ProductVariant/Tab/_details.html.twig | 43 +++++++++++++++++++ 4 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/Form/Extension/ProductVariantTypeExtension.php create mode 100644 src/Migrations/Version20240508115429.php create mode 100644 templates/bundles/SyliusAdminBundle/ProductVariant/Tab/_details.html.twig diff --git a/src/Entity/Product/ProductVariant.php b/src/Entity/Product/ProductVariant.php index 165da39..0749de1 100644 --- a/src/Entity/Product/ProductVariant.php +++ b/src/Entity/Product/ProductVariant.php @@ -8,14 +8,23 @@ use Sylius\Component\Core\Model\ProductVariant as BaseProductVariant; use Sylius\Component\Product\Model\ProductVariantTranslationInterface; -/** - * @ORM\Entity - * @ORM\Table(name="sylius_product_variant") - */ #[ORM\Entity] #[ORM\Table(name: 'sylius_product_variant')] class ProductVariant extends BaseProductVariant { + #[ORM\Column(name: 'generated_gift_card_value', type: 'integer', nullable: true)] + private ?int $generatedGiftCardValue = null; + + public function getGeneratedGiftCardValue(): ?int + { + return $this->generatedGiftCardValue; + } + + public function setGeneratedGiftCardValue(?int $generatedGiftCardValue): void + { + $this->generatedGiftCardValue = $generatedGiftCardValue; + } + protected function createTranslation(): ProductVariantTranslationInterface { return new ProductVariantTranslation(); diff --git a/src/Form/Extension/ProductVariantTypeExtension.php b/src/Form/Extension/ProductVariantTypeExtension.php new file mode 100644 index 0000000..5e8ae05 --- /dev/null +++ b/src/Form/Extension/ProductVariantTypeExtension.php @@ -0,0 +1,27 @@ +add('generatedGiftCardValue', MoneyType::class, [ + 'currency' => 'USD', + ]) + ; + } + + public static function getExtendedTypes(): iterable + { + yield ProductVariantType::class; + } +} diff --git a/src/Migrations/Version20240508115429.php b/src/Migrations/Version20240508115429.php new file mode 100644 index 0000000..0cfe8fa --- /dev/null +++ b/src/Migrations/Version20240508115429.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE sylius_product_variant ADD generated_gift_card_value INT 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_product_variant DROP generated_gift_card_value'); + } +} diff --git a/templates/bundles/SyliusAdminBundle/ProductVariant/Tab/_details.html.twig b/templates/bundles/SyliusAdminBundle/ProductVariant/Tab/_details.html.twig new file mode 100644 index 0000000..dda6858 --- /dev/null +++ b/templates/bundles/SyliusAdminBundle/ProductVariant/Tab/_details.html.twig @@ -0,0 +1,43 @@ +{% from '@SyliusAdmin/Macro/translationForm.html.twig' import translationForm %} + +
+

{{ 'sylius.ui.details'|trans }}

+ +
+ {{ translationForm(form.translations) }} + +
+ {{ form_row(form.code) }} + {{ form_row(form.enabled) }} +
+ +
+
+ {{ form_row(form.shippingCategory) }} +
+
+ {{ form_row(form.shippingRequired) }} +
+
+ {% if form.optionValues is defined and form.optionValues|length > 0 %} + +
+

{{ 'sylius.ui.options'|trans }}

+ {% for option_form in form.optionValues %} + {{ form_row(option_form) }} + {% endfor %} +
+ {% endif %} + +
+

{{ 'sylius.ui.properties'|trans }}

+ {{ form_row(form.height) }} + {{ form_row(form.width) }} + {{ form_row(form.depth) }} + {{ form_row(form.weight) }} + {{ form_row(form.generatedGiftCardValue) }} +
+
+ + {{ sylius_template_event(['sylius.admin.product_variant.' ~ action ~ '.tab_details', 'sylius.admin.product_variant.tab_details'], {'form': form}) }} +