Skip to content

Commit

Permalink
Customise ProductVariant to generate gift card with given value
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed May 15, 2024
1 parent 8e5a513 commit 997f7b9
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Entity/Product/ProductVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
27 changes: 27 additions & 0 deletions src/Form/Extension/ProductVariantTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Form\Extension;

use Sylius\Bundle\MoneyBundle\Form\Type\MoneyType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

final class ProductVariantTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('generatedGiftCardValue', MoneyType::class, [
'currency' => 'USD',
])
;
}

public static function getExtendedTypes(): iterable
{
yield ProductVariantType::class;
}
}
31 changes: 31 additions & 0 deletions src/Migrations/Version20240508115429.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 Version20240508115429 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_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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% from '@SyliusAdmin/Macro/translationForm.html.twig' import translationForm %}

<div class="ui active tab" data-tab="details">
<h3 class="ui dividing header">{{ 'sylius.ui.details'|trans }}</h3>

<div class="ui segments">
{{ translationForm(form.translations) }}
<div class="ui hidden divider"></div>
<div class="ui segment">
{{ form_row(form.code) }}
{{ form_row(form.enabled) }}
</div>
<div class="ui hidden divider"></div>
<div class="ui segment">
<div class="two fields">
{{ form_row(form.shippingCategory) }}
</div>
<div class="one field">
{{ form_row(form.shippingRequired) }}
</div>
</div>
{% if form.optionValues is defined and form.optionValues|length > 0 %}
<div class="ui hidden divider"></div>
<div class="ui segment">
<h4 class="ui dividing header">{{ 'sylius.ui.options'|trans }}</h4>
{% for option_form in form.optionValues %}
{{ form_row(option_form) }}
{% endfor %}
</div>
{% endif %}
<div class="ui hidden divider"></div>
<div class="ui segment">
<h4 class="ui dividing header">{{ 'sylius.ui.properties'|trans }}</h4>
{{ form_row(form.height) }}
{{ form_row(form.width) }}
{{ form_row(form.depth) }}
{{ form_row(form.weight) }}
{{ form_row(form.generatedGiftCardValue) }}
</div>
</div>

{{ sylius_template_event(['sylius.admin.product_variant.' ~ action ~ '.tab_details', 'sylius.admin.product_variant.tab_details'], {'form': form}) }}
</div>

0 comments on commit 997f7b9

Please sign in to comment.